Skip to content

Commit ef60339

Browse files
committed
ClassReflection - the reflection is always BetterReflection adapter
1 parent 5ea642c commit ef60339

19 files changed

Lines changed: 78 additions & 347 deletions

build/baseline-8.0.neon

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,6 @@ parameters:
2020
count: 1
2121
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php
2222

23-
-
24-
message: "#^Call to function method_exists\\(\\) with ReflectionProperty and 'isPromoted' will always evaluate to true\\.$#"
25-
paths:
26-
- ../src/Reflection/Php/PhpClassReflectionExtension.php
27-
- ../src/Reflection/Php/PhpPropertyReflection.php
28-
29-
-
30-
message: "#^Call to function method_exists\\(\\) with ReflectionProperty and 'getDefaultValue' will always evaluate to true\\.$#"
31-
paths:
32-
- ../tests/PHPStan/Analyser/AnalyserIntegrationTest.php
33-
34-
-
35-
message: "#^Call to function method_exists\\(\\) with ReflectionParameter and 'isPromoted' will always evaluate to true\\.$#"
36-
paths:
37-
- ../src/Reflection/Php/PhpClassReflectionExtension.php
38-
39-
-
40-
message: "#^Call to function method_exists\\(\\) with ReflectionClass and 'getAttributes' will always evaluate to true\\.$#"
41-
count: 1
42-
path: ../src/Reflection/ClassReflection.php
43-
4423
-
4524
message: "#^Strict comparison using \\=\\=\\= between array and false will always evaluate to false\\.$#"
4625
count: 1

build/baseline-8.1.neon

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,2 @@
11
parameters:
2-
ignoreErrors:
3-
-
4-
message: "#^Call to function method_exists\\(\\) with ReflectionClassConstant and 'isFinal' will always evaluate to true\\.$#"
5-
count: 1
6-
path: ../src/Reflection/ClassConstantReflection.php
7-
8-
-
9-
message: "#^Call to function method_exists\\(\\) with ReflectionClass and 'isEnum' will always evaluate to true\\.$#"
10-
count: 1
11-
path: ../src/Reflection/ClassReflection.php
12-
13-
-
14-
message: "#^Call to function method_exists\\(\\) with ReflectionMethod and 'getTentativeReturnT…' will always evaluate to true\\.$#"
15-
count: 1
16-
path: ../src/Reflection/Php/NativeBuiltinMethodReflection.php
17-
18-
-
19-
message: "#^Call to function method_exists\\(\\) with ReflectionProperty and 'isReadOnly' will always evaluate to true\\.$#"
20-
count: 1
21-
path: ../src/Reflection/Php/PhpPropertyReflection.php
22-
2+
ignoreErrors: []

build/ignore-gte-php7.4-errors.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ includes:
33

44
parameters:
55
ignoreErrors:
6-
-
7-
message: "#^Call to function method_exists\\(\\) with ReflectionProperty and '(?:hasType|getType)' will always evaluate to true\\.$#"
8-
path: ../src/Reflection/Php/PhpClassReflectionExtension.php
96
- '#^Class PHPStan\\Rules\\RuleErrors\\RuleError(?:\d+) has an uninitialized property (?:\$message|\$line|\$identifier|\$tip|\$file|\$metadata)#'
107
- '#Extension has an uninitialized property (?:\$typeSpecifier|\$broker)#'
118
-

phpstan-baseline.neon

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ parameters:
1616
path: src/Analyser/NodeScopeResolver.php
1717

1818
-
19-
message: "#^Parameter \\#9 \\$reflection of class PHPStan\\\\Reflection\\\\ClassReflection constructor expects ReflectionClass, object given\\.$#"
19+
message: "#^Parameter \\#9 \\$reflection of class PHPStan\\\\Reflection\\\\ClassReflection constructor expects PHPStan\\\\BetterReflection\\\\Reflection\\\\Adapter\\\\ReflectionClass\\|PHPStan\\\\BetterReflection\\\\Reflection\\\\Adapter\\\\ReflectionEnum, object given\\.$#"
2020
count: 1
2121
path: src/Analyser/NodeScopeResolver.php
2222

@@ -139,7 +139,7 @@ parameters:
139139
path: src/Reflection/BetterReflection/BetterReflectionProvider.php
140140

141141
-
142-
message: "#^Parameter \\#9 \\$reflection of class PHPStan\\\\Reflection\\\\ClassReflection constructor expects ReflectionClass, object given\\.$#"
142+
message: "#^Parameter \\#9 \\$reflection of class PHPStan\\\\Reflection\\\\ClassReflection constructor expects PHPStan\\\\BetterReflection\\\\Reflection\\\\Adapter\\\\ReflectionClass\\|PHPStan\\\\BetterReflection\\\\Reflection\\\\Adapter\\\\ReflectionEnum, object given\\.$#"
143143
count: 1
144144
path: src/Reflection/BetterReflection/BetterReflectionProvider.php
145145

@@ -173,61 +173,11 @@ parameters:
173173
count: 1
174174
path: src/Reflection/BetterReflection/SourceLocator/PhpFileCleaner.php
175175

176-
-
177-
message: "#^Method PHPStan\\\\Reflection\\\\ClassReflection\\:\\:__construct\\(\\) has parameter \\$reflection with generic class ReflectionClass but does not specify its types\\: T$#"
178-
count: 1
179-
path: src/Reflection/ClassReflection.php
180-
181176
-
182177
message: "#^Method PHPStan\\\\Reflection\\\\ClassReflection\\:\\:getCacheKey\\(\\) should return string but returns string\\|null\\.$#"
183178
count: 1
184179
path: src/Reflection/ClassReflection.php
185180

186-
-
187-
message: "#^Method PHPStan\\\\Reflection\\\\ClassReflection\\:\\:getNativeReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
188-
count: 1
189-
path: src/Reflection/ClassReflection.php
190-
191-
-
192-
message: "#^Property PHPStan\\\\Reflection\\\\ClassReflection\\:\\:\\$reflection with generic class ReflectionClass does not specify its types\\: T$#"
193-
count: 1
194-
path: src/Reflection/ClassReflection.php
195-
196-
-
197-
message: "#^Method PHPStan\\\\Reflection\\\\Php\\\\BuiltinMethodReflection\\:\\:getDeclaringClass\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
198-
count: 1
199-
path: src/Reflection/Php/BuiltinMethodReflection.php
200-
201-
-
202-
message: "#^Method PHPStan\\\\Reflection\\\\Php\\\\FakeBuiltinMethodReflection\\:\\:__construct\\(\\) has parameter \\$declaringClass with generic class ReflectionClass but does not specify its types\\: T$#"
203-
count: 1
204-
path: src/Reflection/Php/FakeBuiltinMethodReflection.php
205-
206-
-
207-
message: "#^Method PHPStan\\\\Reflection\\\\Php\\\\FakeBuiltinMethodReflection\\:\\:getDeclaringClass\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
208-
count: 1
209-
path: src/Reflection/Php/FakeBuiltinMethodReflection.php
210-
211-
-
212-
message: "#^Property PHPStan\\\\Reflection\\\\Php\\\\FakeBuiltinMethodReflection\\:\\:\\$declaringClass with generic class ReflectionClass does not specify its types\\: T$#"
213-
count: 1
214-
path: src/Reflection/Php/FakeBuiltinMethodReflection.php
215-
216-
-
217-
message: "#^Method PHPStan\\\\Reflection\\\\Php\\\\NativeBuiltinMethodReflection\\:\\:getDeclaringClass\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
218-
count: 1
219-
path: src/Reflection/Php/NativeBuiltinMethodReflection.php
220-
221-
-
222-
message: "#^Method PHPStan\\\\Reflection\\\\Php\\\\PhpClassReflectionExtension\\:\\:collectTraits\\(\\) has parameter \\$class with generic class ReflectionClass but does not specify its types\\: T$#"
223-
count: 1
224-
path: src/Reflection/Php/PhpClassReflectionExtension.php
225-
226-
-
227-
message: "#^Method PHPStan\\\\Reflection\\\\Php\\\\PhpClassReflectionExtension\\:\\:collectTraits\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
228-
count: 1
229-
path: src/Reflection/Php/PhpClassReflectionExtension.php
230-
231181
-
232182
message: "#^Creating new PHPStan\\\\Php8StubsMap is not covered by backward compatibility promise\\. The class might change in a minor PHPStan version\\.$#"
233183
count: 1

src/Reflection/BetterReflection/BetterReflectionProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
1111
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
1212
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction;
13+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
1314
use PHPStan\BetterReflection\Reflection\Exception\NotAClassReflection;
1415
use PHPStan\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
1516
use PHPStan\BetterReflection\Reflection\ReflectionEnum;
@@ -43,7 +44,6 @@
4344
use PHPStan\Type\Generic\TemplateTypeMap;
4445
use PHPStan\Type\MixedType;
4546
use PHPStan\Type\Type;
46-
use ReflectionParameter;
4747
use function array_key_exists;
4848
use function array_map;
4949
use function base64_decode;

src/Reflection/ClassConstantReflection.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
namespace PHPStan\Reflection;
44

55
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
6-
use PHPStan\Php\PhpVersion;
6+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClassConstant;
77
use PHPStan\TrinaryLogic;
88
use PHPStan\Type\ConstantTypeHelper;
99
use PHPStan\Type\Type;
10-
use ReflectionClassConstant;
11-
use function method_exists;
1210
use const NAN;
1311

1412
class ClassConstantReflection implements ConstantReflection
@@ -20,7 +18,6 @@ public function __construct(
2018
private ClassReflection $declaringClass,
2119
private ReflectionClassConstant $reflection,
2220
private ?Type $phpDocType,
23-
private PhpVersion $phpVersion,
2421
private ?string $deprecatedDescription,
2522
private bool $isDeprecated,
2623
private bool $isInternal,
@@ -90,15 +87,7 @@ public function isPublic(): bool
9087

9188
public function isFinal(): bool
9289
{
93-
if (method_exists($this->reflection, 'isFinal')) {
94-
return $this->reflection->isFinal();
95-
}
96-
97-
if (!$this->phpVersion->isInterfaceConstantImplicitlyFinal()) {
98-
return false;
99-
}
100-
101-
return $this->declaringClass->isInterface();
90+
return $this->reflection->isFinal();
10291
}
10392

10493
public function isDeprecated(): TrinaryLogic

src/Reflection/ClassReflection.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace PHPStan\Reflection;
44

55
use Attribute;
6+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
7+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
8+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase;
9+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
610
use PHPStan\Php\PhpVersion;
711
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
812
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
@@ -31,11 +35,7 @@
3135
use PHPStan\Type\TypeAlias;
3236
use PHPStan\Type\TypehintHelper;
3337
use PHPStan\Type\VerbosityLevel;
34-
use ReflectionClass;
35-
use ReflectionEnum;
36-
use ReflectionEnumBackedCase;
3738
use ReflectionException;
38-
use ReflectionMethod;
3939
use function array_diff;
4040
use function array_filter;
4141
use function array_key_exists;
@@ -49,7 +49,6 @@
4949
use function in_array;
5050
use function is_bool;
5151
use function is_file;
52-
use function method_exists;
5352
use function reset;
5453
use function sprintf;
5554
use function strtolower;
@@ -120,7 +119,7 @@ public function __construct(
120119
private array $propertiesClassReflectionExtensions,
121120
private array $methodsClassReflectionExtensions,
122121
private string $displayName,
123-
private ReflectionClass $reflection,
122+
private ReflectionClass|ReflectionEnum $reflection,
124123
private ?string $anonymousFilename,
125124
private ?TemplateTypeMap $resolvedTemplateTypeMap,
126125
private ?ResolvedPhpDocBlock $stubPhpDocBlock,
@@ -129,7 +128,7 @@ public function __construct(
129128
{
130129
}
131130

132-
public function getNativeReflection(): ReflectionClass
131+
public function getNativeReflection(): ReflectionClass|ReflectionEnum
133132
{
134133
return $this->reflection;
135134
}
@@ -304,10 +303,9 @@ public function getClassHierarchyDistances(): array
304303
}
305304

306305
/**
307-
* @param ReflectionClass<object> $class
308-
* @return ReflectionClass<object>[]
306+
* @return ReflectionClass[]
309307
*/
310-
private function collectTraits(ReflectionClass $class): array
308+
private function collectTraits(ReflectionClass|ReflectionEnum $class): array
311309
{
312310
$traits = [];
313311
$traitsLeftToAnalyze = $class->getTraits();
@@ -526,11 +524,7 @@ public function isTrait(): bool
526524

527525
public function isEnum(): bool
528526
{
529-
if (method_exists($this->reflection, 'isEnum')) {
530-
return $this->reflection->isEnum();
531-
}
532-
533-
return false;
527+
return $this->reflection->isEnum();
534528
}
535529

536530
public function isBackedEnum(): bool
@@ -566,7 +560,7 @@ public function hasEnumCase(string $name): bool
566560
return false;
567561
}
568562

569-
if (!method_exists($this->reflection, 'hasCase')) {
563+
if (!$this->reflection instanceof ReflectionEnum) {
570564
return false;
571565
}
572566

@@ -889,7 +883,6 @@ public function getConstant(string $name): ConstantReflection
889883
$declaringClass,
890884
$reflectionConstant,
891885
$phpDocType,
892-
$this->phpVersion,
893886
$deprecatedDescription,
894887
$isDeprecated,
895888
$isInternal,
@@ -1042,10 +1035,6 @@ private function findAttributeClass(): ?Attribute
10421035
return null;
10431036
}
10441037

1045-
if (!method_exists($this->reflection, 'getAttributes')) {
1046-
return null;
1047-
}
1048-
10491038
$nativeAttributes = $this->reflection->getAttributes(Attribute::class);
10501039
if (count($nativeAttributes) === 1) {
10511040
/** @var Attribute */

src/Reflection/FunctionReflectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace PHPStan\Reflection;
44

5+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction;
56
use PHPStan\Reflection\Php\PhpFunctionReflection;
67
use PHPStan\Type\Generic\TemplateTypeMap;
78
use PHPStan\Type\Type;
8-
use ReflectionFunction;
99

1010
interface FunctionReflectionFactory
1111
{

src/Reflection/Php/BuiltinMethodReflection.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PHPStan\Reflection\Php;
44

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;
512
use PHPStan\TrinaryLogic;
6-
use ReflectionClass;
7-
use ReflectionMethod;
8-
use ReflectionParameter;
9-
use ReflectionType;
1013

1114
interface BuiltinMethodReflection
1215
{
@@ -17,7 +20,7 @@ public function getReflection(): ?ReflectionMethod;
1720

1821
public function getFileName(): ?string;
1922

20-
public function getDeclaringClass(): ReflectionClass;
23+
public function getDeclaringClass(): ReflectionClass|ReflectionEnum;
2124

2225
public function getStartLine(): ?int;
2326

@@ -37,9 +40,9 @@ public function isDeprecated(): TrinaryLogic;
3740

3841
public function isVariadic(): bool;
3942

40-
public function getReturnType(): ?ReflectionType;
43+
public function getReturnType(): ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null;
4144

42-
public function getTentativeReturnType(): ?ReflectionType;
45+
public function getTentativeReturnType(): ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null;
4346

4447
/**
4548
* @return ReflectionParameter[]

0 commit comments

Comments
 (0)