Skip to content

Commit 32f3140

Browse files
authored
Skip setter + getter, keep original stmts order (#7627)
* reverse to keep original order * keep return * skip getter
1 parent e99ea33 commit 32f3140

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

rules-tests/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector/Fixture/fixture.php.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ final class SomeClass
2727
public function setup()
2828
{
2929
$someSetterClass = new SomeSetterClass();
30-
$someSetterClass->setSurname('Doe');
3130
$someSetterClass->setName('John');
31+
$someSetterClass->setSurname('Doe');
32+
return $someSetterClass;
3233
}
3334
}
3435

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;
6+
7+
use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\GetterSetterClass;
8+
9+
final class SkipGetterSetter
10+
{
11+
public function setup()
12+
{
13+
return (new GetterSetterClass())
14+
->getter()
15+
->setName('John')
16+
->setSurname('Doe');
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\SomeSetterClass;
7+
8+
final class SkipMocksOnVariable extends TestCase
9+
{
10+
public function test()
11+
{
12+
$someVariable = $this->createMock(SomeSetterClass::class);
13+
$someVariable->expects($this->once())
14+
->method('some')
15+
->with(1);
16+
}
17+
}
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+
final class GetterSetterClass
6+
{
7+
public function getter()
8+
{
9+
return new SomeSetterClass();
10+
}
11+
}

rules/Unambiguous/Rector/Expression/FluentSettersToStandaloneCallMethodRector.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Rector\Unambiguous\Rector\Expression;
66

7-
use PHPStan\Type\ObjectType;
87
use PhpParser\Node;
98
use PhpParser\Node\Expr;
109
use PhpParser\Node\Expr\Assign;
@@ -15,6 +14,7 @@
1514
use PhpParser\Node\Stmt\Expression;
1615
use PhpParser\Node\Stmt\Return_;
1716
use PHPStan\Reflection\ClassReflection;
17+
use PHPStan\Type\ObjectType;
1818
use Rector\Naming\Naming\PropertyNaming;
1919
use Rector\NodeTypeResolver\Node\AttributeKey;
2020
use Rector\Rector\AbstractRector;
@@ -98,6 +98,11 @@ public function refactor(Node $node): ?array
9898

9999
$currentMethodCall = $firstMethodCall;
100100
while ($currentMethodCall instanceof MethodCall) {
101+
// must be exactly one argument
102+
if (count($currentMethodCall->getArgs()) !== 1) {
103+
return null;
104+
}
105+
101106
$methodCalls[] = $currentMethodCall;
102107
$currentMethodCall = $currentMethodCall->var;
103108
}
@@ -119,13 +124,21 @@ public function refactor(Node $node): ?array
119124
$firstAssign = new Assign($someVariable, $rootExpr);
120125
$stmts = [new Expression($firstAssign)];
121126

127+
// revert to normal order
128+
$methodCalls = array_reverse($methodCalls);
129+
122130
foreach ($methodCalls as $methodCall) {
123131
$methodCall->var = $someVariable;
124132
// inlines indent and removes () around first expr
125133
$methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null);
126134
$stmts[] = new Expression($methodCall);
127135
}
128136

137+
if ($node instanceof Return_) {
138+
$node->expr = $someVariable;
139+
$stmts[] = $node;
140+
}
141+
129142
$node->expr = $someVariable;
130143

131144
return $stmts;

0 commit comments

Comments
 (0)