Skip to content

Commit

Permalink
ExtendedPropertyReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 4, 2024
1 parent 46c163c commit d65138a
Show file tree
Hide file tree
Showing 29 changed files with 193 additions and 63 deletions.
3 changes: 2 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;
Expand Down Expand Up @@ -5687,7 +5688,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName,
}

/** @api */
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?PropertyReflection
public function getPropertyReflection(Type $typeWithProperty, string $propertyName): ?ExtendedPropertyReflection
{
if ($typeWithProperty instanceof UnionType) {
$newTypes = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\NamespaceAnswerer;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -71,7 +71,7 @@ public function getDefinedVariables(): array;

public function hasConstant(Name $name): bool;

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Annotations/AnnotationPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace PHPStan\Reflection\Annotations;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

final class AnnotationPropertyReflection implements PropertyReflection
final class AnnotationPropertyReflection implements ExtendedPropertyReflection
{

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Reflection\Annotations;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Type\Generic\TemplateTypeHelper;
Expand All @@ -12,7 +13,7 @@
final class AnnotationsPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension
{

/** @var PropertyReflection[][] */
/** @var ExtendedPropertyReflection[][] */
private array $properties = [];

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

/**
* @return ExtendedPropertyReflection
*/
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
return $this->properties[$classReflection->getCacheKey()][$propertyName];
Expand All @@ -37,7 +41,7 @@ private function findClassReflectionWithProperty(
ClassReflection $classReflection,
ClassReflection $declaringClass,
string $propertyName,
): ?PropertyReflection
): ?ExtendedPropertyReflection
{
$propertyTags = $classReflection->getPropertyTags();
if (isset($propertyTags[$propertyName])) {
Expand Down
15 changes: 12 additions & 3 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ClassReflection
/** @var ExtendedMethodReflection[] */
private array $methods = [];

/** @var PropertyReflection[] */
/** @var ExtendedPropertyReflection[] */
private array $properties = [];

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

private function wrapExtendedProperty(PropertyReflection $method): ExtendedPropertyReflection
{
if ($method instanceof ExtendedPropertyReflection) {
return $method;
}

return new WrappedExtendedPropertyReflection($method);
}

public function hasNativeMethod(string $methodName): bool
{
return $this->getPhpExtension()->hasNativeMethod($this, $methodName);
Expand Down Expand Up @@ -619,7 +628,7 @@ public function evictPrivateSymbols(): void
$this->getPhpExtension()->evictPrivateSymbols($this->getCacheKey());
}

public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection
public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
{
if ($this->isEnum()) {
return $this->getNativeProperty($propertyName);
Expand All @@ -640,7 +649,7 @@ public function getProperty(string $propertyName, ClassMemberAccessAnswerer $sco
continue;
}

$property = $extension->getProperty($this, $propertyName);
$property = $this->wrapExtendedProperty($extension->getProperty($this, $propertyName));
if ($scope->canAccessProperty($property)) {
return $this->properties[$key] = $property;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Reflection/Dummy/ChangedTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace PHPStan\Reflection\Dummy;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\WrapperPropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

final class ChangedTypePropertyReflection implements WrapperPropertyReflection
{

public function __construct(private ClassReflection $declaringClass, private PropertyReflection $reflection, private Type $readableType, private Type $writableType)
public function __construct(private ClassReflection $declaringClass, private ExtendedPropertyReflection $reflection, private Type $readableType, private Type $writableType)
{
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public function isInternal(): TrinaryLogic
return $this->reflection->isInternal();
}

public function getOriginalReflection(): PropertyReflection
public function getOriginalReflection(): ExtendedPropertyReflection
{
return $this->reflection;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Dummy/DummyPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace PHPStan\Reflection\Dummy;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use stdClass;

final class DummyPropertyReflection implements PropertyReflection
final class DummyPropertyReflection implements ExtendedPropertyReflection
{

public function getDeclaringClass(): ClassReflection
Expand Down
22 changes: 22 additions & 0 deletions src/Reflection/ExtendedPropertyReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection;

/**
* The purpose of this interface is to be able to
* answer more questions about properties
* without breaking backward compatibility
* with existing PropertiesClassReflectionExtension.
*
* Developers are meant to only implement PropertyReflection
* and its methods in their code.
*
* New methods on ExtendedPropertyReflection will be added
* in minor versions.
*
* @api
*/
interface ExtendedPropertyReflection extends PropertyReflection
{

}
4 changes: 2 additions & 2 deletions src/Reflection/Php/EnumPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace PHPStan\Reflection\Php;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

final class EnumPropertyReflection implements PropertyReflection
final class EnumPropertyReflection implements ExtendedPropertyReflection
{

public function __construct(private ClassReflection $declaringClass, private Type $type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PHPStan\Reflection\Php;

use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
use PHPStan\Type\Type;

Expand All @@ -18,12 +18,12 @@ public function doNotResolveTemplateTypeMapToBounds(): UnresolvedPropertyPrototy
return $this;
}

public function getNakedProperty(): PropertyReflection
public function getNakedProperty(): ExtendedPropertyReflection
{
return $this->property;
}

public function getTransformedProperty(): PropertyReflection
public function getTransformedProperty(): ExtendedPropertyReflection
{
return $this->property;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\Native\NativeMethodReflection;
use PHPStan\Reflection\Native\NativeParameterWithPhpDocsReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\SignatureMap\FunctionSignature;
use PHPStan\Reflection\SignatureMap\ParameterSignature;
Expand Down Expand Up @@ -64,7 +64,7 @@ final class PhpClassReflectionExtension
implements PropertiesClassReflectionExtension, MethodsClassReflectionExtension
{

/** @var PropertyReflection[][] */
/** @var ExtendedPropertyReflection[][] */
private array $propertiesIncludingAnnotations = [];

/** @var PhpPropertyReflection[][] */
Expand Down Expand Up @@ -152,7 +152,7 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
return $classReflection->getNativeReflection()->hasProperty($propertyName);
}

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
public function getProperty(ClassReflection $classReflection, string $propertyName): ExtendedPropertyReflection
{
if (!isset($this->propertiesIncludingAnnotations[$classReflection->getCacheKey()][$propertyName])) {
$this->propertiesIncludingAnnotations[$classReflection->getCacheKey()][$propertyName] = $this->createProperty($classReflection, $propertyName, true);
Expand All @@ -176,7 +176,7 @@ private function createProperty(
ClassReflection $classReflection,
string $propertyName,
bool $includingAnnotations,
): PropertyReflection
): ExtendedPropertyReflection
{
$propertyReflection = $classReflection->getNativeReflection()->getProperty($propertyName);
$propertyName = $propertyReflection->getName();
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Php/PhpPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionUnionType;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand All @@ -17,7 +17,7 @@
* @api
* @final
*/
class PhpPropertyReflection implements PropertyReflection
class PhpPropertyReflection implements ExtendedPropertyReflection
{

private ?Type $finalNativeType = null;
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Php/SimpleXMLElementProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PHPStan\Reflection\Php;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
Expand All @@ -12,7 +12,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

final class SimpleXMLElementProperty implements PropertyReflection
final class SimpleXMLElementProperty implements ExtendedPropertyReflection
{

public function __construct(
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Php/UniversalObjectCrateProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace PHPStan\Reflection\Php;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

final class UniversalObjectCrateProperty implements PropertyReflection
final class UniversalObjectCrateProperty implements ExtendedPropertyReflection
{

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\ShouldNotHappenException;
Expand All @@ -16,6 +17,9 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
return $this->findProperty($classReflection, $propertyName) !== null;
}

/**
* @return ExtendedPropertyReflection
*/
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
$property = $this->findProperty($classReflection, $propertyName);
Expand All @@ -26,7 +30,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
return $property;
}

private function findProperty(ClassReflection $classReflection, string $propertyName): ?PropertyReflection
private function findProperty(ClassReflection $classReflection, string $propertyName): ?ExtendedPropertyReflection
{
if (!$classReflection->isInterface()) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/ResolvedPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ final class ResolvedPropertyReflection implements WrapperPropertyReflection
private ?Type $writableType = null;

public function __construct(
private PropertyReflection $reflection,
private ExtendedPropertyReflection $reflection,
private TemplateTypeMap $templateTypeMap,
private TemplateTypeVarianceMap $callSiteVarianceMap,
)
{
}

public function getOriginalReflection(): PropertyReflection
public function getOriginalReflection(): ExtendedPropertyReflection
{
return $this->reflection;
}
Expand Down
Loading

0 comments on commit d65138a

Please sign in to comment.