From 755d81c828ba4f73f067426311ecc2bd012b156d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 14 Nov 2025 00:36:59 +0100 Subject: [PATCH 1/2] add fixture --- .../Fixture/fixture.php.inc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules-tests/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector/Fixture/fixture.php.inc b/rules-tests/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector/Fixture/fixture.php.inc index 89036f9a144..3e32a9f42da 100644 --- a/rules-tests/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector/Fixture/fixture.php.inc +++ b/rules-tests/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector/Fixture/fixture.php.inc @@ -4,11 +4,11 @@ namespace Rector\Tests\Unambiguous\Rector\Class_\RemoveReturnThisFromSetterClass final class SomeClass { - private string $name; + private string $nameValue; public function setName(string $name): self { - $this->name = $name; + $this->nameValue = $name; return $this; } @@ -22,11 +22,11 @@ namespace Rector\Tests\Unambiguous\Rector\Class_\RemoveReturnThisFromSetterClass final class SomeClass { - private string $name; + private string $nameValue; public function setName(string $name): void { - $this->name = $name; + $this->nameValue = $name; } } From 7c51a40d3f24158a3bfe389cc890dfa4f9c35bad Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 14 Nov 2025 00:37:43 +0100 Subject: [PATCH 2/2] allow different property name --- .../NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php | 12 ++++++++---- .../SetterTypeDeclarationPropertyTypeInferer.php | 1 - .../RemoveReturnThisFromSetterClassMethodRector.php | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php b/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php index b4c80fcef5d..355cbadcbdc 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php +++ b/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php @@ -53,7 +53,7 @@ public function hasOnlyPropertyAssign(ClassMethod $classMethod, string $property return $this->isLocalPropertyVariableAssign($onlyClassMethodStmt, $propertyName); } - public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod, string $propertyName): bool + public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod): bool { $stmts = (array) $classMethod->stmts; if (count($stmts) !== 2) { @@ -63,7 +63,7 @@ public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod, string $possibleAssignStmt = $stmts[0]; $possibleReturnThis = $stmts[1]; - if (! $this->isLocalPropertyVariableAssign($possibleAssignStmt, $propertyName)) { + if (! $this->isLocalPropertyVariableAssign($possibleAssignStmt, null)) { return false; } @@ -80,7 +80,7 @@ public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod, string return $this->nodeNameResolver->isName($returnExpr, 'this'); } - private function isLocalPropertyVariableAssign(Stmt $onlyClassMethodStmt, string $propertyName): bool + private function isLocalPropertyVariableAssign(Stmt $onlyClassMethodStmt, ?string $propertyName): bool { if (! $onlyClassMethodStmt instanceof Expression) { return false; @@ -102,6 +102,10 @@ private function isLocalPropertyVariableAssign(Stmt $onlyClassMethodStmt, string return false; } - return $this->nodeNameResolver->isName($propertyFetch->name, $propertyName); + if ($propertyName) { + return $this->nodeNameResolver->isName($propertyFetch->name, $propertyName); + } + + return true; } } diff --git a/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/SetterTypeDeclarationPropertyTypeInferer.php b/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/SetterTypeDeclarationPropertyTypeInferer.php index 8de0ff539e0..bc07a55b5d4 100644 --- a/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/SetterTypeDeclarationPropertyTypeInferer.php +++ b/rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/SetterTypeDeclarationPropertyTypeInferer.php @@ -24,7 +24,6 @@ public function __construct( public function inferProperty(Property $property, Class_ $class): ?Type { - /** @var string $propertyName */ $propertyName = $this->nodeNameResolver->getName($property); foreach ($class->getMethods() as $classMethod) { diff --git a/rules/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector.php b/rules/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector.php index 415e0836755..4b8c8654979 100644 --- a/rules/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector.php +++ b/rules/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector.php @@ -101,7 +101,7 @@ public function refactor(Node $node): ?Class_ continue; } - if (! $this->classMethodAndPropertyAnalyzer->hasPropertyAssignWithReturnThis($classMethod, $paramName)) { + if (! $this->classMethodAndPropertyAnalyzer->hasPropertyAssignWithReturnThis($classMethod)) { continue; }