[DeadCode] Skip fluent return $this on RemoveEmptyMethodCallRector (#521)

This commit is contained in:
Abdul Malik Ikhsan 2021-07-27 18:05:33 +07:00 committed by GitHub
parent e5d95bda10
commit 3a96b9097d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\Fixture;
class SkipFluentReturnThis
{
/**
* @return $this
*/
public function prepare()
{
return $this;
}
public function run()
{
}
public function call(string $class)
{
/** @var SkipFluentReturnThis $obj */
$obj = new $class();
$obj->prepare()->run();
}
}
class Extended extends SkipFluentReturnThis
{
public function run()
{
echo 'hello';
}
}
(new SkipFluentReturnThis())->call(Extended::class);
?>

View File

@ -76,7 +76,8 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
$scope = $node->var->getAttribute(AttributeKey::SCOPE);
$scope = $this->getScope($node);
if (! $scope instanceof Scope) {
return null;
}
@ -114,6 +115,20 @@ CODE_SAMPLE
return $node;
}
private function getScope(MethodCall $methodCall): ?Scope
{
if ($methodCall->var instanceof MethodCall) {
return null;
}
$scope = $methodCall->var->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}
return $scope;
}
private function shouldSkipClassMethod(Class_ | Trait_ | Interface_ $classLike, MethodCall $methodCall): bool
{
if (! $classLike instanceof Class_) {