Skip to content

Commit e04dda9

Browse files
committed
Fixes
1 parent fd5d042 commit e04dda9

10 files changed

+21
-36
lines changed

examples/TemplateChecker.php

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ private function checkWithViewClass(Context $context, array $stmts): void
178178
$statements_source = new StatementsAnalyzer(
179179
$view_method_analyzer,
180180
new NodeDataProvider(),
181+
false,
181182
);
182183

183184
$statements_source->analyze($pseudo_method_stmts, $context);

src/Psalm/Internal/Analyzer/AttributesAnalyzer.php

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ private static function analyzeAttributeConstruction(
212212
$statements_analyzer = new StatementsAnalyzer(
213213
$source,
214214
new NodeDataProvider(),
215+
false,
215216
);
216217
$statements_analyzer->addSuppressedIssues(array_values($suppressed_issues));
217218

src/Psalm/Internal/Analyzer/ClassAnalyzer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ public function analyze(
561561
}
562562
}
563563

564-
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider());
565-
$statements_analyzer->analyze($member_stmts, $class_context, $global_context, true);
564+
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider(), true);
565+
$statements_analyzer->analyze($member_stmts, $class_context, $global_context);
566566

567567
ClassConstAnalyzer::analyze($storage, $this->getCodebase());
568568

src/Psalm/Internal/Analyzer/FileAnalyzer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function analyze(
162162
$leftover_stmts = $this->populateCheckers($stmts);
163163

164164
$this->node_data = new NodeDataProvider();
165-
$statements_analyzer = new StatementsAnalyzer($this, $this->node_data);
165+
$statements_analyzer = new StatementsAnalyzer($this, $this->node_data, true);
166166

167167
foreach ($file_storage->docblock_issues as $docblock_issue) {
168168
IssueBuffer::maybeAdd($docblock_issue);
@@ -171,7 +171,7 @@ public function analyze(
171171
// if there are any leftover statements, evaluate them,
172172
// in turn causing the classes/interfaces be evaluated
173173
if ($leftover_stmts) {
174-
$statements_analyzer->analyze($leftover_stmts, $this->context, $global_context, true);
174+
$statements_analyzer->analyze($leftover_stmts, $this->context, $global_context);
175175

176176
foreach ($leftover_stmts as $leftover_stmt) {
177177
if ($leftover_stmt instanceof PhpParser\Node\Stmt\Return_) {

src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function analyze(
240240
$this->is_static = true;
241241
}
242242

243-
$statements_analyzer = new StatementsAnalyzer($this, $type_provider);
243+
$statements_analyzer = new StatementsAnalyzer($this, $type_provider, true);
244244

245245
$byref_uses = [];
246246
if ($this instanceof ClosureAnalyzer && $this->function instanceof Closure) {
@@ -505,7 +505,7 @@ public function analyze(
505505
$statements_analyzer->addSuppressedIssues(['NoValue']);
506506
}
507507

508-
$statements_analyzer->analyze($function_stmts, $context, $global_context, true);
508+
$statements_analyzer->analyze($function_stmts, $context, $global_context);
509509

510510
if ($codebase->alter_code
511511
&& isset($project_analyzer->getIssuesToFix()['MissingPureAnnotation'])

src/Psalm/Internal/Analyzer/InterfaceAnalyzer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ public function analyze(): void
221221

222222
MethodComparator::comparePseudoMethods($pseudo_methods, $this->fq_class_name, $codebase, $class_storage);
223223

224-
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider());
225-
$statements_analyzer->analyze($member_stmts, $interface_context, null, true);
224+
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider(), true);
225+
$statements_analyzer->analyze($member_stmts, $interface_context, null);
226226

227227
ClassConstAnalyzer::analyze($this->storage, $this->getCodebase());
228228
}

src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function collectAnalyzableInformation(): void
7777
}
7878

7979
if ($leftover_stmts) {
80-
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider());
80+
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider(), true);
8181
$file_context = $this->source->context;
8282

8383
if ($file_context !== null) {
@@ -88,7 +88,7 @@ public function collectAnalyzableInformation(): void
8888
$context->defineGlobals();
8989
$context->collect_exceptions = $codebase->config->check_for_throws_in_global_scope;
9090
}
91-
$statements_analyzer->analyze($leftover_stmts, $context, null, true);
91+
$statements_analyzer->analyze($leftover_stmts, $context);
9292
}
9393
}
9494

src/Psalm/Internal/Analyzer/StatementsAnalyzer.php

+7-25
Original file line numberDiff line numberDiff line change
@@ -150,32 +150,19 @@ final class StatementsAnalyzer extends SourceAnalyzer
150150
*/
151151
public array $foreach_var_locations = [];
152152

153-
public function __construct(protected SourceAnalyzer $source, public NodeDataProvider $node_data)
153+
public function __construct(protected SourceAnalyzer $source, public NodeDataProvider $node_data, private readonly bool $root_scope)
154154
{
155155
$this->file_analyzer = $source->getFileAnalyzer();
156156
$this->codebase = $source->getCodebase();
157157

158-
if ($this->codebase->taint_flow_graph) {
159-
$this->initTaintFlowGraph(true);
160-
} elseif ($this->codebase->find_unused_variables) {
161-
$this->data_flow_graph = new VariableUseGraph();
162-
}
163-
}
164-
165-
private function initTaintFlowGraph(bool $enable): ?TaintFlowGraph
166-
{
167-
$old = $this->data_flow_graph;
168-
169-
if ($enable
170-
&& $this->codebase->taint_flow_graph
158+
if ($this->codebase->taint_flow_graph
159+
&& $root_scope
171160
&& $this->codebase->config->trackTaintsInPath($this->getFilePath())
172161
) {
173162
$this->data_flow_graph = $this->codebase->taint_flow_graph;
174-
} else {
175-
$this->data_flow_graph = null;
163+
} elseif ($this->codebase->find_unused_variables) {
164+
$this->data_flow_graph = new VariableUseGraph();
176165
}
177-
178-
return $old;
179166
}
180167

181168
/**
@@ -188,7 +175,6 @@ public function analyze(
188175
array $stmts,
189176
Context $context,
190177
?Context $global_context = null,
191-
bool $root_scope = false,
192178
): ?bool {
193179
if (!$stmts) {
194180
return null;
@@ -198,8 +184,6 @@ public function analyze(
198184

199185
$codebase = $this->codebase;
200186

201-
$prev = $this->initTaintFlowGraph($root_scope);
202-
203187
if ($codebase->config->hoist_constants) {
204188
self::hoistConstants($this, $stmts, $context);
205189
}
@@ -210,7 +194,7 @@ public function analyze(
210194
}
211195
}
212196

213-
if ($root_scope
197+
if ($this->root_scope
214198
&& !$context->collect_initializations
215199
&& !$context->collect_mutations
216200
&& $codebase->find_unused_variables
@@ -219,7 +203,7 @@ public function analyze(
219203
$this->checkUnreferencedVars($stmts, $context);
220204
}
221205

222-
if ($codebase->alter_code && $root_scope && $this->vars_to_initialize) {
206+
if ($codebase->alter_code && $this->root_scope && $this->vars_to_initialize) {
223207
$file_contents = $codebase->getFileContents($this->getFilePath());
224208

225209
foreach ($this->vars_to_initialize as $var_id => $branch_point) {
@@ -231,8 +215,6 @@ public function analyze(
231215
}
232216
}
233217

234-
$this->data_flow_graph = $prev;
235-
236218
return null;
237219
}
238220

tests/AlgebraTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function testCombinatorialExpansion(): void
113113

114114
$file_analyzer = new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
115115
$file_analyzer->context = new Context();
116-
$statements_analyzer = new StatementsAnalyzer($file_analyzer, new NodeDataProvider());
116+
$statements_analyzer = new StatementsAnalyzer($file_analyzer, new NodeDataProvider(), false);
117117

118118
$dnf_clauses = FormulaGenerator::getFormula(
119119
spl_object_id($dnf_stmt->expr),

tests/TypeReconciliation/ReconcilerTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function setUp(): void
5656
$this->statements_analyzer = new StatementsAnalyzer(
5757
$this->file_analyzer,
5858
new NodeDataProvider(),
59+
false,
5960
);
6061

6162
$this->addFile('newfile.php', '

0 commit comments

Comments
 (0)