/
PhpMethodFromParserNodeReflection.php
154 lines (139 loc) · 3.56 KB
/
PhpMethodFromParserNodeReflection.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Php;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VoidType;
use function strtolower;
/**
* @api
*/
class PhpMethodFromParserNodeReflection extends PhpFunctionFromParserNodeReflection implements ExtendedMethodReflection
{
/**
* @param Type[] $realParameterTypes
* @param Type[] $phpDocParameterTypes
* @param Type[] $realParameterDefaultValues
*/
public function __construct(
private ClassReflection $declaringClass,
ClassMethod $classMethod,
string $fileName,
TemplateTypeMap $templateTypeMap,
array $realParameterTypes,
array $phpDocParameterTypes,
array $realParameterDefaultValues,
Type $realReturnType,
?Type $phpDocReturnType,
?Type $throwType,
?string $deprecatedDescription,
bool $isDeprecated,
bool $isInternal,
bool $isFinal,
?bool $isPure,
bool $acceptsNamedArguments,
Assertions $assertions,
private ?Type $selfOutType,
?string $phpDocComment,
array $parameterOutTypes,
)
{
$name = strtolower($classMethod->name->name);
if (
$name === '__construct'
|| $name === '__destruct'
|| $name === '__unset'
|| $name === '__wakeup'
|| $name === '__clone'
) {
$realReturnType = new VoidType();
}
if ($name === '__tostring') {
$realReturnType = new StringType();
}
if ($name === '__isset') {
$realReturnType = new BooleanType();
}
if ($name === '__sleep') {
$realReturnType = new ArrayType(new IntegerType(), new StringType());
}
if ($name === '__set_state') {
$realReturnType = TypeCombinator::intersect(new ObjectWithoutClassType(), $realReturnType);
}
parent::__construct(
$classMethod,
$fileName,
$templateTypeMap,
$realParameterTypes,
$phpDocParameterTypes,
$realParameterDefaultValues,
$realReturnType,
$phpDocReturnType,
$throwType,
$deprecatedDescription,
$isDeprecated,
$isInternal,
$isFinal || $classMethod->isFinal(),
$isPure,
$acceptsNamedArguments,
$assertions,
$phpDocComment,
$parameterOutTypes,
);
}
public function getDeclaringClass(): ClassReflection
{
return $this->declaringClass;
}
public function getPrototype(): ClassMemberReflection
{
try {
return $this->declaringClass->getNativeMethod($this->getClassMethod()->name->name)->getPrototype();
} catch (MissingMethodFromReflectionException) {
return $this;
}
}
private function getClassMethod(): ClassMethod
{
/** @var Node\Stmt\ClassMethod $functionLike */
$functionLike = $this->getFunctionLike();
return $functionLike;
}
public function isStatic(): bool
{
return $this->getClassMethod()->isStatic();
}
public function isPrivate(): bool
{
return $this->getClassMethod()->isPrivate();
}
public function isPublic(): bool
{
return $this->getClassMethod()->isPublic();
}
public function isBuiltin(): bool
{
return false;
}
public function getSelfOutType(): ?Type
{
return $this->selfOutType;
}
public function returnsByReference(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->getClassMethod()->returnsByRef());
}
}