Skip to content

Commit d65138a

Browse files
committed
ExtendedPropertyReflection
1 parent 46c163c commit d65138a

29 files changed

+193
-63
lines changed

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use PHPStan\Reflection\ConstantReflection;
6060
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
6161
use PHPStan\Reflection\ExtendedMethodReflection;
62+
use PHPStan\Reflection\ExtendedPropertyReflection;
6263
use PHPStan\Reflection\FunctionReflection;
6364
use PHPStan\Reflection\InitializerExprContext;
6465
use PHPStan\Reflection\InitializerExprTypeResolver;
@@ -5687,7 +5688,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
56875688
}
56885689

56895690
/** @api */
5690-
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?PropertyReflection
5691+
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?ExtendedPropertyReflection
56915692
{
56925693
if ($typeWithProperty instanceof UnionType) {
56935694
$newTypes = [];

src/Analyser/Scope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use PHPStan\Reflection\ClassReflection;
1111
use PHPStan\Reflection\ConstantReflection;
1212
use PHPStan\Reflection\ExtendedMethodReflection;
13+
use PHPStan\Reflection\ExtendedPropertyReflection;
1314
use PHPStan\Reflection\FunctionReflection;
1415
use PHPStan\Reflection\MethodReflection;
1516
use PHPStan\Reflection\NamespaceAnswerer;
1617
use PHPStan\Reflection\ParameterReflection;
1718
use PHPStan\Reflection\ParametersAcceptor;
18-
use PHPStan\Reflection\PropertyReflection;
1919
use PHPStan\TrinaryLogic;
2020
use PHPStan\Type\Type;
2121
use PHPStan\Type\TypeWithClassName;
@@ -71,7 +71,7 @@ public function getDefinedVariables(): array;
7171

7272
public function hasConstant(Name $name): bool;
7373

74-
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?PropertyReflection;
74+
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?ExtendedPropertyReflection;
7575

7676
public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection;
7777

src/Reflection/Annotations/AnnotationPropertyReflection.php

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

55
use PHPStan\Reflection\ClassReflection;
6-
use PHPStan\Reflection\PropertyReflection;
6+
use PHPStan\Reflection\ExtendedPropertyReflection;
77
use PHPStan\TrinaryLogic;
88
use PHPStan\Type\Type;
99

10-
final class AnnotationPropertyReflection implements PropertyReflection
10+
final class AnnotationPropertyReflection implements ExtendedPropertyReflection
1111
{
1212

1313
public function __construct(

src/Reflection/Annotations/AnnotationsPropertiesClassReflectionExtension.php

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

55
use PHPStan\Reflection\ClassReflection;
6+
use PHPStan\Reflection\ExtendedPropertyReflection;
67
use PHPStan\Reflection\PropertiesClassReflectionExtension;
78
use PHPStan\Reflection\PropertyReflection;
89
use PHPStan\Type\Generic\TemplateTypeHelper;
@@ -12,7 +13,7 @@
1213
final class AnnotationsPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension
1314
{
1415

15-
/** @var PropertyReflection[][] */
16+
/** @var ExtendedPropertyReflection[][] */
1617
private array $properties = [];
1718

1819
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
@@ -28,6 +29,9 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
2829
return isset($this->properties[$classReflection->getCacheKey()][$propertyName]);
2930
}
3031

32+
/**
33+
* @return ExtendedPropertyReflection
34+
*/
3135
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
3236
{
3337
return $this->properties[$classReflection->getCacheKey()][$propertyName];
@@ -37,7 +41,7 @@ private function findClassReflectionWithProperty(
3741
ClassReflection $classReflection,
3842
ClassReflection $declaringClass,
3943
string $propertyName,
40-
): ?PropertyReflection
44+
): ?ExtendedPropertyReflection
4145
{
4246
$propertyTags = $classReflection->getPropertyTags();
4347
if (isset($propertyTags[$propertyName])) {

src/Reflection/ClassReflection.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ClassReflection
7979
/** @var ExtendedMethodReflection[] */
8080
private array $methods = [];
8181

82-
/** @var PropertyReflection[] */
82+
/** @var ExtendedPropertyReflection[] */
8383
private array $properties = [];
8484

8585
/** @var ClassConstantReflection[] */
@@ -537,6 +537,15 @@ private function wrapExtendedMethod(MethodReflection $method): ExtendedMethodRef
537537
return new WrappedExtendedMethodReflection($method);
538538
}
539539

540+
private function wrapExtendedProperty(PropertyReflection $method): ExtendedPropertyReflection
541+
{
542+
if ($method instanceof ExtendedPropertyReflection) {
543+
return $method;
544+
}
545+
546+
return new WrappedExtendedPropertyReflection($method);
547+
}
548+
540549
public function hasNativeMethod(string $methodName): bool
541550
{
542551
return $this->getPhpExtension()->hasNativeMethod($this, $methodName);
@@ -619,7 +628,7 @@ public function evictPrivateSymbols(): void
619628
$this->getPhpExtension()->evictPrivateSymbols($this->getCacheKey());
620629
}
621630

622-
public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection
631+
public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
623632
{
624633
if ($this->isEnum()) {
625634
return $this->getNativeProperty($propertyName);
@@ -640,7 +649,7 @@ public function getProperty(string $propertyName, ClassMemberAccessAnswerer $sco
640649
continue;
641650
}
642651

643-
$property = $extension->getProperty($this, $propertyName);
652+
$property = $this->wrapExtendedProperty($extension->getProperty($this, $propertyName));
644653
if ($scope->canAccessProperty($property)) {
645654
return $this->properties[$key] = $property;
646655
}

src/Reflection/Dummy/ChangedTypePropertyReflection.php

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

55
use PHPStan\Reflection\ClassReflection;
6-
use PHPStan\Reflection\PropertyReflection;
6+
use PHPStan\Reflection\ExtendedPropertyReflection;
77
use PHPStan\Reflection\WrapperPropertyReflection;
88
use PHPStan\TrinaryLogic;
99
use PHPStan\Type\Type;
1010

1111
final class ChangedTypePropertyReflection implements WrapperPropertyReflection
1212
{
1313

14-
public function __construct(private ClassReflection $declaringClass, private PropertyReflection $reflection, private Type $readableType, private Type $writableType)
14+
public function __construct(private ClassReflection $declaringClass, private ExtendedPropertyReflection $reflection, private Type $readableType, private Type $writableType)
1515
{
1616
}
1717

@@ -80,7 +80,7 @@ public function isInternal(): TrinaryLogic
8080
return $this->reflection->isInternal();
8181
}
8282

83-
public function getOriginalReflection(): PropertyReflection
83+
public function getOriginalReflection(): ExtendedPropertyReflection
8484
{
8585
return $this->reflection;
8686
}

src/Reflection/Dummy/DummyPropertyReflection.php

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

55
use PHPStan\Reflection\ClassReflection;
6-
use PHPStan\Reflection\PropertyReflection;
6+
use PHPStan\Reflection\ExtendedPropertyReflection;
77
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
88
use PHPStan\TrinaryLogic;
99
use PHPStan\Type\MixedType;
1010
use PHPStan\Type\Type;
1111
use stdClass;
1212

13-
final class DummyPropertyReflection implements PropertyReflection
13+
final class DummyPropertyReflection implements ExtendedPropertyReflection
1414
{
1515

1616
public function getDeclaringClass(): ClassReflection
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection;
4+
5+
/**
6+
* The purpose of this interface is to be able to
7+
* answer more questions about properties
8+
* without breaking backward compatibility
9+
* with existing PropertiesClassReflectionExtension.
10+
*
11+
* Developers are meant to only implement PropertyReflection
12+
* and its methods in their code.
13+
*
14+
* New methods on ExtendedPropertyReflection will be added
15+
* in minor versions.
16+
*
17+
* @api
18+
*/
19+
interface ExtendedPropertyReflection extends PropertyReflection
20+
{
21+
22+
}

src/Reflection/Php/EnumPropertyReflection.php

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

55
use PHPStan\Reflection\ClassReflection;
6-
use PHPStan\Reflection\PropertyReflection;
6+
use PHPStan\Reflection\ExtendedPropertyReflection;
77
use PHPStan\TrinaryLogic;
88
use PHPStan\Type\Type;
99

10-
final class EnumPropertyReflection implements PropertyReflection
10+
final class EnumPropertyReflection implements ExtendedPropertyReflection
1111
{
1212

1313
public function __construct(private ClassReflection $declaringClass, private Type $type)

src/Reflection/Php/EnumUnresolvedPropertyPrototypeReflection.php

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

33
namespace PHPStan\Reflection\Php;
44

5-
use PHPStan\Reflection\PropertyReflection;
5+
use PHPStan\Reflection\ExtendedPropertyReflection;
66
use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
77
use PHPStan\Type\Type;
88

@@ -18,12 +18,12 @@ public function doNotResolveTemplateTypeMapToBounds(): UnresolvedPropertyPrototy
1818
return $this;
1919
}
2020

21-
public function getNakedProperty(): PropertyReflection
21+
public function getNakedProperty(): ExtendedPropertyReflection
2222
{
2323
return $this->property;
2424
}
2525

26-
public function getTransformedProperty(): PropertyReflection
26+
public function getTransformedProperty(): ExtendedPropertyReflection
2727
{
2828
return $this->property;
2929
}

0 commit comments

Comments
 (0)