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
@@ -0,0 +1,15 @@
<?php

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

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

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

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

class SetNameCaller
{
public function setName(): SurnameCaller
{
return new SurnameCaller();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class SurnameCaller
{
public function setSurname(): self
{
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,28 @@ public function refactor(Node $node): ?array
$methodCalls = [];

$currentMethodCall = $firstMethodCall;
$classNameObjectType = null;
while ($currentMethodCall instanceof MethodCall) {
if ($currentMethodCall->isFirstClassCallable()) {
return null;
}

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

$objectType = $this->getType($currentMethodCall->var);
if (! $objectType instanceof ObjectType) {
return null;
}

if ($classNameObjectType === null) {
$classNameObjectType = $objectType->getClassName();
} elseif ($classNameObjectType !== $objectType->getClassName()) {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fix 👍 Could you extract this to a standalone service? I'll use it more often

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$methodCalls[] = $currentMethodCall;
$currentMethodCall = $currentMethodCall->var;
}
Expand Down