Skip to content

Commit

Permalink
[DeadCode][CodeQuality] Handle use RemoveDeadInstanceOfRector + Switc…
Browse files Browse the repository at this point in the history
…hNegatedTernaryRector with if instanceof check (#201)

Co-authored-by: Robin Richtsfeld <robin.richtsfeld@gmail.com>
  • Loading branch information
samsonasik and Androbin committed Jun 11, 2021
1 parent 3f039f1 commit 9dea8d8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
11 changes: 4 additions & 7 deletions rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getNodeTypes(): array

/**
* @param If_ $node
* @return null|Stmt[]|If_
* @return null|If_
*/
public function refactor(Node $node)
{
Expand All @@ -93,10 +93,7 @@ public function refactor(Node $node)
return $node;
}

/**
* @return Stmt[]|null
*/
private function processMayDeadInstanceOf(If_ $if, Instanceof_ $instanceof): ?array
private function processMayDeadInstanceOf(If_ $if, Instanceof_ $instanceof): ?If_
{
$classType = $this->nodeTypeResolver->resolve($instanceof->class);
$exprType = $this->nodeTypeResolver->resolve($instanceof->expr);
Expand All @@ -108,10 +105,10 @@ private function processMayDeadInstanceOf(If_ $if, Instanceof_ $instanceof): ?ar
}

if ($if->cond === $instanceof) {
return $if->stmts;
$this->addNodesBeforeNode($if->stmts, $if);
}

$this->removeNode($if);
return null;
return $if;
}
}
27 changes: 27 additions & 0 deletions tests/Issues/Issue6480/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class Fixture
{
function test(Foo $foo, mixed $bar): void {
if ($foo instanceof Foo) {
var_dump(!empty($bar) ? $bar : null);
}
}
}

?>
-----
<?php

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

class Fixture
{
function test(Foo $foo, mixed $bar): void {
var_dump(empty($bar) ? null : $bar);
}
}

?>
36 changes: 36 additions & 0 deletions tests/Issues/Issue6480/NegatedTernaryInsideDeadInstanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\Issue6480;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* @see https://github.com/rectorphp/rector/issues/6480
*/
final class NegatedTernaryInsideDeadInstanceTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

declare(strict_types=1);

use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveDeadInstanceOfRector::class);
$services->set(SwitchNegatedTernaryRector::class);
};

0 comments on commit 9dea8d8

Please sign in to comment.