Skip to content

Commit

Permalink
[DeadCode][EarlyReturn] Handle RemoveUnusedPrivateMethodRector + Remo…
Browse files Browse the repository at this point in the history
…veAlwaysElseRector (#807)

* added failing test fixture

* update

* update

* Closes #801 Fixes rectorphp/rector#6670

Co-authored-by: Bl00D4NGEL <kuhlesdominik@gmx.de>
  • Loading branch information
samsonasik and Bl00D4NGEL committed Sep 1, 2021
1 parent 0f9e4f8 commit b1d573e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->nodesToAddCollector->isActive()) {
return null;
}

$this->removeNode($node);

return $node;
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
*/
private $previousAppliedClass;

private NodesToAddCollector $nodesToAddCollector;
protected NodesToAddCollector $nodesToAddCollector;

private CurrentFileProvider $currentFileProvider;

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
?>
Original file line number Diff line number Diff line change
@@ -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';
}
}
13 changes: 13 additions & 0 deletions tests/Issues/Issue6670/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\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);
};

0 comments on commit b1d573e

Please sign in to comment.