From f81bf4a2574e22db9e881f15ca9376502e734755 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 8 Sep 2021 19:37:33 +0000 Subject: [PATCH] [TOOLS][PHPStan] Add mechanism for initializing the whole application --- config/phpstan-bootstrap.php | 3 +- phpstan.neon | 2 +- ...DynamicStaticMethodReturnTypeExtension.php | 27 ++++++---------- src/PHPStan/GNUsocialProvider.php | 32 +++++++++++++++++++ src/PHPStan/ObjectManagerResolver.php | 20 ------------ 5 files changed, 43 insertions(+), 41 deletions(-) create mode 100644 src/PHPStan/GNUsocialProvider.php delete mode 100644 src/PHPStan/ObjectManagerResolver.php diff --git a/config/phpstan-bootstrap.php b/config/phpstan-bootstrap.php index 015a7a02bf..8fcfe5c4d4 100644 --- a/config/phpstan-bootstrap.php +++ b/config/phpstan-bootstrap.php @@ -3,8 +3,7 @@ require_once 'bootstrap.php'; use App\Kernel; -use Symfony\Component\Dotenv\Dotenv; $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $kernel->boot(); -return $kernel->getContainer()->get('doctrine')->getManager(); +return $kernel; diff --git a/phpstan.neon b/phpstan.neon index 39a64b9b05..a190cadce8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -18,4 +18,4 @@ services: - phpstan.broker.dynamicStaticMethodReturnTypeExtension - - class: App\PHPStan\ObjectManagerResolver + class: App\PHPStan\GNUsocialProvider diff --git a/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php b/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php index 929b44098a..fc01aa7e68 100644 --- a/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php +++ b/src/PHPStan/ClassFromTableNameDynamicStaticMethodReturnTypeExtension.php @@ -3,22 +3,19 @@ namespace App\PHPStan; use App\Core\DB\DB; -use App\Util\Formatting; -use Doctrine\Persistence\ObjectManager; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Scalar\String_; use PhpParser\Node\Name; +use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; -use PHPStan\Type\Constant\ConstantStringType; class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension { - private ?ObjectManager $manager = null; - public function __construct(ObjectManagerResolver $resolver) + private ?GNUsocialProvider $provider = null; + public function __construct(GNUsocialProvider $provider) { - $this->manager = $resolver->getManager(); + $this->provider = $provider; } public function getClass(): string @@ -35,18 +32,12 @@ class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements Dynami MethodReflection $methodReflection, StaticCall $staticCall, Scope $scope - ): \PHPStan\Type\Type - { - if (count($staticCall->args) === 0) { - return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs($scope, $staticCall->args, $methodReflection->getVariants())->getReturnType(); - } - $arg = $staticCall->args[0]->value; - if ($arg instanceof String_) { - DB::setManager($this->manager); - DB::initTableMap(); - $class = DB::filterTableName($staticCall->name, [$arg->value]); - return $scope->resolveTypeByName(new Name($class)); + ): \PHPStan\Type\Type { + if (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 { + // Let PHPStan handle it normally return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs($scope, $staticCall->args, $methodReflection->getVariants())->getReturnType(); } } diff --git a/src/PHPStan/GNUsocialProvider.php b/src/PHPStan/GNUsocialProvider.php new file mode 100644 index 0000000000..a36dc04218 --- /dev/null +++ b/src/PHPStan/GNUsocialProvider.php @@ -0,0 +1,32 @@ +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 + { + return $this->gnu_social; + } +} diff --git a/src/PHPStan/ObjectManagerResolver.php b/src/PHPStan/ObjectManagerResolver.php deleted file mode 100644 index a353b5522c..0000000000 --- a/src/PHPStan/ObjectManagerResolver.php +++ /dev/null @@ -1,20 +0,0 @@ -objectManager !== null) { - return $this->objectManager; - } else { - $this->objectManager = require __DIR__ . '/../../config/phpstan-bootstrap.php'; - return $this->objectManager; - } - } -}