forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScopeIsInClassTypeSpecifyingExtension.php
74 lines (63 loc) · 1.87 KB
/
ScopeIsInClassTypeSpecifyingExtension.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php declare(strict_types = 1);
namespace PHPStan\Internal;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\TypeCombinator;
class ScopeIsInClassTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function __construct(
private string $isInMethodName,
private string $removeNullMethodName,
private ReflectionProvider $reflectionProvider,
)
{
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
public function getClass(): string
{
return ClassMemberAccessAnswerer::class;
}
public function isMethodSupported(
MethodReflection $methodReflection,
MethodCall $node,
TypeSpecifierContext $context,
): bool
{
return $methodReflection->getName() === $this->isInMethodName
&& !$context->null();
}
public function specifyTypes(
MethodReflection $methodReflection,
MethodCall $node,
Scope $scope,
TypeSpecifierContext $context,
): SpecifiedTypes
{
$scopeClass = $this->reflectionProvider->getClass(Scope::class);
$methodVariants = $scopeClass
->getMethod($this->removeNullMethodName, $scope)
->getVariants();
return $this->typeSpecifier->create(
new MethodCall($node->var, $this->removeNullMethodName),
TypeCombinator::removeNull(
ParametersAcceptorSelector::selectSingle($methodVariants)->getReturnType(),
),
$context,
false,
$scope,
);
}
}