Skip to content

Commit 46618df

Browse files
authored
allow different property name (#7629)
* add fixture * allow different property name
1 parent 19c04bc commit 46618df

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

rules-tests/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector/Fixture/fixture.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ namespace Rector\Tests\Unambiguous\Rector\Class_\RemoveReturnThisFromSetterClass
44

55
final class SomeClass
66
{
7-
private string $name;
7+
private string $nameValue;
88

99
public function setName(string $name): self
1010
{
11-
$this->name = $name;
11+
$this->nameValue = $name;
1212

1313
return $this;
1414
}
@@ -22,11 +22,11 @@ namespace Rector\Tests\Unambiguous\Rector\Class_\RemoveReturnThisFromSetterClass
2222

2323
final class SomeClass
2424
{
25-
private string $name;
25+
private string $nameValue;
2626

2727
public function setName(string $name): void
2828
{
29-
$this->name = $name;
29+
$this->nameValue = $name;
3030
}
3131
}
3232

rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function hasOnlyPropertyAssign(ClassMethod $classMethod, string $property
5353
return $this->isLocalPropertyVariableAssign($onlyClassMethodStmt, $propertyName);
5454
}
5555

56-
public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod, string $propertyName): bool
56+
public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod): bool
5757
{
5858
$stmts = (array) $classMethod->stmts;
5959
if (count($stmts) !== 2) {
@@ -63,7 +63,7 @@ public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod, string
6363
$possibleAssignStmt = $stmts[0];
6464
$possibleReturnThis = $stmts[1];
6565

66-
if (! $this->isLocalPropertyVariableAssign($possibleAssignStmt, $propertyName)) {
66+
if (! $this->isLocalPropertyVariableAssign($possibleAssignStmt, null)) {
6767
return false;
6868
}
6969

@@ -80,7 +80,7 @@ public function hasPropertyAssignWithReturnThis(ClassMethod $classMethod, string
8080
return $this->nodeNameResolver->isName($returnExpr, 'this');
8181
}
8282

83-
private function isLocalPropertyVariableAssign(Stmt $onlyClassMethodStmt, string $propertyName): bool
83+
private function isLocalPropertyVariableAssign(Stmt $onlyClassMethodStmt, ?string $propertyName): bool
8484
{
8585
if (! $onlyClassMethodStmt instanceof Expression) {
8686
return false;
@@ -102,6 +102,10 @@ private function isLocalPropertyVariableAssign(Stmt $onlyClassMethodStmt, string
102102
return false;
103103
}
104104

105-
return $this->nodeNameResolver->isName($propertyFetch->name, $propertyName);
105+
if ($propertyName) {
106+
return $this->nodeNameResolver->isName($propertyFetch->name, $propertyName);
107+
}
108+
109+
return true;
106110
}
107111
}

rules/TypeDeclaration/TypeInferer/PropertyTypeInferer/SetterTypeDeclarationPropertyTypeInferer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public function __construct(
2424

2525
public function inferProperty(Property $property, Class_ $class): ?Type
2626
{
27-
/** @var string $propertyName */
2827
$propertyName = $this->nodeNameResolver->getName($property);
2928

3029
foreach ($class->getMethods() as $classMethod) {

rules/Unambiguous/Rector/Class_/RemoveReturnThisFromSetterClassMethodRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function refactor(Node $node): ?Class_
101101
continue;
102102
}
103103

104-
if (! $this->classMethodAndPropertyAnalyzer->hasPropertyAssignWithReturnThis($classMethod, $paramName)) {
104+
if (! $this->classMethodAndPropertyAnalyzer->hasPropertyAssignWithReturnThis($classMethod)) {
105105
continue;
106106
}
107107

0 commit comments

Comments
 (0)