Skip to content

Commit

Permalink
[DeadCode] Skip fluent no return type on RemoveUnusedPrivateMethodRec…
Browse files Browse the repository at this point in the history
…tor (#6108)

* [DeadCode] Skip fluent no return type on RemoveUnusedPrivateMethodRector

* comment
  • Loading branch information
samsonasik committed Jul 3, 2024
1 parent 2b2bbd1 commit eccf502
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;

class SkipFluentNoReturnType
{
public function foo()
{
return $this
->bar()
->baz()
->qux();
}

private function bar()
{
return $this;
}

private function baz()
{
return $this;
}

public function qux()
{
return $this;
}
}
16 changes: 16 additions & 0 deletions rules/DeadCode/NodeAnalyzer/CallCollectionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Type\MixedType;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeWithClassName;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -32,6 +34,20 @@ public function isExists(array $calls, string $classMethodName, string $classNam
$callerType = $this->nodeTypeResolver->getType($callerRoot);

if (! $callerType instanceof TypeWithClassName) {
// handle fluent by $this->bar()->baz()->qux()
// that methods don't have return type
if ($callerType instanceof MixedType && ! $callerType->isExplicitMixed()) {
$cloneCallerRoot = clone $callerRoot;
while ($cloneCallerRoot instanceof MethodCall && $cloneCallerRoot->var instanceof MethodCall) {
$callerType = $this->nodeTypeResolver->getType($cloneCallerRoot->var->var);
$cloneCallerRoot = $cloneCallerRoot->var;

if ($callerType instanceof ThisType && $callerType->getStaticObjectType()->getClassName() === $className) {
return true;
}
}
}

continue;
}

Expand Down

0 comments on commit eccf502

Please sign in to comment.