Skip to content

Commit d0c0938

Browse files
committed
Revert "Remove unneded abstraction"
This reverts commit ad6703d.
1 parent fc23154 commit d0c0938

File tree

7 files changed

+232
-21
lines changed

7 files changed

+232
-21
lines changed

src/Reflection/Native/NativeMethodReflection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace PHPStan\Reflection\Native;
44

5-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
65
use PHPStan\Reflection\Assertions;
76
use PHPStan\Reflection\ClassMemberReflection;
87
use PHPStan\Reflection\ClassReflection;
98
use PHPStan\Reflection\ExtendedMethodReflection;
109
use PHPStan\Reflection\MethodPrototypeReflection;
1110
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
11+
use PHPStan\Reflection\Php\BuiltinMethodReflection;
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use PHPStan\TrinaryLogic;
1414
use PHPStan\Type\Type;
@@ -26,7 +26,7 @@ class NativeMethodReflection implements ExtendedMethodReflection
2626
public function __construct(
2727
private ReflectionProvider $reflectionProvider,
2828
private ClassReflection $declaringClass,
29-
private ReflectionMethod $reflection,
29+
private BuiltinMethodReflection $reflection,
3030
private array $variants,
3131
private ?array $namedArgumentsVariants,
3232
private TrinaryLogic $hasSideEffects,
@@ -116,7 +116,7 @@ public function getDeprecatedDescription(): ?string
116116

117117
public function isDeprecated(): TrinaryLogic
118118
{
119-
return TrinaryLogic::createFromBoolean($this->reflection->isDeprecated());
119+
return $this->reflection->isDeprecated();
120120
}
121121

122122
public function isInternal(): TrinaryLogic
@@ -181,7 +181,7 @@ public function getSelfOutType(): ?Type
181181

182182
public function returnsByReference(): TrinaryLogic
183183
{
184-
return TrinaryLogic::createFromBoolean($this->reflection->returnsReference());
184+
return $this->reflection->returnsByReference();
185185
}
186186

187187
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\Php;
4+
5+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
6+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
7+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionIntersectionType;
8+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
9+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionNamedType;
10+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
11+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionUnionType;
12+
use PHPStan\TrinaryLogic;
13+
14+
interface BuiltinMethodReflection
15+
{
16+
17+
public function getName(): string;
18+
19+
public function getReflection(): ReflectionMethod;
20+
21+
public function getFileName(): ?string;
22+
23+
public function getDeclaringClass(): ReflectionClass|ReflectionEnum;
24+
25+
public function getStartLine(): ?int;
26+
27+
public function getEndLine(): ?int;
28+
29+
public function getDocComment(): ?string;
30+
31+
public function isStatic(): bool;
32+
33+
public function isPrivate(): bool;
34+
35+
public function isPublic(): bool;
36+
37+
public function getPrototype(): self;
38+
39+
public function isDeprecated(): TrinaryLogic;
40+
41+
public function isVariadic(): bool;
42+
43+
public function getReturnType(): ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null;
44+
45+
public function getTentativeReturnType(): ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null;
46+
47+
/**
48+
* @return ReflectionParameter[]
49+
*/
50+
public function getParameters(): array;
51+
52+
public function isFinal(): bool;
53+
54+
public function isInternal(): bool;
55+
56+
public function isAbstract(): bool;
57+
58+
public function returnsByReference(): TrinaryLogic;
59+
60+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\Php;
4+
5+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
6+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
7+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionIntersectionType;
8+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
9+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionNamedType;
10+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
11+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionUnionType;
12+
use PHPStan\TrinaryLogic;
13+
14+
class NativeBuiltinMethodReflection implements BuiltinMethodReflection
15+
{
16+
17+
public function __construct(private ReflectionMethod $reflection)
18+
{
19+
}
20+
21+
public function getName(): string
22+
{
23+
return $this->reflection->getName();
24+
}
25+
26+
public function getReflection(): ReflectionMethod
27+
{
28+
return $this->reflection;
29+
}
30+
31+
public function getFileName(): ?string
32+
{
33+
$fileName = $this->reflection->getFileName();
34+
if ($fileName === false) {
35+
return null;
36+
}
37+
38+
return $fileName;
39+
}
40+
41+
public function getDeclaringClass(): ReflectionClass|ReflectionEnum
42+
{
43+
return $this->reflection->getDeclaringClass();
44+
}
45+
46+
public function getStartLine(): ?int
47+
{
48+
$line = $this->reflection->getStartLine();
49+
if ($line === false) {
50+
return null;
51+
}
52+
53+
return $line;
54+
}
55+
56+
public function getEndLine(): ?int
57+
{
58+
$line = $this->reflection->getEndLine();
59+
if ($line === false) {
60+
return null;
61+
}
62+
63+
return $line;
64+
}
65+
66+
public function getDocComment(): ?string
67+
{
68+
$docComment = $this->reflection->getDocComment();
69+
if ($docComment === false) {
70+
return null;
71+
}
72+
73+
return $docComment;
74+
}
75+
76+
public function isStatic(): bool
77+
{
78+
return $this->reflection->isStatic();
79+
}
80+
81+
public function isPrivate(): bool
82+
{
83+
return $this->reflection->isPrivate();
84+
}
85+
86+
public function isPublic(): bool
87+
{
88+
return $this->reflection->isPublic();
89+
}
90+
91+
public function isConstructor(): bool
92+
{
93+
return $this->reflection->isConstructor();
94+
}
95+
96+
public function getPrototype(): BuiltinMethodReflection
97+
{
98+
return new self($this->reflection->getPrototype());
99+
}
100+
101+
public function isDeprecated(): TrinaryLogic
102+
{
103+
return TrinaryLogic::createFromBoolean($this->reflection->isDeprecated());
104+
}
105+
106+
public function isFinal(): bool
107+
{
108+
return $this->reflection->isFinal();
109+
}
110+
111+
public function isInternal(): bool
112+
{
113+
return $this->reflection->isInternal();
114+
}
115+
116+
public function isAbstract(): bool
117+
{
118+
return $this->reflection->isAbstract();
119+
}
120+
121+
public function isVariadic(): bool
122+
{
123+
return $this->reflection->isVariadic();
124+
}
125+
126+
public function getReturnType(): ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null
127+
{
128+
return $this->reflection->getReturnType();
129+
}
130+
131+
public function getTentativeReturnType(): ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null
132+
{
133+
return $this->reflection->getTentativeReturnType();
134+
}
135+
136+
/**
137+
* @return ReflectionParameter[]
138+
*/
139+
public function getParameters(): array
140+
{
141+
return $this->reflection->getParameters();
142+
}
143+
144+
public function returnsByReference(): TrinaryLogic
145+
{
146+
return TrinaryLogic::createFromBoolean($this->reflection->returnsReference());
147+
}
148+
149+
}

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Analyser\NodeScopeResolver;
1111
use PHPStan\Analyser\ScopeContext;
1212
use PHPStan\Analyser\ScopeFactory;
13-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
1413
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
1514
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
1615
use PHPStan\Parser\Parser;
@@ -381,7 +380,7 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
381380
return $this->methodsIncludingAnnotations[$classReflection->getCacheKey()][$methodName];
382381
}
383382

384-
$nativeMethodReflection = $classReflection->getNativeReflection()->getMethod($methodName);
383+
$nativeMethodReflection = new NativeBuiltinMethodReflection($classReflection->getNativeReflection()->getMethod($methodName));
385384
if (!isset($this->methodsIncludingAnnotations[$classReflection->getCacheKey()][$nativeMethodReflection->getName()])) {
386385
$method = $this->createMethod($classReflection, $nativeMethodReflection, true);
387386
$this->methodsIncludingAnnotations[$classReflection->getCacheKey()][$nativeMethodReflection->getName()] = $method;
@@ -408,7 +407,8 @@ public function getNativeMethod(ClassReflection $classReflection, string $method
408407
throw new ShouldNotHappenException();
409408
}
410409

411-
$nativeMethodReflection = $classReflection->getNativeReflection()->getMethod($methodName);
410+
$reflectionMethod = $classReflection->getNativeReflection()->getMethod($methodName);
411+
$nativeMethodReflection = new NativeBuiltinMethodReflection($reflectionMethod);
412412

413413
if (!isset($this->nativeMethods[$classReflection->getCacheKey()][$nativeMethodReflection->getName()])) {
414414
$method = $this->createMethod($classReflection, $nativeMethodReflection, false);
@@ -420,7 +420,7 @@ public function getNativeMethod(ClassReflection $classReflection, string $method
420420

421421
private function createMethod(
422422
ClassReflection $classReflection,
423-
ReflectionMethod $methodReflection,
423+
BuiltinMethodReflection $methodReflection,
424424
bool $includingAnnotations,
425425
): ExtendedMethodReflection
426426
{
@@ -621,14 +621,14 @@ private function createMethod(
621621
return $this->createUserlandMethodReflection($declaringClass, $declaringClass, $methodReflection);
622622
}
623623

624-
public function createUserlandMethodReflection(ClassReflection $fileDeclaringClass, ClassReflection $actualDeclaringClass, ReflectionMethod $methodReflection): PhpMethodReflection
624+
public function createUserlandMethodReflection(ClassReflection $fileDeclaringClass, ClassReflection $actualDeclaringClass, BuiltinMethodReflection $methodReflection): PhpMethodReflection
625625
{
626626
$declaringTraitName = $this->findMethodTrait($methodReflection);
627627
$resolvedPhpDoc = null;
628628
$stubPhpDocPair = $this->findMethodPhpDocIncludingAncestors($fileDeclaringClass, $methodReflection->getName(), array_map(static fn (ReflectionParameter $parameter): string => $parameter->getName(), $methodReflection->getParameters()));
629629
$phpDocBlockClassReflection = $fileDeclaringClass;
630630

631-
$methodDeclaringClass = $methodReflection->getBetterReflection()->getDeclaringClass();
631+
$methodDeclaringClass = $methodReflection->getReflection()->getBetterReflection()->getDeclaringClass();
632632

633633
if ($stubPhpDocPair === null && $methodDeclaringClass->isTrait()) {
634634
if (! $methodReflection->getDeclaringClass()->isTrait() || $methodDeclaringClass->getName() !== $methodReflection->getDeclaringClass()->getName()) {
@@ -648,7 +648,7 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
648648
}
649649

650650
if ($resolvedPhpDoc === null) {
651-
$docComment = $methodReflection->getDocComment() !== false ? $methodReflection->getDocComment() : null;
651+
$docComment = $methodReflection->getDocComment();
652652
$positionalParameterNames = array_map(static fn (ReflectionParameter $parameter): string => $parameter->getName(), $methodReflection->getParameters());
653653

654654
$resolvedPhpDoc = $this->phpDocInheritanceResolver->resolvePhpDocForMethod(
@@ -671,7 +671,10 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
671671
}
672672

673673
$phpDocParameterTypes = [];
674-
if ($methodReflection->isConstructor()) {
674+
if (
675+
$methodReflection instanceof NativeBuiltinMethodReflection
676+
&& $methodReflection->isConstructor()
677+
) {
675678
foreach ($methodReflection->getParameters() as $parameter) {
676679
if (!$parameter->isPromoted()) {
677680
continue;
@@ -862,10 +865,10 @@ private function findPropertyTrait(ReflectionProperty $propertyReflection): ?str
862865
}
863866

864867
private function findMethodTrait(
865-
ReflectionMethod $methodReflection,
868+
BuiltinMethodReflection $methodReflection,
866869
): ?string
867870
{
868-
$declaringClass = $methodReflection->getBetterReflection()->getDeclaringClass();
871+
$declaringClass = $methodReflection->getReflection()->getBetterReflection()->getDeclaringClass();
869872
if ($declaringClass->isTrait()) {
870873
if ($methodReflection->getDeclaringClass()->isTrait() && $declaringClass->getName() === $methodReflection->getDeclaringClass()->getName()) {
871874
return null;

src/Reflection/Php/PhpMethodReflection.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node\Stmt\Declare_;
88
use PhpParser\Node\Stmt\Function_;
99
use PhpParser\Node\Stmt\Namespace_;
10-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
1110
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
1211
use PHPStan\Cache\Cache;
1312
use PHPStan\Parser\FunctionCallStatementFinder;
@@ -67,7 +66,7 @@ public function __construct(
6766
private InitializerExprTypeResolver $initializerExprTypeResolver,
6867
private ClassReflection $declaringClass,
6968
private ?ClassReflection $declaringTrait,
70-
private ReflectionMethod $reflection,
69+
private BuiltinMethodReflection $reflection,
7170
private ReflectionProvider $reflectionProvider,
7271
private Parser $parser,
7372
private FunctionCallStatementFinder $functionCallStatementFinder,
@@ -391,7 +390,7 @@ public function isDeprecated(): TrinaryLogic
391390
return TrinaryLogic::createYes();
392391
}
393392

394-
return TrinaryLogic::createFromBoolean($this->reflection->isDeprecated());
393+
return $this->reflection->isDeprecated();
395394
}
396395

397396
public function isInternal(): TrinaryLogic
@@ -451,7 +450,7 @@ public function getDocComment(): ?string
451450

452451
public function returnsByReference(): TrinaryLogic
453452
{
454-
return TrinaryLogic::createFromBoolean($this->reflection->returnsReference());
453+
return $this->reflection->returnsByReference();
455454
}
456455

457456
}

src/Reflection/Php/PhpMethodReflectionFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPStan\Reflection\Php;
44

5-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
65
use PHPStan\Reflection\Assertions;
76
use PHPStan\Reflection\ClassReflection;
87
use PHPStan\Type\Generic\TemplateTypeMap;
@@ -18,7 +17,7 @@ interface PhpMethodReflectionFactory
1817
public function create(
1918
ClassReflection $declaringClass,
2019
?ClassReflection $declaringTrait,
21-
ReflectionMethod $reflection,
20+
BuiltinMethodReflection $reflection,
2221
TemplateTypeMap $templateTypeMap,
2322
array $phpDocParameterTypes,
2423
?Type $phpDocReturnType,

0 commit comments

Comments
 (0)