Skip to content

Commit

Permalink
[Performance] Remove NODES_TO_MATCH constant on RemoveNonExistingVarA…
Browse files Browse the repository at this point in the history
…nnotationRector (#4267)
  • Loading branch information
samsonasik committed Jun 18, 2023
1 parent 0d9828f commit 392898c
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\MultiInstanceofChecker;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -34,25 +33,8 @@
*/
final class RemoveNonExistingVarAnnotationRector extends AbstractRector
{
/**
* @var array<class-string<Node>>
*/
private const NODES_TO_MATCH = [
Foreach_::class,
Static_::class,
Echo_::class,
Return_::class,
Expression::class,
Throw_::class,
If_::class,
While_::class,
Switch_::class,
Nop::class,
];

public function __construct(
private readonly ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer,
private readonly MultiInstanceofChecker $multiInstanceofChecker
private readonly ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer
) {
}

Expand Down Expand Up @@ -92,7 +74,18 @@ public function get()
*/
public function getNodeTypes(): array
{
return [Node::class];
return [
Foreach_::class,
Static_::class,
Echo_::class,
Return_::class,
Expression::class,
Throw_::class,
If_::class,
While_::class,
Switch_::class,
Nop::class,
];
}

public function refactor(Node $node): ?Node
Expand Down Expand Up @@ -158,15 +151,7 @@ private function isUsedInNextNodeWithExtractPreviouslyCalled(Node $node, string

private function shouldSkip(Node $node): bool
{
if (! $node instanceof Nop) {
return ! $this->multiInstanceofChecker->isInstanceOf($node, self::NODES_TO_MATCH);
}

if (count($node->getComments()) <= 1) {
return ! $this->multiInstanceofChecker->isInstanceOf($node, self::NODES_TO_MATCH);
}

return true;
return count($node->getComments()) !== 1;
}

private function hasVariableName(Node $node, string $variableName): bool
Expand Down

0 comments on commit 392898c

Please sign in to comment.