Skip to content

Commit b69bb4a

Browse files
committed
PhpClassReflectionExtension no longer implements extension interfaces
1 parent 388f0aa commit b69bb4a

File tree

6 files changed

+82
-54
lines changed

6 files changed

+82
-54
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,7 @@ private function createAstClassReflection(Node\Stmt\ClassLike $stmt, string $cla
22252225
$this->signatureMapProvider,
22262226
$this->deprecationProvider,
22272227
$this->attributeReflectionFactory,
2228+
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPhpClassReflectionExtension(),
22282229
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
22292230
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
22302231
$this->classReflectionExtensionRegistryProvider->getRegistry()->getAllowedSubTypesClassReflectionExtensions(),

src/DependencyInjection/Reflection/LazyClassReflectionExtensionRegistryProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function __construct(private Container $container)
3030
public function getRegistry(): ClassReflectionExtensionRegistry
3131
{
3232
if ($this->registry === null) {
33-
$phpClassReflectionExtension = $this->container->getByType(PhpClassReflectionExtension::class);
3433
$annotationsMethodsClassReflectionExtension = $this->container->getByType(AnnotationsMethodsClassReflectionExtension::class);
3534
$annotationsPropertiesClassReflectionExtension = $this->container->getByType(AnnotationsPropertiesClassReflectionExtension::class);
3635

@@ -40,11 +39,12 @@ public function getRegistry(): ClassReflectionExtensionRegistry
4039
$universalObjectCratesClassReflectionExtension = $this->container->getByType(UniversalObjectCratesClassReflectionExtension::class);
4140

4241
$this->registry = new ClassReflectionExtensionRegistry(
43-
array_merge([$phpClassReflectionExtension], $this->container->getServicesByTag(BrokerFactory::PROPERTIES_CLASS_REFLECTION_EXTENSION_TAG), [$annotationsPropertiesClassReflectionExtension, $mixinPropertiesClassReflectionExtension, $universalObjectCratesClassReflectionExtension]),
44-
array_merge([$phpClassReflectionExtension], $this->container->getServicesByTag(BrokerFactory::METHODS_CLASS_REFLECTION_EXTENSION_TAG), [$annotationsMethodsClassReflectionExtension, $mixinMethodsClassReflectionExtension, $soapClientMethodsClassReflectionExtension]),
42+
array_merge($this->container->getServicesByTag(BrokerFactory::PROPERTIES_CLASS_REFLECTION_EXTENSION_TAG), [$annotationsPropertiesClassReflectionExtension, $mixinPropertiesClassReflectionExtension, $universalObjectCratesClassReflectionExtension]),
43+
array_merge($this->container->getServicesByTag(BrokerFactory::METHODS_CLASS_REFLECTION_EXTENSION_TAG), [$annotationsMethodsClassReflectionExtension, $mixinMethodsClassReflectionExtension, $soapClientMethodsClassReflectionExtension]),
4544
$this->container->getServicesByTag(BrokerFactory::ALLOWED_SUB_TYPES_CLASS_REFLECTION_EXTENSION_TAG),
4645
$this->container->getByType(RequireExtendsPropertiesClassReflectionExtension::class),
4746
$this->container->getByType(RequireExtendsMethodsClassReflectionExtension::class),
47+
$this->container->getByType(PhpClassReflectionExtension::class),
4848
);
4949
}
5050

src/Reflection/BetterReflection/BetterReflectionProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public function getClass(string $className): ClassReflection
157157
$this->signatureMapProvider,
158158
$this->deprecationProvider,
159159
$this->attributeReflectionFactory,
160+
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPhpClassReflectionExtension(),
160161
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
161162
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
162163
$this->classReflectionExtensionRegistryProvider->getRegistry()->getAllowedSubTypesClassReflectionExtensions(),
@@ -253,6 +254,7 @@ public function getAnonymousClassReflection(Node\Stmt\Class_ $classNode, Scope $
253254
$this->signatureMapProvider,
254255
$this->deprecationProvider,
255256
$this->attributeReflectionFactory,
257+
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPhpClassReflectionExtension(),
256258
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
257259
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
258260
$this->classReflectionExtensionRegistryProvider->getRegistry()->getAllowedSubTypesClassReflectionExtensions(),

src/Reflection/ClassReflection.php

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public function __construct(
177177
private SignatureMapProvider $signatureMapProvider,
178178
private DeprecationProvider $deprecationProvider,
179179
private AttributeReflectionFactory $attributeReflectionFactory,
180+
private PhpClassReflectionExtension $phpClassReflectionExtension,
180181
private array $propertiesClassReflectionExtensions,
181182
private array $methodsClassReflectionExtensions,
182183
private array $allowedSubTypesClassReflectionExtensions,
@@ -474,18 +475,16 @@ public function hasProperty(string $propertyName): bool
474475
return $this->hasPropertyCache[$propertyName] = $this->hasNativeProperty($propertyName);
475476
}
476477

477-
foreach ($this->propertiesClassReflectionExtensions as $i => $extension) {
478-
if ($i > 0 && !$this->allowsDynamicProperties()) {
479-
break;
480-
}
481-
if ($extension->hasProperty($this, $propertyName)) {
482-
return $this->hasPropertyCache[$propertyName] = true;
483-
}
478+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
479+
return $this->hasPropertyCache[$propertyName] = true;
484480
}
485481

486-
// For BC purpose
487-
if ($this->getPhpExtension()->hasProperty($this, $propertyName)) {
488-
return $this->hasPropertyCache[$propertyName] = true;
482+
if ($this->allowsDynamicProperties()) {
483+
foreach ($this->propertiesClassReflectionExtensions as $extension) {
484+
if ($extension->hasProperty($this, $propertyName)) {
485+
return $this->hasPropertyCache[$propertyName] = true;
486+
}
487+
}
489488
}
490489

491490
if ($this->requireExtendsPropertiesClassReflectionExtension->hasProperty($this, $propertyName)) {
@@ -505,16 +504,22 @@ public function hasInstanceProperty(string $propertyName): bool
505504
return $this->hasInstancePropertyCache[$propertyName] = $this->hasNativeProperty($propertyName);
506505
}
507506

508-
foreach ($this->propertiesClassReflectionExtensions as $i => $extension) {
509-
if ($i > 0 && !$this->allowsDynamicProperties()) {
510-
break;
507+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
508+
$property = $this->phpClassReflectionExtension->getProperty($this, $propertyName);
509+
if (!$property->isStatic()) {
510+
return $this->hasInstancePropertyCache[$propertyName] = true;
511511
}
512-
if ($extension->hasProperty($this, $propertyName)) {
513-
$property = $extension->getProperty($this, $propertyName);
514-
if ($property->isStatic()) {
515-
continue;
512+
}
513+
514+
if ($this->allowsDynamicProperties()) {
515+
foreach ($this->propertiesClassReflectionExtensions as $extension) {
516+
if ($extension->hasProperty($this, $propertyName)) {
517+
$property = $extension->getProperty($this, $propertyName);
518+
if ($property->isStatic()) {
519+
continue;
520+
}
521+
return $this->hasInstancePropertyCache[$propertyName] = true;
516522
}
517-
return $this->hasInstancePropertyCache[$propertyName] = true;
518523
}
519524
}
520525

@@ -531,8 +536,8 @@ public function hasStaticProperty(string $propertyName): bool
531536
return $this->hasStaticPropertyCache[$propertyName];
532537
}
533538

534-
if ($this->getPhpExtension()->hasProperty($this, $propertyName)) {
535-
$property = $this->getPhpExtension()->getProperty($this, $propertyName);
539+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
540+
$property = $this->phpClassReflectionExtension->getProperty($this, $propertyName);
536541
if ($property->isStatic()) {
537542
return $this->hasStaticPropertyCache[$propertyName] = true;
538543
}
@@ -551,6 +556,10 @@ public function hasMethod(string $methodName): bool
551556
return $this->hasMethodCache[$methodName];
552557
}
553558

559+
if ($this->phpClassReflectionExtension->hasMethod($this, $methodName)) {
560+
return $this->hasMethodCache[$methodName] = true;
561+
}
562+
554563
foreach ($this->methodsClassReflectionExtensions as $extension) {
555564
if ($extension->hasMethod($this, $methodName)) {
556565
return $this->hasMethodCache[$methodName] = true;
@@ -571,6 +580,14 @@ public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope):
571580
$key = sprintf('%s-%s', $key, $scope->getClassReflection()->getCacheKey());
572581
}
573582

583+
if ($this->phpClassReflectionExtension->hasMethod($this, $methodName)) {
584+
$method = $this->phpClassReflectionExtension->getMethod($this, $methodName);
585+
if ($scope->canCallMethod($method)) {
586+
return $this->methods[$key] = $method;
587+
}
588+
$this->methods[$key] = $method;
589+
}
590+
574591
if (!isset($this->methods[$key])) {
575592
foreach ($this->methodsClassReflectionExtensions as $extension) {
576593
if (!$extension->hasMethod($this, $methodName)) {
@@ -619,15 +636,15 @@ private function wrapExtendedProperty(string $propertyName, PropertyReflection $
619636

620637
public function hasNativeMethod(string $methodName): bool
621638
{
622-
return $this->getPhpExtension()->hasNativeMethod($this, $methodName);
639+
return $this->phpClassReflectionExtension->hasNativeMethod($this, $methodName);
623640
}
624641

625642
public function getNativeMethod(string $methodName): ExtendedMethodReflection
626643
{
627644
if (!$this->hasNativeMethod($methodName)) {
628645
throw new MissingMethodFromReflectionException($this->getName(), $methodName);
629646
}
630-
return $this->getPhpExtension()->getNativeMethod($this, $methodName);
647+
return $this->phpClassReflectionExtension->getNativeMethod($this, $methodName);
631648
}
632649

633650
public function hasConstructor(): bool
@@ -662,16 +679,6 @@ private function findConstructor(): ?ReflectionMethod
662679
return $constructor;
663680
}
664681

665-
private function getPhpExtension(): PhpClassReflectionExtension
666-
{
667-
$extension = $this->methodsClassReflectionExtensions[0];
668-
if (!$extension instanceof PhpClassReflectionExtension) {
669-
throw new ShouldNotHappenException();
670-
}
671-
672-
return $extension;
673-
}
674-
675682
/** @internal */
676683
public function evictPrivateSymbols(): void
677684
{
@@ -710,7 +717,7 @@ public function evictPrivateSymbols(): void
710717

711718
unset($this->methods[$name]);
712719
}
713-
$this->getPhpExtension()->evictPrivateSymbols($this->getCacheKey());
720+
$this->phpClassReflectionExtension->evictPrivateSymbols($this->getCacheKey());
714721
}
715722

716723
/** @deprecated Use getInstanceProperty or getStaticProperty */
@@ -725,12 +732,16 @@ public function getProperty(string $propertyName, ClassMemberAccessAnswerer $sco
725732
$key = sprintf('%s-%s', $key, $scope->getClassReflection()->getCacheKey());
726733
}
727734

728-
if (!isset($this->properties[$key])) {
729-
foreach ($this->propertiesClassReflectionExtensions as $i => $extension) {
730-
if ($i > 0 && !$this->allowsDynamicProperties()) {
731-
break;
732-
}
735+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
736+
$property = $this->phpClassReflectionExtension->getProperty($this, $propertyName);
737+
if ($scope->canReadProperty($property)) {
738+
return $this->properties[$key] = $property;
739+
}
740+
$this->properties[$key] = $property;
741+
}
733742

743+
if (!isset($this->properties[$key]) && $this->allowsDynamicProperties()) {
744+
foreach ($this->propertiesClassReflectionExtensions as $extension) {
734745
if (!$extension->hasProperty($this, $propertyName)) {
735746
continue;
736747
}
@@ -744,8 +755,8 @@ public function getProperty(string $propertyName, ClassMemberAccessAnswerer $sco
744755
}
745756

746757
// For BC purpose
747-
if ($this->getPhpExtension()->hasProperty($this, $propertyName)) {
748-
$property = $this->getPhpExtension()->getProperty($this, $propertyName);
758+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
759+
$property = $this->phpClassReflectionExtension->getProperty($this, $propertyName);
749760

750761
return $this->properties[$key] = $property;
751762
}
@@ -775,12 +786,18 @@ public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswe
775786
$key = sprintf('%s-%s', $key, $scope->getClassReflection()->getCacheKey());
776787
}
777788

778-
if (!isset($this->instanceProperties[$key])) {
779-
foreach ($this->propertiesClassReflectionExtensions as $i => $extension) {
780-
if ($i > 0 && !$this->allowsDynamicProperties()) {
781-
break;
789+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
790+
$property = $this->phpClassReflectionExtension->getProperty($this, $propertyName);
791+
if (!$property->isStatic()) {
792+
if ($scope->canReadProperty($property)) {
793+
return $this->instanceProperties[$key] = $property;
782794
}
795+
$this->instanceProperties[$key] = $property;
796+
}
797+
}
783798

799+
if (!isset($this->instanceProperties[$key]) && $this->allowsDynamicProperties()) {
800+
foreach ($this->propertiesClassReflectionExtensions as $extension) {
784801
if (!$extension->hasProperty($this, $propertyName)) {
785802
continue;
786803
}
@@ -819,8 +836,8 @@ public function getStaticProperty(string $propertyName): ExtendedPropertyReflect
819836
return $this->staticProperties[$key];
820837
}
821838

822-
if ($this->getPhpExtension()->hasProperty($this, $propertyName)) {
823-
$nakedProperty = $this->getPhpExtension()->getProperty($this, $propertyName);
839+
if ($this->phpClassReflectionExtension->hasProperty($this, $propertyName)) {
840+
$nakedProperty = $this->phpClassReflectionExtension->getProperty($this, $propertyName);
824841
if ($nakedProperty->isStatic()) {
825842
$property = $this->wrapExtendedProperty($propertyName, $nakedProperty);
826843
if ($property->isStatic()) {
@@ -839,7 +856,7 @@ public function getStaticProperty(string $propertyName): ExtendedPropertyReflect
839856

840857
public function hasNativeProperty(string $propertyName): bool
841858
{
842-
return $this->getPhpExtension()->hasProperty($this, $propertyName);
859+
return $this->phpClassReflectionExtension->hasProperty($this, $propertyName);
843860
}
844861

845862
public function getNativeProperty(string $propertyName): PhpPropertyReflection
@@ -848,7 +865,7 @@ public function getNativeProperty(string $propertyName): PhpPropertyReflection
848865
throw new MissingPropertyFromReflectionException($this->getName(), $propertyName);
849866
}
850867

851-
return $this->getPhpExtension()->getNativeProperty($this, $propertyName);
868+
return $this->phpClassReflectionExtension->getNativeProperty($this, $propertyName);
852869
}
853870

854871
public function isAbstract(): bool
@@ -1762,6 +1779,7 @@ public function withTypes(array $types): self
17621779
$this->signatureMapProvider,
17631780
$this->deprecationProvider,
17641781
$this->attributeReflectionFactory,
1782+
$this->phpClassReflectionExtension,
17651783
$this->propertiesClassReflectionExtensions,
17661784
$this->methodsClassReflectionExtensions,
17671785
$this->allowedSubTypesClassReflectionExtensions,
@@ -1794,6 +1812,7 @@ public function withVariances(array $variances): self
17941812
$this->signatureMapProvider,
17951813
$this->deprecationProvider,
17961814
$this->attributeReflectionFactory,
1815+
$this->phpClassReflectionExtension,
17971816
$this->propertiesClassReflectionExtensions,
17981817
$this->methodsClassReflectionExtensions,
17991818
$this->allowedSubTypesClassReflectionExtensions,
@@ -1836,6 +1855,7 @@ public function asFinal(): self
18361855
$this->signatureMapProvider,
18371856
$this->deprecationProvider,
18381857
$this->attributeReflectionFactory,
1858+
$this->phpClassReflectionExtension,
18391859
$this->propertiesClassReflectionExtensions,
18401860
$this->methodsClassReflectionExtensions,
18411861
$this->allowedSubTypesClassReflectionExtensions,
@@ -1878,6 +1898,7 @@ public function removeFinalKeywordOverride(): self
18781898
$this->signatureMapProvider,
18791899
$this->deprecationProvider,
18801900
$this->attributeReflectionFactory,
1901+
$this->phpClassReflectionExtension,
18811902
$this->propertiesClassReflectionExtensions,
18821903
$this->methodsClassReflectionExtensions,
18831904
$this->allowedSubTypesClassReflectionExtensions,

src/Reflection/ClassReflectionExtensionRegistry.php

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

33
namespace PHPStan\Reflection;
44

5+
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
56
use PHPStan\Reflection\RequireExtension\RequireExtendsMethodsClassReflectionExtension;
67
use PHPStan\Reflection\RequireExtension\RequireExtendsPropertiesClassReflectionExtension;
78

@@ -19,6 +20,7 @@ public function __construct(
1920
private array $allowedSubTypesClassReflectionExtensions,
2021
private RequireExtendsPropertiesClassReflectionExtension $requireExtendsPropertiesClassReflectionExtension,
2122
private RequireExtendsMethodsClassReflectionExtension $requireExtendsMethodsClassReflectionExtension,
23+
private PhpClassReflectionExtension $phpClassReflectionExtension,
2224
)
2325
{
2426
}
@@ -57,4 +59,9 @@ public function getRequireExtendsMethodsClassReflectionExtension(): RequireExten
5759
return $this->requireExtendsMethodsClassReflectionExtension;
5860
}
5961

62+
public function getPhpClassReflectionExtension(): PhpClassReflectionExtension
63+
{
64+
return $this->phpClassReflectionExtension;
65+
}
66+
6067
}

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
use PHPStan\Reflection\ExtendedMethodReflection;
2828
use PHPStan\Reflection\InitializerExprContext;
2929
use PHPStan\Reflection\MethodReflection;
30-
use PHPStan\Reflection\MethodsClassReflectionExtension;
3130
use PHPStan\Reflection\Native\ExtendedNativeParameterReflection;
3231
use PHPStan\Reflection\Native\NativeMethodReflection;
33-
use PHPStan\Reflection\PropertiesClassReflectionExtension;
3432
use PHPStan\Reflection\ReflectionProvider;
3533
use PHPStan\Reflection\SignatureMap\FunctionSignature;
3634
use PHPStan\Reflection\SignatureMap\ParameterSignature;
@@ -65,7 +63,6 @@
6563
use function strtolower;
6664

6765
final class PhpClassReflectionExtension
68-
implements PropertiesClassReflectionExtension, MethodsClassReflectionExtension
6966
{
7067

7168
/** @var PhpPropertyReflection[][] */

0 commit comments

Comments
 (0)