From 30adaf3fc04120ae9ddc52bab4347aeed87996a2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 28 Mar 2024 08:14:50 +0000 Subject: [PATCH] Updated Rector to commit eb525ca9fe04a8bc074cf49b5cf25991014fff5f https://github.com/rectorphp/rector-src/commit/eb525ca9fe04a8bc074cf49b5cf25991014fff5f [DeadCode] Skip Array Callable dynamic method using __CLASS__ with constructor (no default args) on RemoveUnusedPrivateMethodRector (#5774) --- src/Application/VersionResolver.php | 4 +-- .../ArrayCallableMethodMatcher.php | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index bfe882799bb..ab1c0a0dfd6 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 = '3e6a4923d463e2a2d104ac283dae04343ee2a2d0'; + public const PACKAGE_VERSION = 'eb525ca9fe04a8bc074cf49b5cf25991014fff5f'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-03-27 22:26:48'; + public const RELEASE_DATE = '2024-03-28 15:12:20'; /** * @var int */ diff --git a/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php b/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php index f97dabd8832..3dc8f8566d4 100644 --- a/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php +++ b/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php @@ -7,6 +7,7 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Scalar\MagicConst\Class_; use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; @@ -135,13 +136,14 @@ final class ArrayCallableMethodMatcher return \in_array($fromFuncCallName, $functionNames, \true); } /** + * @param \PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Scalar\MagicConst\Class_ $classContext * @return \PHPStan\Type\MixedType|\PHPStan\Type\ObjectType */ - private function resolveClassConstFetchType(ClassConstFetch $classConstFetch, Scope $scope, ?string $classMethodName) + private function resolveClassContextType($classContext, Scope $scope, ?string $classMethodName) { - $classConstantReference = $this->valueResolver->getValue($classConstFetch); - if ($classConstantReference === ObjectReference::STATIC) { - $classReflection = $this->reflectionResolver->resolveClassReflection($classConstFetch); + $classConstantReference = $this->valueResolver->getValue($classContext); + if ($this->isRequiredClassReflectionResolution($classConstantReference)) { + $classReflection = $this->reflectionResolver->resolveClassReflection($classContext); if (!$classReflection instanceof ClassReflection || !$classReflection->isClass()) { return new MixedType(); } @@ -173,9 +175,9 @@ final class ArrayCallableMethodMatcher } private function resolveCallerType(Expr $expr, Scope $scope, ?string $classMethodName) : Type { - if ($expr instanceof ClassConstFetch) { - // static ::class reference? - $callerType = $this->resolveClassConstFetchType($expr, $scope, $classMethodName); + if ($expr instanceof ClassConstFetch || $expr instanceof Class_) { + // class context means self|static ::class or __CLASS__ + $callerType = $this->resolveClassContextType($expr, $scope, $classMethodName); } else { $callerType = $this->nodeTypeResolver->getType($expr); } @@ -184,4 +186,14 @@ final class ArrayCallableMethodMatcher } return $callerType; } + private function isRequiredClassReflectionResolution(string $classConstantReference) : bool + { + if ($classConstantReference === ObjectReference::STATIC) { + return \true; + } + if ($classConstantReference === '__CLASS__') { + return \true; + } + return \false; + } }