From be393d497b6c16f60b296a8cc2d74cb8ec542e16 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 24 Mar 2024 14:10:21 +0000 Subject: [PATCH] Updated Rector to commit fc250dd8da281cb79f5657d2f5d6c6c0053e54d1 https://github.com/rectorphp/rector-src/commit/fc250dd8da281cb79f5657d2f5d6c6c0053e54d1 [DeadCode] Fix array callable with constructor args (#5770) --- .../NodeAnalyzer/IsClassMethodUsedAnalyzer.php | 3 ++- src/Application/VersionResolver.php | 4 ++-- .../NodeAnalyzer/ArrayCallableMethodMatcher.php | 13 ++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php b/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php index 1daef12c5a7..5356cee668e 100644 --- a/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php @@ -134,11 +134,12 @@ final class IsClassMethodUsedAnalyzer { /** @var Array_[] $arrays */ $arrays = $this->betterNodeFinder->findInstanceOf($class, Array_::class); + $classMethodName = $this->nodeNameResolver->getName($classMethod); foreach ($arrays as $array) { if ($this->isInArrayMap($class, $array)) { return \true; } - $arrayCallable = $this->arrayCallableMethodMatcher->match($array, $scope); + $arrayCallable = $this->arrayCallableMethodMatcher->match($array, $scope, $classMethodName); if ($arrayCallable instanceof ArrayCallableDynamicMethod) { return \true; } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index caafbaae5ed..707835eee04 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 = '25f3af626f841f7e62606a3a9aa64789a460f74c'; + public const PACKAGE_VERSION = 'fc250dd8da281cb79f5657d2f5d6c6c0053e54d1'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-03-24 00:27:33'; + public const RELEASE_DATE = '2024-03-24 21:08:05'; /** * @var int */ diff --git a/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php b/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php index 0e5f994266e..f97dabd8832 100644 --- a/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php +++ b/src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php @@ -61,7 +61,7 @@ final class ArrayCallableMethodMatcher * @see https://github.com/rectorphp/rector-src/pull/909 * @return null|\Rector\NodeCollector\ValueObject\ArrayCallableDynamicMethod|\Rector\NodeCollector\ValueObject\ArrayCallable */ - public function match(Array_ $array, Scope $scope) + public function match(Array_ $array, Scope $scope, ?string $classMethodName = null) { if (\count($array->items) !== 2) { return null; @@ -73,7 +73,7 @@ final class ArrayCallableMethodMatcher $items = $array->items; // $this, self, static, FQN $firstItemValue = $items[0]->value; - $callerType = $this->resolveCallerType($firstItemValue, $scope); + $callerType = $this->resolveCallerType($firstItemValue, $scope, $classMethodName); if (!$callerType instanceof TypeWithClassName) { return null; } @@ -137,7 +137,7 @@ final class ArrayCallableMethodMatcher /** * @return \PHPStan\Type\MixedType|\PHPStan\Type\ObjectType */ - private function resolveClassConstFetchType(ClassConstFetch $classConstFetch, Scope $scope) + private function resolveClassConstFetchType(ClassConstFetch $classConstFetch, Scope $scope, ?string $classMethodName) { $classConstantReference = $this->valueResolver->getValue($classConstFetch); if ($classConstantReference === ObjectReference::STATIC) { @@ -159,6 +159,9 @@ final class ArrayCallableMethodMatcher if (!$hasConstruct) { return new ObjectType($classConstantReference, null, $classReflection); } + if (\is_string($classMethodName) && $classReflection->hasNativeMethod($classMethodName)) { + return new ObjectType($classConstantReference, null, $classReflection); + } $extendedMethodReflection = $classReflection->getMethod(MethodName::CONSTRUCT, $scope); $parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle($extendedMethodReflection->getVariants()); foreach ($parametersAcceptorWithPhpDocs->getParameters() as $parameterReflectionWithPhpDoc) { @@ -168,11 +171,11 @@ final class ArrayCallableMethodMatcher } return new ObjectType($classConstantReference, null, $classReflection); } - private function resolveCallerType(Expr $expr, Scope $scope) : Type + private function resolveCallerType(Expr $expr, Scope $scope, ?string $classMethodName) : Type { if ($expr instanceof ClassConstFetch) { // static ::class reference? - $callerType = $this->resolveClassConstFetchType($expr, $scope); + $callerType = $this->resolveClassConstFetchType($expr, $scope, $classMethodName); } else { $callerType = $this->nodeTypeResolver->getType($expr); }