Skip to content

Commit 65b80d6

Browse files
authored
[Unambiguous] Skip return different object on FluentSettersToStandaloneCallMethodRector (#7630)
* [Unambiguous] Skip return different object on FluentSettersToStandaloneCallMethodRector * fix
1 parent 46618df commit 65b80d6

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;
4+
5+
use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\SetNameCaller;
6+
7+
class SkipReturnDifferentObject
8+
{
9+
public function setup()
10+
{
11+
return (new SetNameCaller())
12+
->setName('John')
13+
->setSurname('Doe');
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source;
4+
5+
class SetNameCaller
6+
{
7+
public function setName(): SurnameCaller
8+
{
9+
return new SurnameCaller();
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source;
4+
5+
class SurnameCaller
6+
{
7+
public function setSurname(): self
8+
{
9+
return $this;
10+
}
11+
}

rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,28 @@ public function refactor(Node $node): ?array
9898
$methodCalls = [];
9999

100100
$currentMethodCall = $firstMethodCall;
101+
$classNameObjectType = null;
101102
while ($currentMethodCall instanceof MethodCall) {
103+
if ($currentMethodCall->isFirstClassCallable()) {
104+
return null;
105+
}
106+
102107
// must be exactly one argument
103108
if (count($currentMethodCall->getArgs()) !== 1) {
104109
return null;
105110
}
106111

112+
$objectType = $this->getType($currentMethodCall->var);
113+
if (! $objectType instanceof ObjectType) {
114+
return null;
115+
}
116+
117+
if ($classNameObjectType === null) {
118+
$classNameObjectType = $objectType->getClassName();
119+
} elseif ($classNameObjectType !== $objectType->getClassName()) {
120+
return null;
121+
}
122+
107123
$methodCalls[] = $currentMethodCall;
108124
$currentMethodCall = $currentMethodCall->var;
109125
}

0 commit comments

Comments
 (0)