From 1adde913c65f5514d1b90147de78e28ce4f47a28 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 13 Sep 2021 13:52:44 +0100 Subject: [PATCH] [PHPStan] Only run custom PHPStan extensions if environment vairable PHPSTAN_BOOT_KERNEL is defined (since it requires having the whole social setup available) --- .dir-locals.el | 3 +++ docker/tooling/coverage.sh | 2 +- docker/tooling/phpstan.sh | 4 +++- phpstan.neon | 5 +++++ ...DynamicStaticMethodReturnTypeExtension.php | 8 +++++++- src/PHPStan/GNUsocialProvider.php | 20 ++++++++++--------- 6 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 .dir-locals.el diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000000..f69c8d5a3d --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,3 @@ +((nil . ((php-project-root . auto) + (phpstan-config-file . (root . "phpstan.neon")) + (phpstan-memory-limit . "2G")))) diff --git a/docker/tooling/coverage.sh b/docker/tooling/coverage.sh index 7a64f29f69..6091eecebb 100755 --- a/docker/tooling/coverage.sh +++ b/docker/tooling/coverage.sh @@ -4,4 +4,4 @@ cd /var/www/social || exit 1 yes yes | php bin/console doctrine:fixtures:load || exit 1 -runuser -u www-data -- vendor/bin/simple-phpunit -vvv --coverage-html .test_coverage_report +runuser -u www-data -- vendor/bin/simple-phpunit --ansi -vvv --coverage-html .test_coverage_report diff --git a/docker/tooling/phpstan.sh b/docker/tooling/phpstan.sh index 53653998ad..0f28694e9e 100755 --- a/docker/tooling/phpstan.sh +++ b/docker/tooling/phpstan.sh @@ -2,4 +2,6 @@ cd /var/www/social || exit 1 -vendor/bin/phpstan --ansi --no-interaction --memory-limit=2G analyse src tests components plugins +rm -rf /var/www/social/var/cache/* +PHPSTAN_BOOT_KERNEL=1 vendor/bin/phpstan --ansi --no-interaction --memory-limit=2G analyse +rm -rf /var/www/social/var/cache/* diff --git a/phpstan.neon b/phpstan.neon index dacb28b0e4..d0635f5ac1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,11 @@ parameters: level: 3 bootstrapFiles: - config/bootstrap.php + paths: + - src + - tests + - components + - plugins earlyTerminatingMethodCalls: App\Core\Log: - unexpected_exception diff --git a/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php b/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php index fc01aa7e68..8351416225 100644 --- a/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php +++ b/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php @@ -28,12 +28,18 @@ class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements Dynami return in_array($methodReflection->getName(), DB::METHODS_ACCEPTING_TABLE_NAME); } + /** + * For calls to DB::find and such, if the first argument is a + * constant string, it's a table name, so convert it to the + * corresponding entity. Only run if the environment variable + * PHPSTAN_BOOT_KERNEL is defined + */ public function getTypeFromStaticMethodCall( MethodReflection $methodReflection, StaticCall $staticCall, Scope $scope ): \PHPStan\Type\Type { - if (count($staticCall->args) >= 1 && ($arg = $staticCall->args[0]->value) instanceof String_) { + if (isset($_ENV['PHPSTAN_BOOT_KERNEL']) && count($staticCall->args) >= 1 && ($arg = $staticCall->args[0]->value) instanceof String_) { // If called with the first argument as a string, it's a table name return $scope->resolveTypeByName(new Name(DB::filterTableName($staticCall->name, [$arg->value]))); } else { diff --git a/src/PHPStan/GNUsocialProvider.php b/src/PHPStan/GNUsocialProvider.php index a36dc04218..c9e0f7bda6 100644 --- a/src/PHPStan/GNUsocialProvider.php +++ b/src/PHPStan/GNUsocialProvider.php @@ -14,15 +14,17 @@ class GNUsocialProvider public function __construct() { - $this->kernel = require __DIR__ . '/../../config/phpstan-bootstrap.php'; - $container = $this->kernel->getContainer()->get('test.service_container'); - $services = F\map( - (new \ReflectionClass(GNUsocial::class))->getMethod('__construct')->getParameters(), - fn ($p) => $container->get((string) $p->getType()) - ); - $this->gnu_social = new GNUsocial(...$services); - $this->gnu_social->initialize(); - Event::handle('InitializeModule'); + if (isset($_ENV['PHPSTAN_BOOT_KERNEL'])) { + $this->kernel = require __DIR__ . '/../../config/phpstan-bootstrap.php'; + $container = $this->kernel->getContainer()->get('test.service_container'); + $services = F\map( + (new \ReflectionClass(GNUsocial::class))->getMethod('__construct')->getParameters(), + fn ($p) => $container->get((string) $p->getType()) + ); + $this->gnu_social = new GNUsocial(...$services); + $this->gnu_social->initialize(); + Event::handle('InitializeModule'); + } } public function getGNUsocial(): GNUsocial