[DeadCode] Handle combine RemoveAlwaysTrueIfConditionRector + RemoveDeadConstructorRector (#2324)

This commit is contained in:
Abdul Malik Ikhsan 2022-05-17 04:14:55 +07:00 committed by GitHub
parent 7a08f1bbe5
commit 15d93fa815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 3 deletions

View File

@ -7,6 +7,7 @@ namespace Rector\Core\ProcessAnalyzer;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\RectifiedNode;
@ -24,6 +25,10 @@ final class RectifiedAnalyzer
*/
private array $previousFileWithNodes = [];
public function __construct(private readonly NodeComparator $nodeComparator)
{
}
public function verify(RectorInterface $rector, Node $node, File $currentFile): ?RectifiedNode
{
$smartFileInfo = $currentFile->getSmartFileInfo();
@ -47,8 +52,12 @@ final class RectifiedAnalyzer
private function shouldContinue(RectifiedNode $rectifiedNode, RectorInterface $rector, Node $node): bool
{
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
if ($rectifiedNode->getRectorClass() === $rector::class && $rectifiedNode->getNode() === $node) {
return false;
/**
* allow to revisit the Node with same Rector rule if Node is changed by other rule
*/
return ! $this->nodeComparator->areNodesEqual($originalNode, $node);
}
if ($rector instanceof AbstractScopeAwareRector) {
@ -56,7 +65,6 @@ final class RectifiedAnalyzer
return $scope instanceof Scope;
}
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
if ($originalNode instanceof Node) {
return true;
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\AlwaysTrueIfInDeadConstructor;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class AlwaysTrueIfInDeadConstructorTest 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';
}
}

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\AlwaysTrueIfInDeadConstructor\Fixture;
final class Fixture
{
public function __construct()
{
if (1 === 1) {
}
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\AlwaysTrueIfInDeadConstructor\Fixture;
final class Fixture
{
}
?>

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveAlwaysTrueIfConditionRector::class);
$rectorConfig->rule(RemoveDeadConstructorRector::class);
};

View File

@ -28,7 +28,10 @@ class ComplexIfCondOrWithoutElseIf
{
public function run($a, $b, $c)
{
if (($a && false === $b) || ! $c) {
if ($a && false === $b) {
return 'a';
}
if (! $c) {
return 'a';
}
return 'b';