Skip to content

Commit 41275dc

Browse files
committed
[BCB] ConstantReflection
1 parent c067207 commit 41275dc

36 files changed

+260
-223
lines changed

UPGRADING.md

+5
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,8 @@ Instead of `PHPStanTestCase::createBroker()`, call `PHPStanTestCase::createRefle
293293
* Remove `Type::acceptsWithReason()`, `Type:accepts()` return type changed from `TrinaryLogic` to [`AcceptsResult`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.AcceptsResult.html)
294294
* Remove `CompoundType::isAcceptedWithReasonBy()`, `CompoundType::isAcceptedBy()` return type changed from `TrinaryLogic` to [`AcceptsResult`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.AcceptsResult.html)
295295
* `RuleLevelHelper::accepts()` return type changed from `bool` to [`RuleLevelHelperAcceptsResult`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.AcceptsResult.html)
296+
* Changes around `ClassConstantReflection`
297+
* Class `ClassConstantReflection` removed from BC promise, renamed to `RealClassConstantReflection`
298+
* Interface `ConstantReflection` renamed to `ClassConstantReflection`
299+
* Added more methods around PHPDoc types and native types to the (new) `ClassConstantReflection`
300+
* Interface `GlobalConstantReflection` renamed to `ConstantReflection`

src/Analyser/MutatingScope.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
5353
use PHPStan\Reflection\Callables\SimpleImpurePoint;
5454
use PHPStan\Reflection\Callables\SimpleThrowPoint;
55+
use PHPStan\Reflection\ClassConstantReflection;
5556
use PHPStan\Reflection\ClassMemberReflection;
5657
use PHPStan\Reflection\ClassReflection;
57-
use PHPStan\Reflection\ConstantReflection;
5858
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
5959
use PHPStan\Reflection\ExtendedMethodReflection;
6060
use PHPStan\Reflection\ExtendedPropertyReflection;
@@ -5326,7 +5326,7 @@ public function canCallMethod(MethodReflection $methodReflection): bool
53265326
}
53275327

53285328
/** @api */
5329-
public function canAccessConstant(ConstantReflection $constantReflection): bool
5329+
public function canAccessConstant(ClassConstantReflection $constantReflection): bool
53305330
{
53315331
return $this->canAccessClassMember($constantReflection);
53325332
}
@@ -5690,7 +5690,7 @@ private function propertyFetchType(Type $fetchedOnType, string $propertyName, Ex
56905690
return $propertyReflection->getReadableType();
56915691
}
56925692

5693-
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ConstantReflection
5693+
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ClassConstantReflection
56945694
{
56955695
if ($typeWithConstant instanceof UnionType) {
56965696
$newTypes = [];

src/Analyser/OutOfClassScope.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PHPStan\Analyser;
44

5+
use PHPStan\Reflection\ClassConstantReflection;
56
use PHPStan\Reflection\ClassMemberAccessAnswerer;
67
use PHPStan\Reflection\ClassReflection;
7-
use PHPStan\Reflection\ConstantReflection;
88
use PHPStan\Reflection\MethodReflection;
99
use PHPStan\Reflection\PropertyReflection;
1010

@@ -36,7 +36,7 @@ public function canCallMethod(MethodReflection $methodReflection): bool
3636
return $methodReflection->isPublic();
3737
}
3838

39-
public function canAccessConstant(ConstantReflection $constantReflection): bool
39+
public function canAccessConstant(ClassConstantReflection $constantReflection): bool
4040
{
4141
return $constantReflection->isPublic();
4242
}

src/Analyser/Scope.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Name;
88
use PhpParser\Node\Param;
9+
use PHPStan\Reflection\ClassConstantReflection;
910
use PHPStan\Reflection\ClassMemberAccessAnswerer;
1011
use PHPStan\Reflection\ClassReflection;
11-
use PHPStan\Reflection\ConstantReflection;
1212
use PHPStan\Reflection\ExtendedMethodReflection;
1313
use PHPStan\Reflection\ExtendedPropertyReflection;
1414
use PHPStan\Reflection\FunctionReflection;
@@ -73,7 +73,7 @@ public function getPropertyReflection(Type $typeWithProperty, string $propertyNa
7373

7474
public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection;
7575

76-
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ConstantReflection;
76+
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ClassConstantReflection;
7777

7878
public function getIterableKeyType(Type $iteratee): Type;
7979

src/PhpDoc/PhpDocBlock.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace PHPStan\PhpDoc;
44

55
use PHPStan\PhpDoc\Tag\AssertTagParameter;
6+
use PHPStan\Reflection\ClassConstantReflection;
67
use PHPStan\Reflection\ClassReflection;
7-
use PHPStan\Reflection\ConstantReflection;
88
use PHPStan\Reflection\MethodReflection;
99
use PHPStan\Reflection\Php\PhpMethodReflection;
1010
use PHPStan\Reflection\Php\PhpPropertyReflection;
@@ -334,7 +334,7 @@ private static function resolvePhpDocBlockFromClass(
334334
): ?self
335335
{
336336
if ($classReflection->$hasMethodName($name)) {
337-
/** @var PropertyReflection|MethodReflection|ConstantReflection $parentReflection */
337+
/** @var PropertyReflection|MethodReflection|ClassConstantReflection $parentReflection */
338338
$parentReflection = $classReflection->$getMethodName($name);
339339
if ($parentReflection->isPrivate()) {
340340
return null;

src/Reflection/BetterReflection/BetterReflectionProvider.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
use PHPStan\Reflection\ClassNameHelper;
3535
use PHPStan\Reflection\ClassReflection;
3636
use PHPStan\Reflection\Constant\RuntimeConstantReflection;
37+
use PHPStan\Reflection\ConstantReflection;
3738
use PHPStan\Reflection\FunctionReflection;
3839
use PHPStan\Reflection\FunctionReflectionFactory;
39-
use PHPStan\Reflection\GlobalConstantReflection;
4040
use PHPStan\Reflection\InitializerExprContext;
4141
use PHPStan\Reflection\InitializerExprTypeResolver;
4242
use PHPStan\Reflection\NamespaceAnswerer;
@@ -71,7 +71,7 @@ final class BetterReflectionProvider implements ReflectionProvider
7171
/** @var ClassReflection[] */
7272
private static array $anonymousClasses = [];
7373

74-
/** @var array<string, GlobalConstantReflection> */
74+
/** @var array<string, ConstantReflection> */
7575
private array $cachedConstants = [];
7676

7777
/**
@@ -389,7 +389,7 @@ public function hasConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
389389
return $this->resolveConstantName($nameNode, $namespaceAnswerer) !== null;
390390
}
391391

392-
public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAnswerer): GlobalConstantReflection
392+
public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAnswerer): ConstantReflection
393393
{
394394
$constantName = $this->resolveConstantName($nameNode, $namespaceAnswerer);
395395
if ($constantName === null) {

src/Reflection/ClassConstantReflection.php

+8-127
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,22 @@
33
namespace PHPStan\Reflection;
44

55
use PhpParser\Node\Expr;
6-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClassConstant;
7-
use PHPStan\TrinaryLogic;
86
use PHPStan\Type\Type;
9-
use PHPStan\Type\TypehintHelper;
107

11-
/**
12-
* @api
13-
*/
14-
final class ClassConstantReflection implements ConstantReflection
8+
/** @api */
9+
interface ClassConstantReflection extends ClassMemberReflection, ConstantReflection
1510
{
1611

17-
private ?Type $valueType = null;
12+
public function getValueExpr(): Expr;
1813

19-
public function __construct(
20-
private InitializerExprTypeResolver $initializerExprTypeResolver,
21-
private ClassReflection $declaringClass,
22-
private ReflectionClassConstant $reflection,
23-
private ?Type $nativeType,
24-
private ?Type $phpDocType,
25-
private ?string $deprecatedDescription,
26-
private bool $isDeprecated,
27-
private bool $isInternal,
28-
)
29-
{
30-
}
14+
public function isFinal(): bool;
3115

32-
public function getName(): string
33-
{
34-
return $this->reflection->getName();
35-
}
16+
public function hasPhpDocType(): bool;
3617

37-
public function getFileName(): ?string
38-
{
39-
return $this->declaringClass->getFileName();
40-
}
18+
public function getPhpDocType(): ?Type;
4119

42-
public function getValueExpr(): Expr
43-
{
44-
return $this->reflection->getValueExpression();
45-
}
20+
public function hasNativeType(): bool;
4621

47-
public function hasPhpDocType(): bool
48-
{
49-
return $this->phpDocType !== null;
50-
}
51-
52-
public function getPhpDocType(): ?Type
53-
{
54-
return $this->phpDocType;
55-
}
56-
57-
public function hasNativeType(): bool
58-
{
59-
return $this->nativeType !== null;
60-
}
61-
62-
public function getNativeType(): ?Type
63-
{
64-
return $this->nativeType;
65-
}
66-
67-
public function getValueType(): Type
68-
{
69-
if ($this->valueType === null) {
70-
if ($this->phpDocType !== null) {
71-
if ($this->nativeType !== null) {
72-
return $this->valueType = TypehintHelper::decideType(
73-
$this->nativeType,
74-
$this->phpDocType,
75-
);
76-
}
77-
78-
return $this->phpDocType;
79-
} elseif ($this->nativeType !== null) {
80-
return $this->nativeType;
81-
}
82-
83-
$this->valueType = $this->initializerExprTypeResolver->getType($this->getValueExpr(), InitializerExprContext::fromClassReflection($this->declaringClass));
84-
}
85-
86-
return $this->valueType;
87-
}
88-
89-
public function getDeclaringClass(): ClassReflection
90-
{
91-
return $this->declaringClass;
92-
}
93-
94-
public function isStatic(): bool
95-
{
96-
return true;
97-
}
98-
99-
public function isPrivate(): bool
100-
{
101-
return $this->reflection->isPrivate();
102-
}
103-
104-
public function isPublic(): bool
105-
{
106-
return $this->reflection->isPublic();
107-
}
108-
109-
public function isFinal(): bool
110-
{
111-
return $this->reflection->isFinal();
112-
}
113-
114-
public function isDeprecated(): TrinaryLogic
115-
{
116-
return TrinaryLogic::createFromBoolean($this->isDeprecated);
117-
}
118-
119-
public function getDeprecatedDescription(): ?string
120-
{
121-
if ($this->isDeprecated) {
122-
return $this->deprecatedDescription;
123-
}
124-
125-
return null;
126-
}
127-
128-
public function isInternal(): TrinaryLogic
129-
{
130-
return TrinaryLogic::createFromBoolean($this->isInternal);
131-
}
132-
133-
public function getDocComment(): ?string
134-
{
135-
$docComment = $this->reflection->getDocComment();
136-
if ($docComment === false) {
137-
return null;
138-
}
139-
140-
return $docComment;
141-
}
22+
public function getNativeType(): ?Type;
14223

14324
}

src/Reflection/ClassMemberAccessAnswerer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public function canAccessProperty(PropertyReflection $propertyReflection): bool;
1717

1818
public function canCallMethod(MethodReflection $methodReflection): bool;
1919

20-
public function canAccessConstant(ConstantReflection $constantReflection): bool;
20+
public function canAccessConstant(ClassConstantReflection $constantReflection): bool;
2121

2222
}

src/Reflection/ClassReflection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ final class ClassReflection
8181
/** @var ExtendedPropertyReflection[] */
8282
private array $properties = [];
8383

84-
/** @var ClassConstantReflection[] */
84+
/** @var RealClassClassConstantReflection[] */
8585
private array $constants = [];
8686

8787
/** @var EnumCaseReflection[]|null */
@@ -1082,7 +1082,7 @@ public function getConstant(string $name): ClassConstantReflection
10821082
$nativeType = $this->signatureMapProvider->getClassConstantMetadata($declaringClass->getName(), $name)['nativeType'];
10831083
}
10841084

1085-
$this->constants[$name] = new ClassConstantReflection(
1085+
$this->constants[$name] = new RealClassClassConstantReflection(
10861086
$this->initializerExprTypeResolver,
10871087
$declaringClass,
10881088
$reflectionConstant,

src/Reflection/Constant/RuntimeConstantReflection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace PHPStan\Reflection\Constant;
44

5-
use PHPStan\Reflection\GlobalConstantReflection;
5+
use PHPStan\Reflection\ConstantReflection;
66
use PHPStan\TrinaryLogic;
77
use PHPStan\Type\Type;
88

9-
final class RuntimeConstantReflection implements GlobalConstantReflection
9+
final class RuntimeConstantReflection implements ConstantReflection
1010
{
1111

1212
public function __construct(

src/Reflection/ConstantReflection.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
namespace PHPStan\Reflection;
44

5-
use PhpParser\Node\Expr;
5+
use PHPStan\TrinaryLogic;
6+
use PHPStan\Type\Type;
67

78
/** @api */
8-
interface ConstantReflection extends ClassMemberReflection, GlobalConstantReflection
9+
interface ConstantReflection
910
{
1011

11-
public function getValueExpr(): Expr;
12+
public function getName(): string;
13+
14+
public function getValueType(): Type;
15+
16+
public function isDeprecated(): TrinaryLogic;
17+
18+
public function getDeprecatedDescription(): ?string;
19+
20+
public function isInternal(): TrinaryLogic;
21+
22+
public function getFileName(): ?string;
1223

1324
}

src/Reflection/Dummy/DummyConstantReflection.php renamed to src/Reflection/Dummy/DummyClassConstantReflection.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use PhpParser\Node\Expr;
66
use PHPStan\Node\Expr\TypeExpr;
7+
use PHPStan\Reflection\ClassConstantReflection;
78
use PHPStan\Reflection\ClassReflection;
8-
use PHPStan\Reflection\ConstantReflection;
99
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
1010
use PHPStan\TrinaryLogic;
1111
use PHPStan\Type\MixedType;
1212
use PHPStan\Type\Type;
1313
use stdClass;
1414

15-
final class DummyConstantReflection implements ConstantReflection
15+
final class DummyClassConstantReflection implements ClassConstantReflection
1616
{
1717

1818
public function __construct(private string $name)
@@ -26,6 +26,11 @@ public function getDeclaringClass(): ClassReflection
2626
return $reflectionProvider->getClass(stdClass::class);
2727
}
2828

29+
public function isFinal(): bool
30+
{
31+
return false;
32+
}
33+
2934
public function getFileName(): ?string
3035
{
3136
return null;
@@ -81,4 +86,24 @@ public function getDocComment(): ?string
8186
return null;
8287
}
8388

89+
public function hasPhpDocType(): bool
90+
{
91+
return false;
92+
}
93+
94+
public function getPhpDocType(): ?Type
95+
{
96+
return null;
97+
}
98+
99+
public function hasNativeType(): bool
100+
{
101+
return false;
102+
}
103+
104+
public function getNativeType(): ?Type
105+
{
106+
return null;
107+
}
108+
84109
}

0 commit comments

Comments
 (0)