forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodExistsTypeSpecifyingExtension.php
More file actions
123 lines (110 loc) · 3.21 KB
/
MethodExistsTypeSpecifyingExtension.php
File metadata and controls
123 lines (110 loc) · 3.21 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\HasMethodType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\UnionType;
use function count;
use function ltrim;
#[AutowiredService]
final class MethodExistsTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
public function isFunctionSupported(
FunctionReflection $functionReflection,
FuncCall $node,
TypeSpecifierContext $context,
): bool
{
return $functionReflection->getName() === 'method_exists'
&& $context->true()
&& count($node->getArgs()) >= 2;
}
public function specifyTypes(
FunctionReflection $functionReflection,
FuncCall $node,
Scope $scope,
TypeSpecifierContext $context,
): SpecifiedTypes
{
$args = $node->getArgs();
$methodNameType = $scope->getType($args[1]->value);
if (!$methodNameType instanceof ConstantStringType) {
return $this->typeSpecifier->create(
new FuncCall(new FullyQualified('method_exists'), $node->getRawArgs()),
new ConstantBooleanType(true),
$context,
$scope,
);
}
$objectType = $scope->getType($args[0]->value);
if ($objectType->isString()->yes()) {
if ($objectType->isClassString()->yes()) {
$result = $this->typeSpecifier->create(
$args[0]->value,
new IntersectionType([
$objectType,
new HasMethodType($methodNameType->getValue()),
]),
$context,
$scope,
);
if (!$args[0]->value instanceof ClassConstFetch) {
foreach ($objectType->getConstantStrings() as $constantString) {
$className = ltrim($constantString->getValue(), '\\');
if ($className === '') {
continue;
}
$classConstFetch = new Expr\ClassConstFetch(
new FullyQualified($className),
'class',
);
$classConstFetchType = $scope->getType($classConstFetch);
$result = $result->unionWith($this->typeSpecifier->create(
$classConstFetch,
new IntersectionType([
$classConstFetchType,
new HasMethodType($methodNameType->getValue()),
]),
$context,
$scope,
));
}
}
return $result;
}
return new SpecifiedTypes([], []);
}
return $this->typeSpecifier->create(
$args[0]->value,
new UnionType([
new IntersectionType([
new ObjectWithoutClassType(),
new HasMethodType($methodNameType->getValue()),
]),
new ClassStringType(),
]),
$context,
$scope,
);
}
}