Skip to content

Commit

Permalink
[DX] Check only for current scope in RemoveUnusedVariableInCatchRecto…
Browse files Browse the repository at this point in the history
…r, as… magic exception use is bad practise and should be avoided (#4449)
  • Loading branch information
TomasVotruba committed Jul 9, 2023
1 parent ce03029 commit cfc88dc
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 71 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,6 @@ parameters:
- src/NodeManipulator/PropertyManipulator.php

- '#Fetching class constant class of deprecated class Rector\\Core\\Configuration\\Parameter\\ParameterProvider#'

# handle in next PR
- '#Call to deprecated method findFirstNext\(\) of class Rector\\Core\\PhpParser\\Node\\BetterNodeFinder#'

This file was deleted.

33 changes: 4 additions & 29 deletions rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Catch_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -21,11 +20,6 @@
*/
final class RemoveUnusedVariableInCatchRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove unused variable in catch()', [
Expand Down Expand Up @@ -75,11 +69,11 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isVariableUsedInStmts($node->stmts, $caughtVar)) {
return null;
}
/** @var string $variableName */
$variableName = $this->getName($caughtVar);

if ($this->isVariableUsedNext($node, $caughtVar)) {
$isVariableUsed = (bool) $this->betterNodeFinder->findVariableOfName($node->stmts, $variableName);
if ($isVariableUsed) {
return null;
}

Expand All @@ -92,23 +86,4 @@ public function provideMinPhpVersion(): int
{
return PhpVersionFeature::NON_CAPTURING_CATCH;
}

/**
* @param Node[] $nodes
*/
private function isVariableUsedInStmts(array $nodes, Variable $variable): bool
{
return (bool) $this->betterNodeFinder->findFirst(
$nodes,
fn (Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $variable)
);
}

private function isVariableUsedNext(Catch_ $catch, Variable $variable): bool
{
return (bool) $this->betterNodeFinder->findFirstNext(
$catch,
fn (Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $variable)
);
}
}
18 changes: 1 addition & 17 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Rector\Core\NodeAnalyzer\ScopeAnalyzer;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;

/**
Expand Down Expand Up @@ -60,22 +59,7 @@ public function refresh(Node $node, ?MutatingScope $mutatingScope, ?string $file
$mutatingScope = $this->scopeAnalyzer->resolveScope($node, $filePath, $mutatingScope);

if (! $mutatingScope instanceof MutatingScope) {
/**
* @var Node $parentNode
*
* $parentNode is always a Node when $mutatingScope is null, as checked in previous
*
* $this->scopeAnalyzer->resolveScope()
*
* which verify if no parent and no scope, it resolve Scope from File
*/
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

$errorMessage = sprintf(
'Node "%s" with parent of "%s" is missing scope required for scope refresh.',
$node::class,
$parentNode::class
);
$errorMessage = sprintf('Node "%s" with is missing scope required for scope refresh', $node::class);

throw new ShouldNotHappenException($errorMessage);
}
Expand Down
2 changes: 2 additions & 0 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public function findFirst(Node | array $nodes, callable $filter): ?Node
}

/**
* @deprecated Use node traversing instead
*
* @param callable(Node $node): bool $filter
*/
public function findFirstNext(Node $node, callable $filter): ?Node
Expand Down

0 comments on commit cfc88dc

Please sign in to comment.