forked from GNUsocial/gnu-social
[TOOLS][PHPStan] Add mechanism for initializing the whole application
This commit is contained in:
parent
ddb9702b1c
commit
f81bf4a257
@ -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;
|
||||
|
@ -18,4 +18,4 @@ services:
|
||||
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
|
||||
|
||||
-
|
||||
class: App\PHPStan\ObjectManagerResolver
|
||||
class: App\PHPStan\GNUsocialProvider
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
32
src/PHPStan/GNUsocialProvider.php
Normal file
32
src/PHPStan/GNUsocialProvider.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user