From aace6ea006af3adbc59d6624204732fdcf248f92 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 30 Jan 2024 23:57:24 +0000 Subject: [PATCH] Updated Rector to commit 7294e98a19531d580c3ba901c147ea0a52282595 https://github.com/rectorphp/rector-src/commit/7294e98a19531d580c3ba901c147ea0a52282595 Fix AddMethodCallBasedStrictParamTypeRector if already has a type (#5530) --- .../ClassMethodParamTypeCompleter.php | 5 ++++ ...ddMethodCallBasedStrictParamTypeRector.php | 25 +++++++++++++------ src/Application/VersionResolver.php | 4 +-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php b/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php index 2db2372c13e..2532b1a8046 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php +++ b/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\NodeAnalyzer; use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; @@ -69,6 +70,10 @@ final class ClassMethodParamTypeCompleter if (!$this->isAcceptedByDefault($param, $argumentStaticType)) { continue; } + // skip if param type already filled + if ($param->type instanceof Identifier) { + continue; + } if ($param->type instanceof Name && $param->type->getAttribute(AttributeKey::VIRTUAL_NODE) === \true) { continue; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php index 5c14db986d6..81fd975695c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php @@ -47,7 +47,7 @@ final class AddMethodCallBasedStrictParamTypeRector extends AbstractRector } public function getRuleDefinition() : RuleDefinition { - return new RuleDefinition('Change private method param type to strict type, based on passed strict types', [new CodeSample(<<<'CODE_SAMPLE' + return new RuleDefinition('Change private classMethod param type to strict type, based on passed strict types', [new CodeSample(<<<'CODE_SAMPLE' final class SomeClass { public function run(int $value) @@ -88,20 +88,19 @@ CODE_SAMPLE public function refactor(Node $node) : ?Node { $hasChanged = \false; - foreach ($node->getMethods() as $method) { - if ($method->params === []) { + foreach ($node->getMethods() as $classMethod) { + if ($classMethod->params === []) { continue; } - $isPrivate = $node->isFinal() && !$node->extends instanceof Name && $node->implements === [] && $method->isProtected() || $method->isFinal() && !$node->extends instanceof Name && $node->implements === [] || $method->isPrivate(); - if (!$isPrivate) { + if (!$this->isClassMethodPrivate($node, $classMethod)) { continue; } - if ($method->isPublic()) { + if ($classMethod->isPublic()) { continue; } - $methodCalls = $this->localMethodCallFinder->match($node, $method); + $methodCalls = $this->localMethodCallFinder->match($node, $classMethod); $classMethodParameterTypes = $this->callTypesResolver->resolveStrictTypesFromCalls($methodCalls); - $classMethod = $this->classMethodParamTypeCompleter->complete($method, $classMethodParameterTypes, self::MAX_UNION_TYPES); + $classMethod = $this->classMethodParamTypeCompleter->complete($classMethod, $classMethodParameterTypes, self::MAX_UNION_TYPES); if ($classMethod instanceof ClassMethod) { $hasChanged = \true; } @@ -111,4 +110,14 @@ CODE_SAMPLE } return null; } + private function isClassMethodPrivate(Class_ $class, ClassMethod $classMethod) : bool + { + if ($classMethod->isPrivate()) { + return \true; + } + if ($classMethod->isFinal() && !$class->extends instanceof Name && $class->implements === []) { + return \true; + } + return $class->isFinal() && !$class->extends instanceof Name && $class->implements === [] && $classMethod->isProtected(); + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 2117c80e3b5..aa14a5ef55a 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 = 'daf8b4879cbd0c4a0f42691b81b8d0c7f63da855'; + public const PACKAGE_VERSION = '7294e98a19531d580c3ba901c147ea0a52282595'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-01-30 22:47:15'; + public const RELEASE_DATE = '2024-01-30 23:55:11'; /** * @var int */