From de3d68141dd74407da5733cb4483212e78b10cf8 Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Wed, 15 Nov 2023 13:36:23 +0100 Subject: [PATCH 1/6] Added check for unique filenames in sass-paths --- .../SymfonycastsSassExtension.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index 3025038..5c485f3 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -17,6 +17,9 @@ use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; +use function basename; +use function in_array; + class SymfonycastsSassExtension extends Extension implements ConfigurationInterface { public function load(array $configs, ContainerBuilder $container): void @@ -59,6 +62,22 @@ public function getConfigTreeBuilder(): TreeBuilder ->cannotBeEmpty() ->scalarPrototype() ->end() + ->validate() + ->ifTrue(static function (array $paths): bool { + if (count($paths) === 1) { + return false; + } + + $filenames = []; + foreach ($paths as $path) { + $filename = basename($path, '.scss'); + $filenames[$filename] = $filename; + } + + return count($filenames) !== count($paths); + }) + ->thenInvalid('The root sass-paths need to end with unique filenames.') + ->end() ->defaultValue(['%kernel.project_dir%/assets/styles/app.scss']) ->end() ->scalarNode('binary') From b2c9eaa9199342c349c661497bcb4c747310654f Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Wed, 15 Nov 2023 13:37:07 +0100 Subject: [PATCH 2/6] Added test for bundle configuration --- composer.json | 1 + tests/ConfigurationTest.php | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/ConfigurationTest.php diff --git a/composer.json b/composer.json index b21275d..ff10a9e 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "symfony/process": "^5.4|^6.3" }, "require-dev": { + "matthiasnoback/symfony-config-test": "^5.0", "symfony/filesystem": "^6.3", "symfony/framework-bundle": "^6.3", "symfony/phpunit-bridge": "^6.3", diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php new file mode 100644 index 0000000..6409541 --- /dev/null +++ b/tests/ConfigurationTest.php @@ -0,0 +1,55 @@ +assertConfigurationIsValid([ + 'symfonycasts_sass' => [ + 'root_sass' => [ + '%kernel.project_dir%/assets/scss/app.scss' + ] + ] + ]); + } + + public function testMultipleSassRootPaths(): void + { + $this->assertConfigurationIsValid([ + 'symfonycasts_sass' => [ + 'root_sass' => [ + '%kernel.project_dir%/assets/scss/app.scss', + '%kernel.project_dir%/assets/admin/scss/admin.scss' + ] + ] + ]); + } + + public function testMultipleSassRootPathsWithSameFilename(): void + { + $this->assertConfigurationIsInvalid([ + 'symfonycasts_sass' => [ + 'root_sass' => [ + '%kernel.project_dir%/assets/scss/app.scss', + '%kernel.project_dir%/assets/admin/scss/app.scss' + ] + ] + ], + 'Invalid configuration for path "symfonycasts_sass.root_sass": The root sass-paths need to end with unique filenames.'); + } +} From e824d26ce2cc1af1262769e27d75b2d2d3bc71e8 Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Wed, 15 Nov 2023 13:41:17 +0100 Subject: [PATCH 3/6] Added phpstan-symfony to prevent new errors and simplify config --- composer.json | 2 +- phpstan.neon.dist | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index ff10a9e..31f2e18 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "symfony/filesystem": "^6.3", "symfony/framework-bundle": "^6.3", "symfony/phpunit-bridge": "^6.3", - "phpstan/phpstan": "1.11.x-dev" + "phpstan/phpstan-symfony": "^1.4" }, "minimum-stability": "dev", "autoload": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ded42a0..6ee10a5 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,10 +1,7 @@ +includes: + - vendor/phpstan/phpstan-symfony/extension.neon + parameters: level: 4 paths: - src - - ignoreErrors: - - - message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\:\\:defaultValue\\(\\)\\.$#" - count: 1 - path: src/DependencyInjection/SymfonycastsSassExtension.php From d3f06849c173f019db66ac4fec8a27db6c83539f Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Wed, 15 Nov 2023 13:42:10 +0100 Subject: [PATCH 4/6] Updated phpstan level to 5 --- phpstan.neon.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6ee10a5..5c3268e 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,6 +2,6 @@ includes: - vendor/phpstan/phpstan-symfony/extension.neon parameters: - level: 4 + level: 5 paths: - src From d82f70356ff00946466e1e7e83d77b069f9744cb Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Wed, 15 Nov 2023 13:49:06 +0100 Subject: [PATCH 5/6] Fix php-cs-fixer errors --- .../SymfonycastsSassExtension.php | 7 ++--- tests/ConfigurationTest.php | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index 5c485f3..fb46ffd 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -17,9 +17,6 @@ use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; -use function basename; -use function in_array; - class SymfonycastsSassExtension extends Extension implements ConfigurationInterface { public function load(array $configs, ContainerBuilder $container): void @@ -64,7 +61,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->validate() ->ifTrue(static function (array $paths): bool { - if (count($paths) === 1) { + if (1 === \count($paths)) { return false; } @@ -74,7 +71,7 @@ public function getConfigTreeBuilder(): TreeBuilder $filenames[$filename] = $filename; } - return count($filenames) !== count($paths); + return \count($filenames) !== \count($paths); }) ->thenInvalid('The root sass-paths need to end with unique filenames.') ->end() diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 6409541..cd0dee9 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -2,6 +2,13 @@ declare(strict_types=1); +/* + * This file is part of the SymfonyCasts SassBundle package. + * Copyright (c) SymfonyCasts + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\SassBundle\Tests; use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; @@ -22,9 +29,9 @@ public function testSingleSassRootPath(): void $this->assertConfigurationIsValid([ 'symfonycasts_sass' => [ 'root_sass' => [ - '%kernel.project_dir%/assets/scss/app.scss' - ] - ] + '%kernel.project_dir%/assets/scss/app.scss', + ], + ], ]); } @@ -34,9 +41,9 @@ public function testMultipleSassRootPaths(): void 'symfonycasts_sass' => [ 'root_sass' => [ '%kernel.project_dir%/assets/scss/app.scss', - '%kernel.project_dir%/assets/admin/scss/admin.scss' - ] - ] + '%kernel.project_dir%/assets/admin/scss/admin.scss', + ], + ], ]); } @@ -46,10 +53,10 @@ public function testMultipleSassRootPathsWithSameFilename(): void 'symfonycasts_sass' => [ 'root_sass' => [ '%kernel.project_dir%/assets/scss/app.scss', - '%kernel.project_dir%/assets/admin/scss/app.scss' - ] - ] + '%kernel.project_dir%/assets/admin/scss/app.scss', + ], + ], ], - 'Invalid configuration for path "symfonycasts_sass.root_sass": The root sass-paths need to end with unique filenames.'); + 'Invalid configuration for path "symfonycasts_sass.root_sass": The root sass-paths need to end with unique filenames.'); } } From 53cc09d434b55ba580d0b4a320b0262371c245b2 Mon Sep 17 00:00:00 2001 From: Evert Harmeling Date: Thu, 16 Nov 2023 19:02:39 +0100 Subject: [PATCH 6/6] Update error message --- src/DependencyInjection/SymfonycastsSassExtension.php | 2 +- tests/ConfigurationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index fb46ffd..c07d9a4 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -73,7 +73,7 @@ public function getConfigTreeBuilder(): TreeBuilder return \count($filenames) !== \count($paths); }) - ->thenInvalid('The root sass-paths need to end with unique filenames.') + ->thenInvalid('The "root_sass" paths need to end with unique filenames.') ->end() ->defaultValue(['%kernel.project_dir%/assets/styles/app.scss']) ->end() diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index cd0dee9..f1b724e 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -57,6 +57,6 @@ public function testMultipleSassRootPathsWithSameFilename(): void ], ], ], - 'Invalid configuration for path "symfonycasts_sass.root_sass": The root sass-paths need to end with unique filenames.'); + 'Invalid configuration for path "symfonycasts_sass.root_sass": The "root_sass" paths need to end with unique filenames.'); } }