Skip to content

Commit

Permalink
Merge ParentFinder to BetterNodeFinder (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 6, 2021
1 parent 824df97 commit 8e6701c
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 135 deletions.
6 changes: 2 additions & 4 deletions packages/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ final class ContextAnalyzer
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeTypeResolver $nodeTypeResolver,
private ParentFinder $parentFinder
) {
}

public function isInLoop(Node $node): bool
{
$stopNodes = array_merge(self::LOOP_NODES, self::BREAK_NODES);

$firstParent = $this->parentFinder->findByTypes($node, $stopNodes);
$firstParent = $this->betterNodeFinder->findParentByTypes($node, $stopNodes);
if (! $firstParent instanceof Node) {
return false;
}
Expand All @@ -68,8 +67,7 @@ public function isInIf(Node $node): bool
{
$breakNodes = array_merge([If_::class], self::BREAK_NODES);

$previousNode = $this->parentFinder->findByTypes($node, $breakNodes);

$previousNode = $this->betterNodeFinder->findParentByTypes($node, $breakNodes);
if (! $previousNode instanceof Node) {
return false;
}
Expand Down
41 changes: 0 additions & 41 deletions packages/NodeNestingScope/ParentFinder.php

This file was deleted.

5 changes: 3 additions & 2 deletions packages/NodeNestingScope/ParentScopeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;

final class ParentScopeFinder
{
public function __construct(
private ParentFinder $parentFinder
private BetterNodeFinder $betterNodeFinder,
) {
}

public function find(Node $node): ClassMethod | Function_ | Class_ | Namespace_ | Closure | null
{
return $this->parentFinder->findByTypes($node, [
return $this->betterNodeFinder->findParentByTypes($node, [
Closure::class,
Function_::class,
ClassMethod::class,
Expand Down
7 changes: 3 additions & 4 deletions packages/NodeNestingScope/ScopeNestingComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ final class ScopeNestingComparator
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeComparator $nodeComparator,
private ParentFinder $parentFinder
) {
}

public function areReturnScopeNested(Return_ $return, Node $secondNodeScopeNode): bool
{
$firstNodeScopeNode = $this->parentFinder->findByTypes(
$firstNodeScopeNode = $this->betterNodeFinder->findParentByTypes(
$return,
ControlStructure::RETURN_ISOLATING_SCOPE_NODE_TYPES
);
Expand All @@ -48,7 +47,7 @@ public function areScopeNestingEqual(Node $firstNode, Node $secondNode): bool

public function isNodeConditionallyScoped(Expr $expr): bool
{
$foundParent = $this->parentFinder->findByTypes(
$foundParent = $this->betterNodeFinder->findParentByTypes(
$expr,
ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES + [FunctionLike::class]
);
Expand Down Expand Up @@ -107,6 +106,6 @@ public function isInBothIfElseBranch(Node $foundParentNode, Expr $seekedExpr): b

private function findParentControlStructure(Node $node): ?Node
{
return $this->parentFinder->findByTypes($node, ControlStructure::BREAKING_SCOPE_NODE_TYPES);
return $this->betterNodeFinder->findParentByTypes($node, ControlStructure::BREAKING_SCOPE_NODE_TYPES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand All @@ -19,15 +18,8 @@ final class FunctionMethodAndClassNodeVisitor extends NodeVisitorAbstract
*/
private array $classStack = [];

/**
* @var ClassMethod[]|null[]
*/
private array $methodStack = [];

private ?ClassLike $classLike = null;

private ?ClassMethod $classMethod = null;

/**
* @param Node[] $nodes
* @return Node[]|null
Expand All @@ -36,15 +28,13 @@ public function beforeTraverse(array $nodes): ?array
{
$this->classLike = null;
$this->className = null;
$this->classMethod = null;

return null;
}

public function enterNode(Node $node): ?Node
{
$this->processClass($node);
$this->processMethod($node);

return $node;
}
Expand All @@ -56,10 +46,6 @@ public function leaveNode(Node $node)
$this->setClassNodeAndName($classLike);
}

if ($node instanceof ClassMethod) {
$this->classMethod = array_pop($this->methodStack);
}

return null;
}

Expand All @@ -73,17 +59,6 @@ private function processClass(Node $node): void
$node->setAttribute(AttributeKey::CLASS_NAME, $this->className);
}

private function processMethod(Node $node): void
{
if ($node instanceof ClassMethod) {
$this->methodStack[] = $this->classMethod;

$this->classMethod = $node;
}

$node->setAttribute(AttributeKey::METHOD_NODE, $this->classMethod);
}

private function setClassNodeAndName(?ClassLike $classLike): void
{
$this->classLike = $classLike;
Expand Down
6 changes: 3 additions & 3 deletions packages/ReadWrite/NodeAnalyzer/ReadWritePropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PhpParser\Node\Stmt\Unset_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\NodeNestingScope\ParentFinder;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\ReadWrite\Guard\VariableToConstantGuard;

Expand All @@ -27,7 +27,7 @@ public function __construct(
private VariableToConstantGuard $variableToConstantGuard,
private AssignManipulator $assignManipulator,
private ReadExprAnalyzer $readExprAnalyzer,
private ParentFinder $parentFinder
private BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -68,7 +68,7 @@ private function unwrapPostPreIncDec(Node $node): Node

private function isNotInsideIssetUnset(ArrayDimFetch $arrayDimFetch): bool
{
return ! (bool) $this->parentFinder->findByTypes($arrayDimFetch, [Isset_::class, Unset_::class]);
return ! (bool) $this->betterNodeFinder->findParentByTypes($arrayDimFetch, [Isset_::class, Unset_::class]);
}

private function isArrayDimFetchRead(ArrayDimFetch $arrayDimFetch): bool
Expand Down
4 changes: 1 addition & 3 deletions rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ParentFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
Expand All @@ -45,7 +44,6 @@ public function __construct(
private NodeTypeResolver $nodeTypeResolver,
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private TypeFactory $typeFactory,
private ParentFinder $parentFinder
) {
}

Expand Down Expand Up @@ -147,7 +145,7 @@ private function normalizeToSingleType(array $propertyNameToTypes): array

private function shouldSkipForLaravelCollection(PropertyFetch $propertyFetch): bool
{
$staticCallOrClassMethod = $this->parentFinder->findByTypes(
$staticCallOrClassMethod = $this->betterNodeFinder->findParentByTypes(
$propertyFetch,
[ClassMethod::class, StaticCall::class]
);
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/NodeFinder/NextVariableUsageNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
public function find(Assign $assign): ?Node
{
$scopeNode = $this->parentScopeFinder->find($assign);
if ($scopeNode === null) {
if (! $scopeNode instanceof Node) {
return null;
}

Expand Down
4 changes: 1 addition & 3 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\NodeNestingScope\ParentFinder;
use Rector\NodeNestingScope\ScopeNestingComparator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -29,7 +28,6 @@ final class RemoveDoubleAssignRector extends AbstractRector
public function __construct(
private ScopeNestingComparator $scopeNestingComparator,
private SideEffectNodeDetector $sideEffectNodeDetector,
private ParentFinder $parentFinder
) {
}

Expand Down Expand Up @@ -108,7 +106,7 @@ private function shouldSkipForDifferentScope(Assign $assign, Expression $express
return true;
}

return (bool) $this->parentFinder->findByTypes($assign, [Ternary::class, Coalesce::class]);
return (bool) $this->betterNodeFinder->findParentByTypes($assign, [Ternary::class, Coalesce::class]);
}

private function isSelfReferencing(Assign $assign): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Stmt\Function_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeNestingScope\ParentFinder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -25,7 +24,6 @@
final class DowngradeDefineArrayConstantRector extends AbstractRector
{
public function __construct(
private ParentFinder $parentFinder,
private ArgsAnalyzer $argsAnalyzer
) {
}
Expand Down Expand Up @@ -113,6 +111,6 @@ private function shouldSkip(FuncCall $funcCall): bool
return true;
}

return (bool) $this->parentFinder->findByTypes($funcCall, [ClassMethod::class, Function_::class]);
return (bool) $this->betterNodeFinder->findParentByTypes($funcCall, [ClassMethod::class, Function_::class]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Rector\EarlyReturn\NodeFactory\InvertedIfFactory;
use Rector\NodeCollector\NodeAnalyzer\BooleanAndAnalyzer;
use Rector\NodeNestingScope\ContextAnalyzer;
use Rector\NodeNestingScope\ParentFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -33,7 +32,6 @@ public function __construct(
private InvertedIfFactory $invertedIfFactory,
private ContextAnalyzer $contextAnalyzer,
private BooleanAndAnalyzer $booleanAndAnalyzer,
private ParentFinder $parentFinder
) {
}

Expand Down Expand Up @@ -243,7 +241,7 @@ private function isNestedIfInLoop(If_ $if): bool
return false;
}

return (bool) $this->parentFinder->findByTypes($if, [If_::class, Else_::class, ElseIf_::class]);
return (bool) $this->betterNodeFinder->findParentByTypes($if, [If_::class, Else_::class, ElseIf_::class]);
}

private function isLastIfOrBeforeLastReturn(If_ $if): bool
Expand Down
9 changes: 6 additions & 3 deletions rules/Naming/Matcher/ForeachMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Naming\ValueObject\VariableAndCallForeach;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ParentFinder;

final class ForeachMatcher
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private CallMatcher $callMatcher,
private ParentFinder $parentFinder
private BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -48,6 +48,9 @@ public function match(Foreach_ $foreach): ?VariableAndCallForeach

private function getFunctionLike(Foreach_ $foreach): ClassMethod | Function_ | Closure | null
{
return $this->parentFinder->findByTypes($foreach, [Closure::class, ClassMethod::class, Function_::class]);
return $this->betterNodeFinder->findParentByTypes(
$foreach,
[Closure::class, ClassMethod::class, Function_::class]
);
}
}
9 changes: 6 additions & 3 deletions rules/Naming/Matcher/VariableAndCallAssignMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Naming\ValueObject\VariableAndCallAssign;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ParentFinder;

final class VariableAndCallAssignMatcher
{
public function __construct(
private CallMatcher $callMatcher,
private NodeNameResolver $nodeNameResolver,
private ParentFinder $parentFinder
private BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -49,6 +49,9 @@ public function match(Assign $assign): ?VariableAndCallAssign

private function getFunctionLike(Assign $assign): ClassMethod | Function_ | Closure | null
{
return $this->parentFinder->findByTypes($assign, [Closure::class, ClassMethod::class, Function_::class]);
return $this->betterNodeFinder->findParentByTypes(
$assign,
[Closure::class, ClassMethod::class, Function_::class]
);
}
}
Loading

0 comments on commit 8e6701c

Please sign in to comment.