Skip to content

Commit

Permalink
[Instanceof] Fix combination of dead instance and compare (#5319)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 3, 2023
1 parent 026398c commit 8e6d46a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\DeadCode\Rector\If_;

use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BooleanNot;
Expand Down Expand Up @@ -113,6 +115,11 @@ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): nu
return null;
}

if ($instanceof->expr instanceof Assign) {
$assignExpression = new Expression($instanceof->expr);
return array_merge([$assignExpression], $if->stmts);
}

if ($if->cond !== $instanceof) {
return NodeTraverser::REMOVE_NODE;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/DeadInstanceFlip/DeadInstanceFlipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DeadInstanceFlip;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DeadInstanceFlipTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
34 changes: 34 additions & 0 deletions tests/Issues/DeadInstanceFlip/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rector\Core\Tests\Issues\DeadInstanceFlip\Fixture;

use Rector\Core\Tests\Issues\DeadInstanceFlip\Source\SomeEvent;

final class Fixture
{
public function process(SomeEvent $someEvent)
{
if (null !== $class = $someEvent->getSomeType()) {
$class->result;
}
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\DeadInstanceFlip\Fixture;

use Rector\Core\Tests\Issues\DeadInstanceFlip\Source\SomeEvent;

final class Fixture
{
public function process(SomeEvent $someEvent)
{
$class = $someEvent->getSomeType();
$class->result;
}
}

?>
11 changes: 11 additions & 0 deletions tests/Issues/DeadInstanceFlip/Source/SomeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Core\Tests\Issues\DeadInstanceFlip\Source;

class SomeEvent
{
public function getSomeType(): SomeEventType
{

}
}
8 changes: 8 additions & 0 deletions tests/Issues/DeadInstanceFlip/Source/SomeEventType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Core\Tests\Issues\DeadInstanceFlip\Source;

final class SomeEventType
{
public $result;
}
11 changes: 11 additions & 0 deletions tests/Issues/DeadInstanceFlip/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([RemoveDeadInstanceOfRector::class, FlipTypeControlToUseExclusiveTypeRector::class]);
};

0 comments on commit 8e6d46a

Please sign in to comment.