Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-8568] Configure and enable PHPStan #433

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ jobs:

- name: Run PHPUnit
run: vendor/bin/phpunit

- name: Run PHPStan
run: composer phpstan
51 changes: 51 additions & 0 deletions bin/phpstan-config-generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

use Adyen\Shopware\AdyenPaymentShopware6;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\StaticKernelPluginLoader;
use Shopware\Core\DevOps\StaticAnalyze\StaticAnalyzeKernel;
use Symfony\Component\Dotenv\Dotenv;

$projectRoot = dirname(__DIR__, 4);
$pluginRootPath = dirname(__DIR__);

$classLoader = require $projectRoot . '/vendor/autoload.php';
if (file_exists($projectRoot . '/.env')) {
(new Dotenv())->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);
11 changes: 11 additions & 0 deletions bin/static-analyze-autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

use Symfony\Component\Dotenv\Dotenv;

$projectRoot = $_SERVER['PROJECT_ROOT'] ?? dirname(__DIR__, 4);

require_once $projectRoot . '/vendor/autoload.php';

if (file_exists($projectRoot . '/.env')) {
(new Dotenv())->usePutEnv()->load($projectRoot . '/.env');
}
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@
"allow-plugins": {
"symfony/*": true
}
},
"scripts": {
"phpstan": [
"php bin/phpstan-config-generator.php",
"../../../vendor/bin/phpstan analyze -c phpstan.neon.dist"
]
}
}
49 changes: 49 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -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
Loading