diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php index d44b19e70f3..745851e56dc 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php @@ -87,6 +87,11 @@ public function refactor(Node $node): ?Node { $this->hasChanged = false; + // skip class without parents/interfaces, there is no param type to mimic + if ($node instanceof Class_ && !$node->extends instanceof Name && $node->implements === []) { + return null; + } + // skip guarded classes, where adding a param type would break child classes if ($this->parentClassMethodTypeOverrideGuard->isTypeGuardedClass($node)) { return null; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php index 7002872ac1f..3c8cb16aa3c 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php @@ -131,24 +131,24 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod): bool { - if ($this->classModifierChecker->isInsideFinalClass($classMethod)) { + if (count((array) $classMethod->stmts) !== 1) { return false; } - if (count((array) $classMethod->stmts) !== 1) { + $onlyStmt = $classMethod->stmts[0] ?? null; + if (! $onlyStmt instanceof Expression || ! $onlyStmt->expr instanceof Throw_) { return false; } - $onlyStmt = $classMethod->stmts[0] ?? null; - return $onlyStmt instanceof Expression && $onlyStmt->expr instanceof Throw_; + return ! $this->classModifierChecker->isInsideFinalClass($classMethod); } private function isNotFinalAndEmpty(ClassMethod $classMethod): bool { - if ($this->classModifierChecker->isInsideFinalClass($classMethod)) { + if ($classMethod->stmts !== []) { return false; } - return $classMethod->stmts === []; + return ! $this->classModifierChecker->isInsideFinalClass($classMethod); } } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php index bc0cfa1865a..379b82f92a6 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php @@ -79,6 +79,17 @@ public function refactor(Node $node): ?Node continue; } + if (! $this->isNames($classMethod, [ + MethodName::CALL, + MethodName::CALL_STATIC, + MethodName::__SET, + MethodName::__GET, + MethodName::ISSET, + MethodName::UNSET, + ])) { + continue; + } + if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) { continue; } @@ -91,23 +102,11 @@ public function refactor(Node $node): ?Node } } - // first arg string - if ($this->isNames( - $classMethod, - [ - MethodName::CALL, - MethodName::CALL_STATIC, - MethodName::__SET, - MethodName::__GET, - MethodName::ISSET, - MethodName::UNSET, - ] - )) { - $firstParam = $classMethod->getParams()[0]; - if (! $firstParam->type instanceof Node) { - $firstParam->type = new Identifier('string'); - $hasChanged = true; - } + // first arg string, shared by all handled magic methods + $firstParam = $classMethod->getParams()[0]; + if (! $firstParam->type instanceof Node) { + $firstParam->type = new Identifier('string'); + $hasChanged = true; } if ($this->isName($classMethod, MethodName::__GET) && $this->phpVersionProvider->isAtLeastPhpVersion( diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php index 4ccc14398d9..565cfa46a8a 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php @@ -91,10 +91,10 @@ private function isTestClassMethodWithFilledReturnType(ClassMethod|Function_ $ca return false; } - if (! $this->testsNodeAnalyzer->isInTestClass($callLike)) { + if (! $callLike->returnType instanceof Node) { return false; } - return $callLike->returnType instanceof Node; + return $this->testsNodeAnalyzer->isInTestClass($callLike); } } diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromDocblockSetUpDefinedRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromDocblockSetUpDefinedRector.php index d8f771738ae..a61e7b79ea2 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromDocblockSetUpDefinedRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromDocblockSetUpDefinedRector.php @@ -92,16 +92,16 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->testsNodeAnalyzer->isInTestClass($node)) { - return null; - } - // nothing useful here $setUpClassMethod = $node->getMethod(MethodName::SET_UP); if (! $setUpClassMethod instanceof ClassMethod) { return null; } + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + $hasChanged = false; foreach ($node->getProperties() as $property) { diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php index 4697f195678..49272e79b0b 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php @@ -75,11 +75,6 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $yieldNodes = $this->yieldNodeFinder->find($node); - if ($yieldNodes === []) { - return null; - } - // skip already filled type if ($node->returnType instanceof Node && $this->isNames( $node->returnType, @@ -88,6 +83,11 @@ public function refactor(Node $node): ?Node return null; } + $yieldNodes = $this->yieldNodeFinder->find($node); + if ($yieldNodes === []) { + return null; + } + $scope = ScopeFetcher::fetch($node); if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod( $node, diff --git a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php index 5378dbbcbf7..57d649d8f6b 100644 --- a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php @@ -83,11 +83,11 @@ public function refactor(Node $node): ?Node } foreach ($this->addPropertyTypeDeclarations as $addPropertyTypeDeclaration) { - if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) { + if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) { continue; } - if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) { + if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) { continue; }