diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index f771ca3708f..375f5c2730b 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'a207391d81e8a0e20a9207611c8c9661d851a94e'; + public const PACKAGE_VERSION = 'e9b4c647f9f40adc9e1859cd620c3632281e3384'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-07-14 00:30:38'; + public const RELEASE_DATE = '2024-07-15 00:03:58'; /** * @var int */ diff --git a/src/Reflection/ReflectionResolver.php b/src/Reflection/ReflectionResolver.php index ab5e28ea855..9624628552c 100644 --- a/src/Reflection/ReflectionResolver.php +++ b/src/Reflection/ReflectionResolver.php @@ -30,7 +30,6 @@ use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\NodeTypeResolver; -use Rector\PhpParser\AstResolver; use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; use Rector\ValueObject\MethodName; final class ReflectionResolver @@ -60,19 +59,13 @@ final class ReflectionResolver * @var \Rector\Reflection\MethodReflectionResolver */ private $methodReflectionResolver; - /** - * @readonly - * @var \Rector\PhpParser\AstResolver - */ - private $astResolver; - public function __construct(ReflectionProvider $reflectionProvider, NodeTypeResolver $nodeTypeResolver, NodeNameResolver $nodeNameResolver, ClassAnalyzer $classAnalyzer, \Rector\Reflection\MethodReflectionResolver $methodReflectionResolver, AstResolver $astResolver) + public function __construct(ReflectionProvider $reflectionProvider, NodeTypeResolver $nodeTypeResolver, NodeNameResolver $nodeNameResolver, ClassAnalyzer $classAnalyzer, \Rector\Reflection\MethodReflectionResolver $methodReflectionResolver) { $this->reflectionProvider = $reflectionProvider; $this->nodeTypeResolver = $nodeTypeResolver; $this->nodeNameResolver = $nodeNameResolver; $this->classAnalyzer = $classAnalyzer; $this->methodReflectionResolver = $methodReflectionResolver; - $this->astResolver = $astResolver; } /** * @api @@ -105,19 +98,67 @@ public function resolveClassReflection(?Node $node) : ?ClassReflection */ public function resolveClassReflectionSourceObject($node) : ?ClassReflection { + $objectType = $node instanceof StaticCall || $node instanceof StaticPropertyFetch ? $this->nodeTypeResolver->getType($node->class) : $this->nodeTypeResolver->getType($node->var); + if (!$objectType instanceof TypeWithClassName) { + return null; + } + $className = $objectType->getClassName(); + if (!$this->reflectionProvider->hasClass($className)) { + return null; + } + $classReflection = $this->reflectionProvider->getClass($className); if ($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch) { - $objectType = $node instanceof PropertyFetch ? $this->nodeTypeResolver->getType($node->var) : $this->nodeTypeResolver->getType($node->class); - if (!$objectType instanceof TypeWithClassName) { + $propertyName = (string) $this->nodeNameResolver->getName($node->name); + if (!$classReflection->hasNativeProperty($propertyName)) { return null; } - $className = $objectType->getClassName(); - if (!$this->reflectionProvider->hasClass($className)) { - return null; + $property = $classReflection->getNativeProperty($propertyName); + if ($property->isPrivate()) { + return $classReflection; + } + $nativeReflection = $classReflection->getNativeReflection(); + $properties = $nativeReflection->getProperties(); + $ancestors = \array_merge($classReflection->getParents(), $classReflection->getInterfaces()); + foreach ($properties as $property) { + if ($property->getName() !== $propertyName) { + continue; + } + if ($property->getDeclaringClass()->getName() === $className) { + return $classReflection; + } + foreach ($ancestors as $ancestor) { + if ($ancestor->hasNativeProperty($propertyName)) { + return $ancestor; + } + } + } + return $classReflection; + } + $methodName = (string) $this->nodeNameResolver->getName($node->name); + if (!$classReflection->hasNativeMethod($methodName)) { + return null; + } + $extendedMethodReflection = $classReflection->getNativeMethod($methodName); + if ($extendedMethodReflection->isPrivate()) { + return $classReflection; + } + $nativeReflection = $classReflection->getNativeReflection(); + $methods = $nativeReflection->getMethods(); + $ancestors = \array_merge($classReflection->getParents(), $classReflection->getInterfaces()); + foreach ($methods as $method) { + if ($method->getName() !== $methodName) { + continue; + } + if ($method->getDeclaringClass()->getName() === $className) { + return $classReflection; + } + foreach ($ancestors as $ancestor) { + if ($ancestor->hasNativeMethod($methodName)) { + return $ancestor; + } } - return $this->reflectionProvider->getClass($className); } - $classMethod = $this->astResolver->resolveClassMethodFromCall($node); - return $this->resolveClassReflection($classMethod); + return $classReflection; } /** * @param class-string $className