[DeadCode][EarlyReturn] Handle RemoveUnusedPrivateMethodRector + RemoveAlwaysElseRector (#807)

* added failing test fixture

* update

* update

* Closes #801 Fixes https://github.com/rectorphp/rector/issues/6670

Co-authored-by: Bl00D4NGEL <kuhlesdominik@gmx.de>
This commit is contained in:
Abdul Malik Ikhsan 2021-09-01 14:17:01 +07:00 committed by GitHub
parent 0f9e4f8360
commit b1d573e8b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 1 deletions

View File

@ -78,6 +78,10 @@ CODE_SAMPLE
return null;
}
if ($this->nodesToAddCollector->isActive()) {
return null;
}
$this->removeNode($node);
return $node;

View File

@ -122,7 +122,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
*/
private $previousAppliedClass;
private NodesToAddCollector $nodesToAddCollector;
protected NodesToAddCollector $nodesToAddCollector;
private CurrentFileProvider $currentFileProvider;

View File

@ -0,0 +1,42 @@
<?php
namespace Rector\Core\Tests\Issues\Issue6670\Fixture;
final class DoNotRemoveUsedClassMethod
{
public function doSomething(): int
{
if (true === false) {
return -1;
} else {
return $this->notUnused();
}
}
private function notUnused(): int
{
// This is some code that is very important
}
}
?>
-----
<?php
namespace Rector\Core\Tests\Issues\Issue6670\Fixture;
final class DoNotRemoveUsedClassMethod
{
public function doSomething(): int
{
if (true === false) {
return -1;
}
return $this->notUnused();
}
private function notUnused(): int
{
// This is some code that is very important
}
}
?>

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\Issue6670;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @see https://github.com/rectorphp/rector/issues/6670
*/
final class RemoveAlwaysElseAndUnusedPrivateMethodsRectorTest 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,13 @@
<?php
declare(strict_types=1);
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveUnusedPrivateMethodRector::class);
$services->set(RemoveAlwaysElseRector::class);
};