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,20 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

use Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Source\SomeLock;

final class SkipObjectWithDestructMethod
{
public function run()
{
$lock = $this->createLock();

echo 'foobar';
}

private function createLock(): SomeLock
{
return new SomeLock();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Source;

class SomeLock
{
public function __destruct()
{
// Do important things
}
}
22 changes: 22 additions & 0 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\NodeAnalyzer\VariableAnalyzer;
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -107,6 +110,10 @@ public function refactor(Node $node): null|ClassMethod|Function_
/** @var Assign $assign */
$assign = $currentStmt->expr;

if ($this->isObjectWithDestructMethod($assign->expr)) {
continue;
}

if ($this->hasCallLikeInAssignExpr($assign)) {
// clean safely
$cleanAssignedExpr = $this->cleanCastedExpr($assign->expr);
Expand All @@ -127,6 +134,21 @@ public function refactor(Node $node): null|ClassMethod|Function_
return null;
}

private function isObjectWithDestructMethod(Expr $expr): bool
{
$exprType = $this->getType($expr);
if (! $exprType instanceof ObjectType) {
return false;
}

$classReflection = $exprType->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->hasNativeMethod(MethodName::DESTRUCT);
}

private function cleanCastedExpr(Expr $expr): Expr
{
if (! $expr instanceof Cast) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function enterNode(Node $node): ?Node
if (($node instanceof Ternary || $node instanceof If_) && $this->hasVersionCompareCond($node)) {
if ($node instanceof Ternary) {
$nodes = [$node->else];
if ($node->if instanceof \PhpParser\Node) {
if ($node->if instanceof Node) {
$nodes[] = $node->if;
}
} else {
Expand Down