Skip to content

Commit

Permalink
[ECP-8568] Configure and enable PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
candemiralp committed Nov 17, 2023
1 parent ecc3b5c commit be3883a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
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

0 comments on commit be3883a

Please sign in to comment.