From be3883abca598737d5efb30cae517e3a79a13edb Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Fri, 17 Nov 2023 14:15:30 +0100 Subject: [PATCH 1/2] [ECP-8568] Configure and enable PHPStan --- .github/workflows/main.yml | 3 ++ bin/phpstan-config-generator.php | 51 +++++++++++++++++++++++++++++++ bin/static-analyze-autoloader.php | 11 +++++++ composer.json | 6 ++++ phpstan.neon.dist | 49 +++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 bin/phpstan-config-generator.php create mode 100644 bin/static-analyze-autoloader.php create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index aa244284..d49788e2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,3 +32,6 @@ jobs: - name: Run PHPUnit run: vendor/bin/phpunit + + - name: Run PHPStan + run: composer phpstan diff --git a/bin/phpstan-config-generator.php b/bin/phpstan-config-generator.php new file mode 100644 index 00000000..ee871011 --- /dev/null +++ b/bin/phpstan-config-generator.php @@ -0,0 +1,51 @@ +usePutEnv()->load($projectRoot . '/.env'); +} + +$composerJson = json_decode((string) file_get_contents($pluginRootPath . '/composer.json'), true); +$adyenPaymentsShopware6 = [ + 'autoload' => $composerJson['autoload'], + 'baseClass' => AdyenPaymentShopware6::class, + 'managedByComposer' => false, + 'name' => 'AdyenPaymentsShopware6', + 'version' => $composerJson['version'], + 'active' => true, + 'path' => $pluginRootPath, +]; +$pluginLoader = new StaticKernelPluginLoader($classLoader, null, [$adyenPaymentsShopware6]); + +$kernel = new StaticAnalyzeKernel('dev', true, $pluginLoader, 'phpstan-test-cache-id'); +$kernel->boot(); + +$phpStanConfigDist = file_get_contents($pluginRootPath . '/phpstan.neon.dist'); +if ($phpStanConfigDist === false) { + throw new RuntimeException('phpstan.neon.dist file not found'); +} + +// because the cache dir is hashed by Shopware, we need to set the PHPStan config dynamically +$phpStanConfig = str_replace( + [ + '%ShopwareHashedCacheDir%', + '%ShopwareRoot%', + '%ShopwareKernelClass%', + ], + [ + str_replace($kernel->getProjectDir(), '', $kernel->getCacheDir()), + $projectRoot . (is_dir($projectRoot . '/platform') ? '/platform' : ''), + str_replace('\\', '_', get_class($kernel)), + ], + $phpStanConfigDist +); + +file_put_contents(__DIR__ . '/../phpstan.neon.dist', $phpStanConfig); diff --git a/bin/static-analyze-autoloader.php b/bin/static-analyze-autoloader.php new file mode 100644 index 00000000..e36bac88 --- /dev/null +++ b/bin/static-analyze-autoloader.php @@ -0,0 +1,11 @@ +usePutEnv()->load($projectRoot . '/.env'); +} diff --git a/composer.json b/composer.json index d142e9db..dfdfc956 100644 --- a/composer.json +++ b/composer.json @@ -48,5 +48,11 @@ "allow-plugins": { "symfony/*": true } + }, + "scripts": { + "phpstan": [ + "php bin/phpstan-config-generator.php", + "../../../vendor/bin/phpstan analyze -c phpstan.neon.dist" + ] } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..91088c1c --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,49 @@ +includes: + - %ShopwareRoot%/vendor/phpstan/phpstan/conf/bleedingEdge.neon + +parameters: + phpVersion: 80100 + level: 8 + tmpDir: var/cache/phpstan + inferPrivatePropertyTypeFromConstructor: true + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + reportUnmatchedIgnoredErrors: false + + paths: + - src + excludePaths: + - src/Resources + - src/DevOps/Rector + - src/Test + + ignoreErrors: + # We won't type all arrays/iterables for now + - '#no value type specified in iterable type#' + + - # ignore attributes, since we have to support PHP 7.4 for Shopware 6.4 + message: '#use the .* attribute instead#' + + - # ignore Plus deprecations + message: '#deprecated.*(Plus|PLUS|_PAYMENT_|MERCHANT_LOCATION)#' + + - # ignore Symfony 6 message queue deprecations + message: '#AsMessageHandler#' + + - # ignore param type coverage + message: '#.*Add more param types to get over.*#' + + - # ignore lineItemPayloadDeprecation deprecations + message: '#Call to deprecated method setPayloadValue\(\) of class .*LineItem#' + + - # ignore DomainException deprecations + message: '#use .*Exception::.* instead#' + + - # ignore deprecated StockUpdater (PPI-805) + message : '#deprecated class Shopware\\Core\\Content\\Product\\DataAbstractionLayer\\StockUpdater#' + + - # ignore deprecated UrlGenerator + message : '#Use AbstractMediaUrlGenerator instead#' + + bootstrapFiles: + - bin/static-analyze-autoloader.php From 90918162feed0175349acf7a23188e65bda17eeb Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Fri, 17 Nov 2023 14:21:02 +0100 Subject: [PATCH 2/2] [ECP-8568] Fix typo --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d49788e2..c715bea6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -34,4 +34,4 @@ jobs: run: vendor/bin/phpunit - name: Run PHPStan - run: composer phpstan + run: composer phpstan