mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-15 23:52:23 +00:00
[DeadCode][CodeQuality] Handle use RemoveDeadInstanceOfRector + SwitchNegatedTernaryRector with if instanceof check (#201)
Co-authored-by: Robin Richtsfeld <robin.richtsfeld@gmail.com>
This commit is contained in:
parent
3f039f15e8
commit
9dea8d8cd8
|
@ -67,7 +67,7 @@ CODE_SAMPLE
|
|||
|
||||
/**
|
||||
* @param If_ $node
|
||||
* @return null|Stmt[]|If_
|
||||
* @return null|If_
|
||||
*/
|
||||
public function refactor(Node $node)
|
||||
{
|
||||
|
@ -93,10 +93,7 @@ CODE_SAMPLE
|
|||
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);
|
||||
|
@ -108,10 +105,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
if ($if->cond === $instanceof) {
|
||||
return $if->stmts;
|
||||
$this->addNodesBeforeNode($if->stmts, $if);
|
||||
}
|
||||
|
||||
$this->removeNode($if);
|
||||
return null;
|
||||
return $if;
|
||||
}
|
||||
}
|
||||
|
|
27
tests/Issues/Issue6480/Fixture/fixture.php.inc
Normal file
27
tests/Issues/Issue6480/Fixture/fixture.php.inc
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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
tests/Issues/Issue6480/config/configured_rule.php
Normal file
13
tests/Issues/Issue6480/config/configured_rule.php
Normal file
|
@ -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);
|
||||
};
|
Loading…
Reference in New Issue
Block a user