Skip to content

Commit

Permalink
Merge pull request #1645 from staabm/finder
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 7, 2022
2 parents e33afe6 + 7af077b commit 0af102a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,24 @@ private function clearNotNullBeforeCount(Class_ $class, array $propertyNames): v

$isNextNodeCountingProperty = (bool) $this->betterNodeFinder->findFirst($node->right, function (Node $node) use (
$propertyNames
): ?bool {
): bool {
if (! $node instanceof FuncCall) {
return null;
return false;
}

if (! $this->isName($node, 'count')) {
return null;
return false;
}

if (! $this->argsAnalyzer->isArgInstanceInArgsPosition($node->args, 0)) {
return null;
return false;
}

/** @var Arg $firstArg */
$firstArg = $node->args[0];
$countedArgument = $firstArg->value;
if (! $countedArgument instanceof PropertyFetch) {
return null;
return false;
}

return $this->isNames($countedArgument, $propertyNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ private function isProbablyMysql(Expr $expr): bool

private function findConnectionVariable(FuncCall $funcCall): ?Expr
{
$connectionAssign = $this->betterNodeFinder->findFirstPrevious($funcCall, function (Node $node): ?bool {
$connectionAssign = $this->betterNodeFinder->findFirstPrevious($funcCall, function (Node $node): bool {
if (! $node instanceof Assign) {
return null;
return false;
}

return $this->isObjectType($node->expr, new ObjectType('mysqli'));
Expand Down
13 changes: 13 additions & 0 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public function findFirstNonAnonymousClass(array $nodes): ?Node

/**
* @param Node|Node[] $nodes
* @param callable(Node $node): bool $filter
*/
public function findFirst(Node | array $nodes, callable $filter): ?Node
{
Expand Down Expand Up @@ -274,6 +275,9 @@ public function findPreviousAssignToExpr(Expr $expr): ?Node
});
}

/**
* @param callable(Node $node): bool $filter
*/
public function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
{
// move to previous expression
Expand All @@ -300,6 +304,9 @@ public function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
return null;
}

/**
* @param callable(Node $node): bool $filter
*/
public function findFirstPrevious(Node $node, callable $filter): ?Node
{
$node = $node instanceof Expression ? $node : $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
Expand Down Expand Up @@ -339,6 +346,9 @@ public function findFirstPreviousOfTypes(Node $mainNode, array $types): ?Node
);
}

/**
* @param callable(Node $node): bool $filter
*/
public function findFirstNext(Node $node, callable $filter): ?Node
{
$next = $node->getAttribute(AttributeKey::NEXT_NODE);
Expand Down Expand Up @@ -530,6 +540,9 @@ private function findInstanceOfName(Node | array $nodes, string $type, string $n
return null;
}

/**
* @return Closure|Function_|ClassMethod|Class_|Namespace_|null
*/
private function findParentScope(Node $node): Node|null
{
return $this->findParentByTypes($node, [
Expand Down

0 comments on commit 0af102a

Please sign in to comment.