2021-09-07 21:16:47 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\PHPStan;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Name;
|
2021-09-08 20:37:33 +01:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2021-09-07 21:16:47 +01:00
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
use PHPStan\Reflection\MethodReflection;
|
|
|
|
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
|
|
|
|
|
|
|
|
class ClassFromTableNameDynamicStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
|
|
|
|
{
|
2021-09-08 20:37:33 +01:00
|
|
|
private ?GNUsocialProvider $provider = null;
|
|
|
|
public function __construct(GNUsocialProvider $provider)
|
2021-09-07 21:16:47 +01:00
|
|
|
{
|
2021-09-08 20:37:33 +01:00
|
|
|
$this->provider = $provider;
|
2021-09-07 21:16:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getClass(): string
|
|
|
|
{
|
|
|
|
return \App\Core\DB\DB::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
|
|
|
|
{
|
|
|
|
return in_array($methodReflection->getName(), DB::METHODS_ACCEPTING_TABLE_NAME);
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:52:44 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-09-07 21:16:47 +01:00
|
|
|
public function getTypeFromStaticMethodCall(
|
|
|
|
MethodReflection $methodReflection,
|
|
|
|
StaticCall $staticCall,
|
|
|
|
Scope $scope
|
2021-09-08 20:37:33 +01:00
|
|
|
): \PHPStan\Type\Type {
|
2021-09-13 13:52:44 +01:00
|
|
|
if (isset($_ENV['PHPSTAN_BOOT_KERNEL']) && count($staticCall->args) >= 1 && ($arg = $staticCall->args[0]->value) instanceof String_) {
|
2021-09-08 20:37:33 +01:00
|
|
|
// 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])));
|
2021-09-07 21:16:47 +01:00
|
|
|
} else {
|
2021-09-08 20:37:33 +01:00
|
|
|
// Let PHPStan handle it normally
|
2021-09-07 21:16:47 +01:00
|
|
|
return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs($scope, $staticCall->args, $methodReflection->getVariants())->getReturnType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|