From 98d98be4439d9b8b20757125025954df73f3b1ef Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Wed, 10 Jan 2024 21:57:56 +1300 Subject: [PATCH] Re-work CheckTrivialExprVisitor 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. --- .../Statements/UnusedAssignmentRemover.php | 2 +- .../PhpVisitor/CheckTrivialExprVisitor.php | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/UnusedAssignmentRemover.php b/src/Psalm/Internal/Analyzer/Statements/UnusedAssignmentRemover.php index d3aaa7050a7..a82a3fabb13 100644 --- a/src/Psalm/Internal/Analyzer/Statements/UnusedAssignmentRemover.php +++ b/src/Psalm/Internal/Analyzer/Statements/UnusedAssignmentRemover.php @@ -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; diff --git a/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php b/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php index 4fe4afe5269..4179ac0d0e6 100644 --- a/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php @@ -9,10 +9,7 @@ */ final class CheckTrivialExprVisitor extends PhpParser\NodeVisitorAbstract { - /** - * @var array - */ - protected array $non_trivial_expr = []; + private bool $has_non_trivial_expr = false; private function checkNonTrivialExpr(PhpParser\Node\Expr $node): bool { @@ -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; } @@ -70,11 +67,8 @@ public function enterNode(PhpParser\Node $node): ?int return null; } - /** - * @return array - */ - public function getNonTrivialExpr(): array + public function hasNonTrivialExpr(): bool { - return $this->non_trivial_expr; + return $this->has_non_trivial_expr; } }