Skip to content

Commit

Permalink
Re-work CheckTrivialExprVisitor
Browse files Browse the repository at this point in the history
In php-parser 5.0, `ClosureUse` is no longer considered an expression.
This requires changes to Psalm's `CheckTrivialExprVisitor`, which stores
an array of "non-trivial" `Expr` nodes.

However the only use of this array is to count whether or not it's
empty. Instead of keeping the array, we can keep a boolean, and avoid
needing to change the types in this class when we upgrade to php-parser
5.0.
  • Loading branch information
edsrzf committed Jan 29, 2024
1 parent 45f3218 commit 98d98be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function findUnusedAssignment(
$traverser->addVisitor($visitor);
$traverser->traverse([$rhs_exp]);

$rhs_exp_trivial = (count($visitor->getNonTrivialExpr()) === 0);
$rhs_exp_trivial = !$visitor->hasNonTrivialExpr();

if ($rhs_exp_trivial) {
$treat_as_expr = false;
Expand Down
14 changes: 4 additions & 10 deletions src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*/
final class CheckTrivialExprVisitor extends PhpParser\NodeVisitorAbstract
{
/**
* @var array<int, PhpParser\Node\Expr>
*/
protected array $non_trivial_expr = [];
private bool $has_non_trivial_expr = false;

private function checkNonTrivialExpr(PhpParser\Node\Expr $node): bool
{
Expand Down Expand Up @@ -55,7 +52,7 @@ public function enterNode(PhpParser\Node $node): ?int
if ($node instanceof PhpParser\Node\Expr) {
// Check for Non-Trivial Expression first
if ($this->checkNonTrivialExpr($node)) {
$this->non_trivial_expr[] = $node;
$this->has_non_trivial_expr = true;
return PhpParser\NodeTraverser::STOP_TRAVERSAL;
}

Expand All @@ -70,11 +67,8 @@ public function enterNode(PhpParser\Node $node): ?int
return null;
}

/**
* @return array<int, PhpParser\Node\Expr>
*/
public function getNonTrivialExpr(): array
public function hasNonTrivialExpr(): bool
{
return $this->non_trivial_expr;
return $this->has_non_trivial_expr;
}
}

0 comments on commit 98d98be

Please sign in to comment.