[TOOLS][PHPStan] Add mechanism for initializing the whole application

This commit is contained in:
Hugo Sales 2021-09-08 19:37:33 +00:00
parent ddb9702b1c
commit f81bf4a257
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 43 additions and 41 deletions

View File

@ -3,8 +3,7 @@
require_once 'bootstrap.php'; require_once 'bootstrap.php';
use App\Kernel; use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot(); $kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager(); return $kernel;

View File

@ -18,4 +18,4 @@ services:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension - phpstan.broker.dynamicStaticMethodReturnTypeExtension
- -
class: App\PHPStan\ObjectManagerResolver class: App\PHPStan\GNUsocialProvider

View File

@ -3,22 +3,19 @@
namespace App\PHPStan; namespace App\PHPStan;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Util\Formatting;
use Doctrine\Persistence\ObjectManager;
use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope; use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\Constant\ConstantStringType;
class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{ {
private ?ObjectManager $manager = null; private ?GNUsocialProvider $provider = null;
public function __construct(ObjectManagerResolver $resolver) public function __construct(GNUsocialProvider $provider)
{ {
$this->manager = $resolver->getManager(); $this->provider = $provider;
} }
public function getClass(): string public function getClass(): string
@ -35,18 +32,12 @@ class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements Dynami
MethodReflection $methodReflection, MethodReflection $methodReflection,
StaticCall $staticCall, StaticCall $staticCall,
Scope $scope Scope $scope
): \PHPStan\Type\Type ): \PHPStan\Type\Type {
{ if (count($staticCall->args) >= 1 && ($arg = $staticCall->args[0]->value) instanceof String_) {
if (count($staticCall->args) === 0) { // If called with the first argument as a string, it's a table name
return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs($scope, $staticCall->args, $methodReflection->getVariants())->getReturnType(); return $scope->resolveTypeByName(new Name(DB::filterTableName($staticCall->name, [$arg->value])));
}
$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));
} else { } else {
// Let PHPStan handle it normally
return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs($scope, $staticCall->args, $methodReflection->getVariants())->getReturnType(); return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs($scope, $staticCall->args, $methodReflection->getVariants())->getReturnType();
} }
} }

View File

@ -0,0 +1,32 @@
<?php
namespace App\PHPStan;
use App\Core\Event;
use App\Core\GNUsocial;
use App\Kernel;
use Functional as F;
class GNUsocialProvider
{
private ?GNUsocial $gnu_social = null;
private ?Kernel $kernel = null;
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');
}
public function getGNUsocial(): GNUsocial
{
return $this->gnu_social;
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\PHPStan;
use Doctrine\Persistence\ObjectManager;
class ObjectManagerResolver
{
private ?ObjectManager $objectManager = null;
public function getManager(): ObjectManager
{
if ($this->objectManager !== null) {
return $this->objectManager;
} else {
$this->objectManager = require __DIR__ . '/../../config/phpstan-bootstrap.php';
return $this->objectManager;
}
}
}