1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Merge pull request #6129 from orklah/unused-global

This commit is contained in:
Bruce Weirdan 2021-07-18 02:33:50 +03:00 committed by GitHub
commit 53ae7764e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 11 deletions

View File

@ -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;

View File

@ -65,6 +65,7 @@ class GlobalAnalyzer
$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),

View File

@ -194,7 +194,7 @@ class StatementsAnalyzer extends SourceAnalyzer
&& $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 @@ class StatementsAnalyzer extends SourceAnalyzer
/**
* @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();
@ -776,6 +776,7 @@ class StatementsAnalyzer extends SourceAnalyzer
$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)

View File

@ -2424,6 +2424,14 @@ class UnusedVariableTest extends TestCase
return $randomBytes;
}'
],
'globalChangeValue' => [
'<?php
function setProxySettingsFromEnv(): void {
global $a;
$a = false;
}'
],
];
}
@ -3261,15 +3269,6 @@ class UnusedVariableTest extends TestCase
};',
'error_message' => 'UnusedVariable',
],
'globalVariableUsage' => [
'<?php
$a = "hello";
function example() : void {
global $a;
}
example();',
'error_message' => 'UnusedVariable',
],
'warnAboutOriginalBadArray' => [
'<?php
function takesArray(array $arr) : void {