Skip to content

Commit 2206359

Browse files
authored
Merge pull request #56 from jolicode/fix-ci
Fix CI and setup PHPStan
2 parents 052422a + 924d990 commit 2206359

File tree

9 files changed

+78
-23
lines changed

9 files changed

+78
-23
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Checkout
16-
uses: actions/checkout@v2
16+
uses: actions/checkout@v3
1717

1818
- name: PHP-CS-Fixer
1919
uses: docker://oskarstark/php-cs-fixer-ga
2020
with:
21-
args: --config=.php-cs-fixer.php --diff --dry-run
21+
args: --config=.php-cs-fixer.php --diff --dry-run
22+
23+
phpstan:
24+
name: PHPStan
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v3
29+
30+
- name: PHPStan
31+
uses: docker://oskarstark/phpstan-ga
32+
env:
33+
REQUIRE_DEV: true
2234

2335
ci:
2436
name: Test PHP ${{ matrix.php-version }} ${{ matrix.name }}
@@ -36,7 +48,7 @@ jobs:
3648
name: '(prefer lowest dependencies)'
3749
steps:
3850
- name: Checkout
39-
uses: actions/checkout@v2
51+
uses: actions/checkout@v3
4052

4153
- name: Setup PHP, with composer and extensions
4254
uses: shivammathur/setup-php@v2

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: 10 additions & 5 deletions
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',
@@ -62,9 +66,6 @@ public function onKernelResponse(ResponseEvent $event): void
6266
$event->getResponse()->setContent($content);
6367
}
6468

65-
/**
66-
* {@inheritdoc}
67-
*/
6869
public static function getSubscribedEvents(): array
6970
{
7071
return [
@@ -78,7 +79,7 @@ public static function getSubscribedEvents(): array
7879
private function getGifDir(int $statusCode): string
7980
{
8081
if (\array_key_exists($statusCode, $this->gifs) && \count($this->gifs[$statusCode]) > 0) {
81-
return $statusCode;
82+
return (string) $statusCode;
8283
}
8384

8485
return 'other';
@@ -89,6 +90,10 @@ private function getGifDir(int $statusCode): string
8990
*/
9091
private function getRandomGif(string $dir): string
9192
{
93+
if (!\array_key_exists($dir, $this->gifs) || 0 === \count($this->gifs[$dir])) {
94+
return '';
95+
}
96+
9297
$imageIndex = random_int(0, \count($this->gifs[$dir]) - 1);
9398

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

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ cs_dry_run: ## Test if PHP CS is correct
1313

1414
test: ## Run the test suite
1515
vendor/bin/simple-phpunit
16+
17+
phpstan: ## Run static analysis
18+
vendor/bin/phpstan analyse -c phpstan.neon

bin/optimizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* file that was distributed with this source code.
1111
*/
1212

13-
use Joli\GifExceptionBundle\Tests\app\AppKernel;
13+
use Joli\GifExceptionBundle\Tests\app\src\Kernel;
1414
use Symfony\Bundle\FrameworkBundle\Console\Application;
1515
use Symfony\Component\Console\Input\ArgvInput;
1616

@@ -36,6 +36,6 @@
3636
array_unshift($args, __DIR__ . '/optimizer.php');
3737

3838
$input = new ArgvInput($args);
39-
$kernel = new AppKernel('dev', false);
39+
$kernel = new Kernel('dev', false);
4040
$application = new Application($kernel);
4141
$application->run($input);

composer.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
"symfony/twig-bundle": "^5.4 || ^6.0"
2424
},
2525
"require-dev": {
26-
"doctrine/annotations": "^1.2",
27-
"friendsofphp/php-cs-fixer": "^3.0",
26+
"doctrine/annotations": "^2.0",
27+
"friendsofphp/php-cs-fixer": "^3.17",
28+
"phpstan/phpstan": "^1.10",
2829
"ps/image-optimizer": "^1.0.4 || ^2.0",
2930
"symfony/asset": "^5.4 || ^6.0",
3031
"symfony/browser-kit": "^5.4 || ^6.0",
3132
"symfony/config": "^5.4 || ^6.0",
32-
"symfony/console": "^6.3",
33-
"symfony/debug": "^4.4",
33+
"symfony/console": "^5.4 || ^6.0",
3434
"symfony/dependency-injection": "^5.4 || ^6.0",
35+
"symfony/error-handler": "^5.4 || ^6.0",
3536
"symfony/http-kernel": "^5.4 || ^6.0",
3637
"symfony/monolog-bundle": "^3.8",
37-
"symfony/phpunit-bridge": "^5.4 || ^6.0",
38-
"symfony/profiler-pack": "^1.0",
38+
"symfony/phpunit-bridge": "^6.3",
39+
"symfony/stopwatch": "^5.4 || ^6.0",
40+
"symfony/web-profiler-bundle": "^5.4 || ^6.0",
3941
"symfony/yaml": "^5.4 || ^6.0"
4042
},
4143
"config": {

phpstan.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- .
5+
excludePaths:
6+
- vendor
7+
- tests

tests/app/config/config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ framework:
77
http_method_override: false
88
profiler:
99
only_exceptions: false
10-
collect_serializer_data: true
1110

1211
web_profiler:
1312
toolbar: true

0 commit comments

Comments
 (0)