Skip to content

Commit

Permalink
[DeadCode] Skip fluent return $this on RemoveEmptyMethodCallRector (#521
Browse files Browse the repository at this point in the history
)
  • Loading branch information
samsonasik committed Jul 27, 2021
1 parent e5d95bd commit 3a96b90
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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);

?>
17 changes: 16 additions & 1 deletion rules/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$scope = $node->var->getAttribute(AttributeKey::SCOPE);
$scope = $this->getScope($node);

if (! $scope instanceof Scope) {
return null;
}
Expand Down Expand Up @@ -114,6 +115,20 @@ public function refactor(Node $node): ?Node
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_) {
Expand Down

0 comments on commit 3a96b90

Please sign in to comment.