Skip to content

Commit 924d990

Browse files
committed
Make phpstan level max
1 parent b52d0ac commit 924d990

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

Command/GifOptimizerCommand.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7777
{
7878
$imageDir = $input->getArgument('image_dir');
7979

80-
if (!is_dir($imageDir)) {
80+
if (!\is_string($imageDir) || !is_dir($imageDir)) {
8181
throw new \RuntimeException($imageDir . ' is not a valid directory');
8282
}
8383

8484
$pattern = $imageDir . '/*/*.gif';
85-
foreach (glob($pattern) as $path) {
85+
$images = glob($pattern);
86+
87+
if (!$images) {
88+
throw new \RuntimeException('No images found in ' . $pattern);
89+
}
90+
91+
foreach ($images as $path) {
8692
$realPath = realpath($path);
93+
94+
if (!$realPath) {
95+
throw new \RuntimeException('Could not find ' . $path);
96+
}
97+
8798
$originalFileSize = filesize($realPath);
8899

100+
if (!$originalFileSize) {
101+
throw new \RuntimeException('Could not get file size for ' . $realPath);
102+
}
103+
89104
$output->writeln(sprintf('<info>Optimizing image: %s</info>', $realPath));
90105
$output->writeln(sprintf('<comment>Before: %s</comment>', $this->formatBytes($originalFileSize)));
91106

@@ -96,6 +111,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
96111

97112
$optimizedFileSize = filesize($realPath);
98113

114+
if (!$optimizedFileSize) {
115+
throw new \RuntimeException('Could not get file size for ' . $realPath);
116+
}
117+
99118
$output->writeln(sprintf('<comment>After: %s</comment>', $this->formatBytes($optimizedFileSize)));
100119

101120
$percentage = 100 - (($optimizedFileSize / $originalFileSize) * 100);

DependencyInjection/GifExceptionExtension.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ public function load(array $configs, ContainerBuilder $container): void
3030
$gifs = [];
3131

3232
$pattern = __DIR__ . '/../Resources/public/images/*/*.gif';
33-
foreach (glob($pattern) as $path) {
34-
$gifs[basename(\dirname($path))][] = basename($path);
33+
$images = glob($pattern);
34+
35+
if ($images) {
36+
foreach ($images as $path) {
37+
$gifs[basename(\dirname($path))][] = basename($path);
38+
}
3539
}
3640

3741
$pattern = __DIR__ . '/../Resources/public/images/other/*.gif';
38-
foreach (glob($pattern) as $path) {
39-
$gifs['other'][] = basename($path);
42+
$images = glob($pattern);
43+
44+
if ($images) {
45+
foreach ($images as $path) {
46+
$gifs['other'][] = basename($path);
47+
}
4048
}
4149

4250
$container->getDefinition('gif_exception.listener.replace_image')->replaceArgument(0, $gifs);

EventListener/ReplaceImageListener.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function onKernelResponse(ResponseEvent $event): void
3737
}
3838

3939
$exception = $event->getRequest()->attributes->get('exception');
40+
if (!($exception instanceof \Throwable)) {
41+
return;
42+
}
43+
4044
// Status code is not set by the exception controller but only by the
4145
// kernel at the very end.
4246
// So lets use the status code from the flatten exception instead.
@@ -51,7 +55,7 @@ public function onKernelResponse(ResponseEvent $event): void
5155
$gif = $this->getRandomGif($dir);
5256
$url = $this->getGifUrl($dir, $gif);
5357

54-
$content = $event->getResponse()->getContent();
58+
$content = (string) $event->getResponse()->getContent();
5559

5660
$content = preg_replace(
5761
'@<div class="exception-illustration hidden-xs-down">(.*?)</div>@ims',
@@ -86,6 +90,10 @@ private function getGifDir(int $statusCode): string
8690
*/
8791
private function getRandomGif(string $dir): string
8892
{
93+
if (!\array_key_exists($dir, $this->gifs) || 0 === \count($this->gifs[$dir])) {
94+
return '';
95+
}
96+
8997
$imageIndex = random_int(0, \count($this->gifs[$dir]) - 1);
9098

9199
return $this->gifs[$dir][$imageIndex];

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 6
2+
level: 9
33
paths:
44
- .
55
excludePaths:

0 commit comments

Comments
 (0)