Browse Source

[PHPStan] Only run custom PHPStan extensions if environment vairable PHPSTAN_BOOT_KERNEL is defined (since it requires having the whole social setup available)

v3
Hugo Sales 1 month ago
parent
commit
1adde913c6
Signed by: someonewithpc <hugo@hsal.es> GPG Key ID: 7D0C7EAFC9D835A0
6 changed files with 30 additions and 12 deletions
  1. +3
    -0
      .dir-locals.el
  2. +1
    -1
      docker/tooling/coverage.sh
  3. +3
    -1
      docker/tooling/phpstan.sh
  4. +5
    -0
      phpstan.neon
  5. +7
    -1
      src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php
  6. +11
    -9
      src/PHPStan/GNUsocialProvider.php

+ 3
- 0
.dir-locals.el View File

@@ -0,0 +1,3 @@
((nil . ((php-project-root . auto)
(phpstan-config-file . (root . "phpstan.neon"))
(phpstan-memory-limit . "2G"))))

+ 1
- 1
docker/tooling/coverage.sh View File

@@ -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

+ 3
- 1
docker/tooling/phpstan.sh View File

@@ -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/*

+ 5
- 0
phpstan.neon View File

@@ -2,6 +2,11 @@ parameters:
level: 3
bootstrapFiles:
- config/bootstrap.php
paths:
- src
- tests
- components
- plugins
earlyTerminatingMethodCalls:
App\Core\Log:
- unexpected_exception


+ 7
- 1
src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php View File

@@ -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 {


+ 11
- 9
src/PHPStan/GNUsocialProvider.php View File

@@ -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


Loading…
Cancel
Save