From bee5b69f7fae3413b959132186a059df5a4f7f9f Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 13 Oct 2021 13:27:56 +0200 Subject: [PATCH] use Attribute feature of PHP-Parser to avoid dealing with dynamic properties --- src/Psalm/Internal/Analyzer/ScopeAnalyzer.php | 2 +- .../Analyzer/Statements/Block/SwitchAnalyzer.php | 3 +-- .../Expression/Call/FunctionCallAnalyzer.php | 3 +-- .../Call/Method/MethodCallPurityAnalyzer.php | 13 ++++++------- .../Expression/Call/MethodCallAnalyzer.php | 3 +-- .../Statements/Expression/Call/NewAnalyzer.php | 3 +-- .../Statements/Expression/ExpressionIdentifier.php | 2 +- .../Internal/PhpVisitor/CheckTrivialExprVisitor.php | 4 ++-- src/Psalm/Internal/Provider/NodeDataProvider.php | 2 +- 9 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php index 32f7232f6..5e32dc4a4 100644 --- a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php @@ -314,7 +314,7 @@ class ScopeAnalyzer } ); - if ($has_default_terminator || isset($stmt->allMatched)) { + if ($has_default_terminator || $stmt->getAttribute('allMatched', false)) { return array_values(array_unique(array_merge($control_actions, $all_case_actions))); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php index 4e948ca16..cf4885607 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php @@ -199,8 +199,7 @@ class SwitchAnalyzer } } - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->allMatched = true; + $stmt->setAttribute('allMatched', true); } elseif ($switch_scope->possibly_redefined_vars) { foreach ($switch_scope->possibly_redefined_vars as $var_id => $type) { if (isset($context->vars_in_scope[$var_id])) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php index 860bf334d..dcde0859b 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php @@ -1011,8 +1011,7 @@ class FunctionCallAnalyzer extends CallAnalyzer // fall through } } else { - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->pure = true; + $stmt->setAttribute('pure', true); } } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php index 2ed1b492e..6ca14459c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallPurityAnalyzer.php @@ -76,7 +76,9 @@ class MethodCallPurityAnalyzer } } elseif (($method_storage->mutation_free || ($method_storage->external_mutation_free - && (isset($stmt->var->external_mutation_free) || isset($stmt->var->pure)))) + && ($stmt->var->getAttribute('external_mutation_free', false) + || $stmt->var->getAttribute('pure', false)) + )) && !$context->inside_unset ) { if ($method_storage->mutation_free @@ -89,12 +91,10 @@ class MethodCallPurityAnalyzer && !$method_storage->assertions && !$method_storage->if_true_assertions ) { - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->memoizable = true; + $stmt->setAttribute('memoizable', true); if ($method_storage->immutable) { - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->pure = true; + $stmt->setAttribute('pure', true); } } @@ -118,8 +118,7 @@ class MethodCallPurityAnalyzer // fall through } } elseif (!$method_storage->mutation_free_inferred) { - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->pure = true; + $stmt->setAttribute('pure', true); } } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php index 1dc8a387d..d3b4b034c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php @@ -213,8 +213,7 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ } if ($result->can_memoize) { - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->memoizable = true; + $stmt->setAttribute('memoizable', true); } } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php index c5e7f1a54..b94f1cce1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php @@ -551,8 +551,7 @@ class NewAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAna } if ($storage->external_mutation_free) { - /** @psalm-suppress UndefinedPropertyAssignment */ - $stmt->external_mutation_free = true; + $stmt->setAttribute('external_mutation_free', true); $stmt_type = $statements_analyzer->node_data->getType($stmt); if ($stmt_type) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/ExpressionIdentifier.php b/src/Psalm/Internal/Analyzer/Statements/Expression/ExpressionIdentifier.php index 4bb79d0f7..0e81a27a1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/ExpressionIdentifier.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/ExpressionIdentifier.php @@ -201,7 +201,7 @@ class ExpressionIdentifier ) { $config = \Psalm\Config::getInstance(); - if ($config->memoize_method_calls || isset($stmt->memoizable)) { + if ($config->memoize_method_calls || $stmt->getAttribute('memoizable', false)) { $lhs_var_name = self::getArrayVarId( $stmt->var, $this_class_name, diff --git a/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php b/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php index 3aeb0ed61..8f2011d5a 100644 --- a/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php @@ -38,12 +38,12 @@ class CheckTrivialExprVisitor extends PhpParser\NodeVisitorAbstract if (($node instanceof PhpParser\Node\Expr\FuncCall || $node instanceof PhpParser\Node\Expr\MethodCall || $node instanceof PhpParser\Node\Expr\StaticCall) - && isset($node->pure) + && $node->getAttribute('pure', false) ) { return false; } - if ($node instanceof PhpParser\Node\Expr\New_ && isset($node->external_mutation_free)) { + if ($node instanceof PhpParser\Node\Expr\New_ && $node->getAttribute('external_mutation_free', false)) { return false; } diff --git a/src/Psalm/Internal/Provider/NodeDataProvider.php b/src/Psalm/Internal/Provider/NodeDataProvider.php index ce95d7b00..297502722 100644 --- a/src/Psalm/Internal/Provider/NodeDataProvider.php +++ b/src/Psalm/Internal/Provider/NodeDataProvider.php @@ -114,7 +114,7 @@ class NodeDataProvider implements \Psalm\NodeTypeProvider { $node_type = $this->getType($node); - return ($node_type && $node_type->reference_free) || isset($node->pure); + return ($node_type && $node_type->reference_free) || $node->getAttribute('pure', false); } public function clearNodeOfTypeAndAssertions(Node\Expr $node) : void