Skip to content

Commit

Permalink
Merge pull request #56 from jolicode/fix-ci
Browse files Browse the repository at this point in the history
Fix CI and setup PHPStan
  • Loading branch information
pyrech authored Jun 7, 2023
2 parents 052422a + 924d990 commit 2206359
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 23 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.php --diff --dry-run
args: --config=.php-cs-fixer.php --diff --dry-run

phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
REQUIRE_DEV: true

ci:
name: Test PHP ${{ matrix.php-version }} ${{ matrix.name }}
Expand All @@ -36,7 +48,7 @@ jobs:
name: '(prefer lowest dependencies)'
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
Expand Down
23 changes: 21 additions & 2 deletions Command/GifOptimizerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$imageDir = $input->getArgument('image_dir');

if (!is_dir($imageDir)) {
if (!\is_string($imageDir) || !is_dir($imageDir)) {
throw new \RuntimeException($imageDir . ' is not a valid directory');
}

$pattern = $imageDir . '/*/*.gif';
foreach (glob($pattern) as $path) {
$images = glob($pattern);

if (!$images) {
throw new \RuntimeException('No images found in ' . $pattern);
}

foreach ($images as $path) {
$realPath = realpath($path);

if (!$realPath) {
throw new \RuntimeException('Could not find ' . $path);
}

$originalFileSize = filesize($realPath);

if (!$originalFileSize) {
throw new \RuntimeException('Could not get file size for ' . $realPath);
}

$output->writeln(sprintf('<info>Optimizing image: %s</info>', $realPath));
$output->writeln(sprintf('<comment>Before: %s</comment>', $this->formatBytes($originalFileSize)));

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

$optimizedFileSize = filesize($realPath);

if (!$optimizedFileSize) {
throw new \RuntimeException('Could not get file size for ' . $realPath);
}

$output->writeln(sprintf('<comment>After: %s</comment>', $this->formatBytes($optimizedFileSize)));

$percentage = 100 - (($optimizedFileSize / $originalFileSize) * 100);
Expand Down
16 changes: 12 additions & 4 deletions DependencyInjection/GifExceptionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ public function load(array $configs, ContainerBuilder $container): void
$gifs = [];

$pattern = __DIR__ . '/../Resources/public/images/*/*.gif';
foreach (glob($pattern) as $path) {
$gifs[basename(\dirname($path))][] = basename($path);
$images = glob($pattern);

if ($images) {
foreach ($images as $path) {
$gifs[basename(\dirname($path))][] = basename($path);
}
}

$pattern = __DIR__ . '/../Resources/public/images/other/*.gif';
foreach (glob($pattern) as $path) {
$gifs['other'][] = basename($path);
$images = glob($pattern);

if ($images) {
foreach ($images as $path) {
$gifs['other'][] = basename($path);
}
}

$container->getDefinition('gif_exception.listener.replace_image')->replaceArgument(0, $gifs);
Expand Down
15 changes: 10 additions & 5 deletions EventListener/ReplaceImageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function onKernelResponse(ResponseEvent $event): void
}

$exception = $event->getRequest()->attributes->get('exception');
if (!($exception instanceof \Throwable)) {
return;
}

// Status code is not set by the exception controller but only by the
// kernel at the very end.
// So lets use the status code from the flatten exception instead.
Expand All @@ -51,7 +55,7 @@ public function onKernelResponse(ResponseEvent $event): void
$gif = $this->getRandomGif($dir);
$url = $this->getGifUrl($dir, $gif);

$content = $event->getResponse()->getContent();
$content = (string) $event->getResponse()->getContent();

$content = preg_replace(
'@<div class="exception-illustration hidden-xs-down">(.*?)</div>@ims',
Expand All @@ -62,9 +66,6 @@ public function onKernelResponse(ResponseEvent $event): void
$event->getResponse()->setContent($content);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
Expand All @@ -78,7 +79,7 @@ public static function getSubscribedEvents(): array
private function getGifDir(int $statusCode): string
{
if (\array_key_exists($statusCode, $this->gifs) && \count($this->gifs[$statusCode]) > 0) {
return $statusCode;
return (string) $statusCode;
}

return 'other';
Expand All @@ -89,6 +90,10 @@ private function getGifDir(int $statusCode): string
*/
private function getRandomGif(string $dir): string
{
if (!\array_key_exists($dir, $this->gifs) || 0 === \count($this->gifs[$dir])) {
return '';
}

$imageIndex = random_int(0, \count($this->gifs[$dir]) - 1);

return $this->gifs[$dir][$imageIndex];
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ cs_dry_run: ## Test if PHP CS is correct

test: ## Run the test suite
vendor/bin/simple-phpunit

phpstan: ## Run static analysis
vendor/bin/phpstan analyse -c phpstan.neon
4 changes: 2 additions & 2 deletions bin/optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* file that was distributed with this source code.
*/

use Joli\GifExceptionBundle\Tests\app\AppKernel;
use Joli\GifExceptionBundle\Tests\app\src\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

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

$input = new ArgvInput($args);
$kernel = new AppKernel('dev', false);
$kernel = new Kernel('dev', false);
$application = new Application($kernel);
$application->run($input);
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@
"symfony/twig-bundle": "^5.4 || ^6.0"
},
"require-dev": {
"doctrine/annotations": "^1.2",
"friendsofphp/php-cs-fixer": "^3.0",
"doctrine/annotations": "^2.0",
"friendsofphp/php-cs-fixer": "^3.17",
"phpstan/phpstan": "^1.10",
"ps/image-optimizer": "^1.0.4 || ^2.0",
"symfony/asset": "^5.4 || ^6.0",
"symfony/browser-kit": "^5.4 || ^6.0",
"symfony/config": "^5.4 || ^6.0",
"symfony/console": "^6.3",
"symfony/debug": "^4.4",
"symfony/console": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
"symfony/error-handler": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/monolog-bundle": "^3.8",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"symfony/profiler-pack": "^1.0",
"symfony/phpunit-bridge": "^6.3",
"symfony/stopwatch": "^5.4 || ^6.0",
"symfony/web-profiler-bundle": "^5.4 || ^6.0",
"symfony/yaml": "^5.4 || ^6.0"
},
"config": {
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 9
paths:
- .
excludePaths:
- vendor
- tests
1 change: 0 additions & 1 deletion tests/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ framework:
http_method_override: false
profiler:
only_exceptions: false
collect_serializer_data: true

web_profiler:
toolbar: true
Expand Down

0 comments on commit 2206359

Please sign in to comment.