Skip to content

Commit

Permalink
[DeadCode][EarlyReturn] Handle RemoveUnusedVariableAssignRector + Rem…
Browse files Browse the repository at this point in the history
…oveAlwaysElseRector (#1277)

* [DeadCode][EarlyReturn] Handle RemoveUnusedVariableAssignRector + RemoveAlwaysElseRector

* update fixture

* eol

* Fixed 🎉

* comment

* Trigger notification

* check in ExprUsedInNextNodeAnalyzer to handle error in parallel test

* comment

* clean up

* phpstan

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fix

* move comment

* [ci-review] Rector Rectify

* Trigger notification

* try 3 process

* try 3 process

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 20, 2021
1 parent 7acec2c commit bc5462b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ jobs:

- uses: "ramsey/composer-install@v1"

# with --runner=WrapperRunner, it is faster, only ideal with 4 processes (p4)
# with --runner=WrapperRunner, it is faster, only ideal with 3 processes (p3)
# @see https://github.com/rectorphp/rector-src/pull/551#issuecomment-889990905
- run: vendor/bin/paratest -p5 --runner=WrapperRunner ${{ matrix.path }} --colors
- run: vendor/bin/paratest -p3 --runner=WrapperRunner ${{ matrix.path }} --colors
if: ${{ matrix.path == 'rules-tests' }}

- run: vendor/bin/phpunit ${{ matrix.path }}
Expand Down
21 changes: 20 additions & 1 deletion rules/DeadCode/NodeAnalyzer/ExprUsedInNextNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ExprUsedInNextNodeAnalyzer
Expand Down Expand Up @@ -39,8 +41,25 @@ function (Node $node) use ($expr, $isCheckNameScope): bool {
}
}

return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
if (! $node instanceof If_) {
return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
}

/**
* handle when used along with RemoveAlwaysElseRector
*/
if (! $this->hasIfChangedByRemoveAlwaysElseRector($node)) {
return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
}

return true;
}
);
}

private function hasIfChangedByRemoveAlwaysElseRector(If_ $if): bool
{
$createdByRule = $if->getAttribute(AttributeKey::CREATED_BY_RULE);
return $createdByRule === RemoveAlwaysElseRector::class;
}
}
4 changes: 4 additions & 0 deletions rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -81,6 +82,7 @@ public function refactor(Node $node): ?Node
$originalNode = clone $node;
$if = new If_($node->cond);
$if->stmts = $node->stmts;
$node->setAttribute(AttributeKey::CREATED_BY_RULE, self::class);

$this->nodesToAddCollector->addNodeBeforeNode($if, $node);
$this->mirrorComments($if, $node);
Expand All @@ -107,6 +109,8 @@ public function refactor(Node $node): ?Node
if ($node->else !== null) {
$this->nodesToAddCollector->addNodesAfterNode($node->else->stmts, $node);
$node->else = null;

$node->setAttribute(AttributeKey::CREATED_BY_RULE, self::class);
return $node;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\RemoveUnusedVariableAlwaysElse\Fixture;

final class Fixture
{
public function run(Request $request)
{
$path = $request->get('path', null);
$url = $request->get('url', null);
if (null !== $path) {
return response()->file($path);
} elseif (null !== $url) {
return response()->getFile($url);
}
throw new \Exception('Error');
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\RemoveUnusedVariableAlwaysElse\Fixture;

final class Fixture
{
public function run(Request $request)
{
$path = $request->get('path', null);
$url = $request->get('url', null);
if (null !== $path) {
return response()->file($path);
}
if (null !== $url) {
return response()->getFile($url);
}
throw new \Exception('Error');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\RemoveUnusedVariableAlwaysElse;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RemoveUnusedVariableAlwaysElseTest 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveUnusedVariableAssignRector::class);
$services->set(RemoveAlwaysElseRector::class);
};

0 comments on commit bc5462b

Please sign in to comment.