Skip to content

Commit 3024c02

Browse files
committed
MethodReflection - fix isInternal vs. isBuiltin
1 parent 67fb7a6 commit 3024c02

15 files changed

+99
-5
lines changed

src/Reflection/Annotations/AnnotationMethodReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public function isInternal(): TrinaryLogic
119119
return TrinaryLogic::createNo();
120120
}
121121

122+
public function isBuiltin(): TrinaryLogic
123+
{
124+
return TrinaryLogic::createNo();
125+
}
126+
122127
public function getThrowType(): ?Type
123128
{
124129
return $this->throwType;

src/Reflection/Dummy/ChangedTypeMethodReflection.php

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ public function isInternal(): TrinaryLogic
110110
return $this->reflection->isInternal();
111111
}
112112

113+
public function isBuiltin(): TrinaryLogic
114+
{
115+
$builtin = $this->reflection->isBuiltin();
116+
if (is_bool($builtin)) {
117+
return TrinaryLogic::createFromBoolean($builtin);
118+
}
119+
120+
return $builtin;
121+
}
122+
113123
public function getThrowType(): ?Type
114124
{
115125
return $this->reflection->getThrowType();

src/Reflection/Dummy/DummyConstructorReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public function isInternal(): TrinaryLogic
9797
return TrinaryLogic::createMaybe();
9898
}
9999

100+
public function isBuiltin(): TrinaryLogic
101+
{
102+
return TrinaryLogic::createMaybe();
103+
}
104+
100105
public function getThrowType(): ?Type
101106
{
102107
return null;

src/Reflection/Dummy/DummyMethodReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public function isInternal(): TrinaryLogic
9494
return TrinaryLogic::createMaybe();
9595
}
9696

97+
public function isBuiltin(): TrinaryLogic
98+
{
99+
return TrinaryLogic::createMaybe();
100+
}
101+
97102
public function getThrowType(): ?Type
98103
{
99104
return null;

src/Reflection/ExtendedMethodReflection.php

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function isFinalByKeyword(): TrinaryLogic;
4949

5050
public function isAbstract(): TrinaryLogic|bool;
5151

52+
public function isBuiltin(): TrinaryLogic|bool;
53+
5254
/**
5355
* This indicates whether the method has phpstan-pure
5456
* or phpstan-impure annotation above it.

src/Reflection/Native/NativeMethodReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public function isDeprecated(): TrinaryLogic
140140
}
141141

142142
public function isInternal(): TrinaryLogic
143+
{
144+
return TrinaryLogic::createNo();
145+
}
146+
147+
public function isBuiltin(): TrinaryLogic
143148
{
144149
return TrinaryLogic::createFromBoolean($this->reflection->isInternal());
145150
}

src/Reflection/Php/ClosureCallMethodReflection.php

+10
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ public function isInternal(): TrinaryLogic
142142
return $this->nativeMethodReflection->isInternal();
143143
}
144144

145+
public function isBuiltin(): TrinaryLogic
146+
{
147+
$builtin = $this->nativeMethodReflection->isBuiltin();
148+
if (is_bool($builtin)) {
149+
return TrinaryLogic::createFromBoolean($builtin);
150+
}
151+
152+
return $builtin;
153+
}
154+
145155
public function getThrowType(): ?Type
146156
{
147157
return $this->nativeMethodReflection->getThrowType();

src/Reflection/Php/EnumCasesMethodReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public function isInternal(): TrinaryLogic
111111
return TrinaryLogic::createNo();
112112
}
113113

114+
public function isBuiltin(): TrinaryLogic
115+
{
116+
return TrinaryLogic::createYes();
117+
}
118+
114119
public function getThrowType(): ?Type
115120
{
116121
return null;

src/Reflection/Php/PhpMethodReflection.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,12 @@ public function isDeprecated(): TrinaryLogic
379379

380380
public function isInternal(): TrinaryLogic
381381
{
382-
return TrinaryLogic::createFromBoolean($this->isInternal || $this->reflection->isInternal());
382+
return TrinaryLogic::createFromBoolean($this->isInternal);
383+
}
384+
385+
public function isBuiltin(): TrinaryLogic
386+
{
387+
return TrinaryLogic::createFromBoolean($this->reflection->isInternal());
383388
}
384389

385390
public function isFinal(): TrinaryLogic

src/Reflection/ResolvedMethodReflection.php

+10
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public function isInternal(): TrinaryLogic
150150
return $this->reflection->isInternal();
151151
}
152152

153+
public function isBuiltin(): TrinaryLogic
154+
{
155+
$builtin = $this->reflection->isBuiltin();
156+
if (is_bool($builtin)) {
157+
return TrinaryLogic::createFromBoolean($builtin);
158+
}
159+
160+
return $builtin;
161+
}
162+
153163
public function getThrowType(): ?Type
154164
{
155165
return $this->reflection->getThrowType();

src/Reflection/Type/IntersectionTypeMethodReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public function isInternal(): TrinaryLogic
152152
return TrinaryLogic::lazyMaxMin($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->isInternal());
153153
}
154154

155+
public function isBuiltin(): TrinaryLogic
156+
{
157+
return TrinaryLogic::lazyMaxMin($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => is_bool($method->isBuiltin()) ? TrinaryLogic::createFromBoolean($method->isBuiltin()) : $method->isBuiltin());
158+
}
159+
155160
public function getThrowType(): ?Type
156161
{
157162
$types = [];

src/Reflection/Type/UnionTypeMethodReflection.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ public function isFinalByKeyword(): TrinaryLogic
132132

133133
public function isInternal(): TrinaryLogic
134134
{
135-
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->isInternal());
135+
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => $method->isInternal());
136+
}
137+
138+
public function isBuiltin(): TrinaryLogic
139+
{
140+
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => is_bool($method->isBuiltin()) ? TrinaryLogic::createFromBoolean($method->isBuiltin()) : $method->isBuiltin());
136141
}
137142

138143
public function getThrowType(): ?Type

src/Reflection/WrappedExtendedMethodReflection.php

+5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public function isInternal(): TrinaryLogic
123123
return $this->method->isInternal();
124124
}
125125

126+
public function isBuiltin(): TrinaryLogic
127+
{
128+
return TrinaryLogic::createNo();
129+
}
130+
126131
public function getThrowType(): ?Type
127132
{
128133
return $this->method->getThrowType();

src/Rules/Methods/OverridingMethodRule.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ public function processNode(Node $node, Scope $scope): array
214214
$prototypeReturnType = $prototypeVariant->getNativeReturnType();
215215
$reportReturnType = true;
216216
if ($this->phpVersion->hasTentativeReturnTypes()) {
217-
$reportReturnType = !$realPrototype instanceof MethodPrototypeReflection || $realPrototype->getTentativeReturnType() === null || $prototype->isInternal()->no();
217+
$reportReturnType = !$realPrototype instanceof MethodPrototypeReflection
218+
|| $realPrototype->getTentativeReturnType() === null
219+
|| (is_bool($prototype->isBuiltin()) ? !$prototype->isBuiltin() : $prototype->isBuiltin()->no());
218220
} else {
219221
if ($realPrototype instanceof MethodPrototypeReflection && $realPrototype->isInternal()) {
220-
if ($prototype->isInternal()->yes() && $prototypeDeclaringClass->getName() !== $realPrototype->getDeclaringClass()->getName()) {
222+
if (
223+
(is_bool($prototype->isBuiltin()) ? $prototype->isBuiltin() : $prototype->isBuiltin()->yes())
224+
&& $prototypeDeclaringClass->getName() !== $realPrototype->getDeclaringClass()->getName()
225+
) {
221226
$realPrototypeVariant = $realPrototype->getVariants()[0];
222227
if (
223228
$prototypeReturnType instanceof MixedType
@@ -228,7 +233,10 @@ public function processNode(Node $node, Scope $scope): array
228233
}
229234
}
230235

231-
if ($reportReturnType && $prototype->isInternal()->yes()) {
236+
if (
237+
$reportReturnType
238+
&& (is_bool($prototype->isBuiltin()) ? $prototype->isBuiltin() : $prototype->isBuiltin()->yes())
239+
) {
232240
$reportReturnType = !$this->hasReturnTypeWillChangeAttribute($node->getOriginalNode());
233241
}
234242
}

tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use function get_defined_functions;
2929
use function getenv;
3030
use function implode;
31+
use function is_bool;
3132
use function mkdir;
3233
use function sort;
3334
use function strpos;
@@ -349,6 +350,14 @@ private static function generateFunctionMethodBaseDescription($reflection): stri
349350
$result .= 'Is internal: ' . $reflection->isInternal()->describe() . "\n";
350351
}
351352

353+
if (is_bool($reflection->isBuiltin()) && $reflection->isBuiltin()) {
354+
$result .= 'Is built-in' . "\n";
355+
}
356+
357+
if (!is_bool($reflection->isBuiltin()) && !$reflection->isBuiltin()->no()) {
358+
$result .= 'Is built-in: ' . $reflection->isBuiltin()->describe() . "\n";
359+
}
360+
352361
if (! $reflection->returnsByReference()->no()) {
353362
$result .= 'Returns by reference: ' . $reflection->returnsByReference()->describe() . "\n";
354363
}

0 commit comments

Comments
 (0)