Skip to content

Commit

Permalink
Merge pull request #6129 from orklah/unused-global
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Jul 17, 2021
2 parents 381ea8d + d1a4975 commit 53ae776
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ class Context
*/
public $has_returned = false;

/**
* @var array<string, bool>
*/
public $vars_from_global = [];

public function __construct(?string $self = null)
{
$this->self = $self;
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -701,7 +701,7 @@ private function parseStatementDocblock(
/**
* @param array<PhpParser\Node\Stmt> $stmts
*/
public function checkUnreferencedVars(array $stmts): void
public function checkUnreferencedVars(array $stmts, Context $context): void
{
$source = $this->getSource();
$codebase = $source->getCodebase();
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,14 @@ function randomBits() : string
return $randomBytes;
}'
],
'globalChangeValue' => [
'<?php
function setProxySettingsFromEnv(): void {
global $a;
$a = false;
}'
],
];
}

Expand Down Expand Up @@ -3261,15 +3269,6 @@ function foo($mixed_or_null): Exception {
};',
'error_message' => 'UnusedVariable',
],
'globalVariableUsage' => [
'<?php
$a = "hello";
function example() : void {
global $a;
}
example();',
'error_message' => 'UnusedVariable',
],
'warnAboutOriginalBadArray' => [
'<?php
function takesArray(array $arr) : void {
Expand Down

0 comments on commit 53ae776

Please sign in to comment.