Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};