-
Notifications
You must be signed in to change notification settings - Fork 432
/
ReflectionFunctionConstructorThrowTypeExtension.php
55 lines (43 loc) · 1.57 KB
/
ReflectionFunctionConstructorThrowTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\DynamicStaticMethodThrowTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use ReflectionFunction;
use function count;
class ReflectionFunctionConstructorThrowTypeExtension implements DynamicStaticMethodThrowTypeExtension
{
public function __construct(private ReflectionProvider $reflectionProvider)
{
}
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === '__construct' && $methodReflection->getDeclaringClass()->getName() === ReflectionFunction::class;
}
public function getThrowTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
if (count($methodCall->getArgs()) < 1) {
return $methodReflection->getThrowType();
}
$valueType = $scope->getType($methodCall->getArgs()[0]->value);
foreach ($valueType->getConstantStrings() as $constantString) {
if ($constantString->getValue() === '') {
return null;
}
if (!$this->reflectionProvider->hasFunction(new Name($constantString->getValue()), $scope)) {
return $methodReflection->getThrowType();
}
$valueType = TypeCombinator::remove($valueType, $constantString);
}
if (!$valueType instanceof NeverType) {
return $methodReflection->getThrowType();
}
return null;
}
}