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:
commit
53ae7764e9
@ -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;
|
||||
|
@ -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),
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user