From 79d0647d1935815a18aa72982f57078df7104ffe Mon Sep 17 00:00:00 2001 From: orklah Date: Sat, 17 Jul 2021 22:00:54 +0200 Subject: [PATCH 1/3] fix issue when setting a value to a global is considered unused --- src/Psalm/Context.php | 5 +++++ src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php | 1 + src/Psalm/Internal/Analyzer/StatementsAnalyzer.php | 5 +++-- tests/UnusedVariableTest.php | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Context.php b/src/Psalm/Context.php index 1101b8fe555..ab482acac32 100644 --- a/src/Psalm/Context.php +++ b/src/Psalm/Context.php @@ -367,6 +367,11 @@ class Context */ public $has_returned = false; + /** + * @var array + */ + public $vars_from_global; + public function __construct(?string $self = null) { $this->self = $self; diff --git a/src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php index dc27c13ae0e..6ebb3d34beb 100644 --- a/src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php @@ -65,6 +65,7 @@ public static function analyze( $context->vars_in_scope[$var_id]->parent_nodes = [ $assignment_node->id => $assignment_node, ]; + $context->vars_from_global[$var_id] = true; $statements_analyzer->registerVariable( $var_id, new CodeLocation($statements_analyzer, $var), diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index fd154f7a663..034ee5baefd 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -194,7 +194,7 @@ public function analyze( && $context->check_variables ) { //var_dump($this->data_flow_graph); - $this->checkUnreferencedVars($stmts); + $this->checkUnreferencedVars($stmts, $context); } if ($codebase->alter_code && $root_scope && $this->vars_to_initialize) { @@ -701,7 +701,7 @@ private function parseStatementDocblock( /** * @param array $stmts */ - public function checkUnreferencedVars(array $stmts): void + public function checkUnreferencedVars(array $stmts, Context $context): void { $source = $this->getSource(); $codebase = $source->getCodebase(); @@ -776,6 +776,7 @@ public function checkUnreferencedVars(array $stmts): void $assignment_node = DataFlowNode::getForAssignment($var_id, $original_location); if (!isset($this->byref_uses[$var_id]) + && !isset($context->vars_from_global[$var_id]) && !VariableFetchAnalyzer::isSuperGlobal($var_id) && $this->data_flow_graph instanceof VariableUseGraph && !$this->data_flow_graph->isVariableUsed($assignment_node) diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 6e4790a2b81..1fe4755baba 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -2424,6 +2424,14 @@ function randomBits() : string return $randomBytes; }' ], + 'globalChangeValue' => [ + ' Date: Sat, 17 Jul 2021 22:04:22 +0200 Subject: [PATCH 2/3] initialize array --- src/Psalm/Context.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Context.php b/src/Psalm/Context.php index ab482acac32..7531fcbd4a8 100644 --- a/src/Psalm/Context.php +++ b/src/Psalm/Context.php @@ -370,7 +370,7 @@ class Context /** * @var array */ - public $vars_from_global; + public $vars_from_global = []; public function __construct(?string $self = null) { From d1a4975e3dec8c38840ee88b014ecf61fcce5079 Mon Sep 17 00:00:00 2001 From: orklah Date: Sat, 17 Jul 2021 23:20:13 +0200 Subject: [PATCH 3/3] remove test no longer applicable --- tests/UnusedVariableTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 1fe4755baba..f9235c698b3 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -3269,15 +3269,6 @@ function foo($mixed_or_null): Exception { };', 'error_message' => 'UnusedVariable', ], - 'globalVariableUsage' => [ - ' 'UnusedVariable', - ], 'warnAboutOriginalBadArray' => [ '