Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ final class SomeClass
public function setup()
{
$someSetterClass = new SomeSetterClass();
$someSetterClass->setSurname('Doe');
$someSetterClass->setName('John');
$someSetterClass->setSurname('Doe');
return $someSetterClass;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;

use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\GetterSetterClass;

final class SkipGetterSetter
{
public function setup()
{
return (new GetterSetterClass())
->getter()
->setName('John')
->setSurname('Doe');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source\SomeSetterClass;

final class SkipMocksOnVariable extends TestCase
{
public function test()
{
$someVariable = $this->createMock(SomeSetterClass::class);
$someVariable->expects($this->once())
->method('some')
->with(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Unambiguous\Rector\Expression\FluentSettersToStandaloneCallMethodRector\Source;

final class GetterSetterClass
{
public function getter()
{
return new SomeSetterClass();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Unambiguous\Rector\Expression;

use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
Expand All @@ -15,6 +14,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -98,6 +98,11 @@ public function refactor(Node $node): ?array

$currentMethodCall = $firstMethodCall;
while ($currentMethodCall instanceof MethodCall) {
// must be exactly one argument
if (count($currentMethodCall->getArgs()) !== 1) {
return null;
}

$methodCalls[] = $currentMethodCall;
$currentMethodCall = $currentMethodCall->var;
}
Expand All @@ -119,13 +124,21 @@ public function refactor(Node $node): ?array
$firstAssign = new Assign($someVariable, $rootExpr);
$stmts = [new Expression($firstAssign)];

// revert to normal order
$methodCalls = array_reverse($methodCalls);

foreach ($methodCalls as $methodCall) {
$methodCall->var = $someVariable;
// inlines indent and removes () around first expr
$methodCall->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$stmts[] = new Expression($methodCall);
}

if ($node instanceof Return_) {
$node->expr = $someVariable;
$stmts[] = $node;
}

$node->expr = $someVariable;

return $stmts;
Expand Down