mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 19:30:50 +00:00
[DeadCode] Handle combine RemoveAlwaysTrueIfConditionRector + RemoveDeadConstructorRector (#2324)
This commit is contained in:
parent
7a08f1bbe5
commit
15d93fa815
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
};
|
|
@ -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';
|
||||
|
|
Loading…
Reference in New Issue
Block a user