Skip to content

Commit f78967c

Browse files
committed
Fix
1 parent 0f32350 commit f78967c

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

src/Psalm/Internal/Analyzer/StatementsAnalyzer.php

+41-29
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ final class StatementsAnalyzer extends SourceAnalyzer
149149
*/
150150
public array $foreach_var_locations = [];
151151

152+
private int $depth = 0;
153+
152154
public function __construct(
153155
protected SourceAnalyzer $source,
154156
public NodeDataProvider $node_data,
@@ -181,43 +183,53 @@ public function analyze(
181183
if (!$stmts) {
182184
return null;
183185
}
184-
// hoist functions to the top
185-
$this->hoistFunctions($stmts, $context);
186-
187-
$codebase = $this->codebase;
186+
$this->depth++;
187+
try {
188+
// hoist functions to the top
189+
$this->hoistFunctions($stmts, $context);
188190

189-
if ($codebase->config->hoist_constants) {
190-
self::hoistConstants($this, $stmts, $context);
191-
}
191+
$codebase = $this->codebase;
192192

193-
foreach ($stmts as $stmt) {
194-
if (self::analyzeStatement($this, $stmt, $context, $global_context) === false) {
195-
return false;
193+
if ($codebase->config->hoist_constants) {
194+
self::hoistConstants($this, $stmts, $context);
196195
}
197-
}
198196

199-
if ($this->root_scope
200-
&& !$context->collect_initializations
201-
&& !$context->collect_mutations
202-
&& $codebase->find_unused_variables
203-
&& $context->check_variables
204-
) {
205-
$this->checkUnreferencedVars($stmts, $context);
206-
}
197+
foreach ($stmts as $stmt) {
198+
if (self::analyzeStatement($this, $stmt, $context, $global_context) === false) {
199+
return false;
200+
}
201+
}
207202

208-
if ($codebase->alter_code && $this->root_scope && $this->vars_to_initialize) {
209-
$file_contents = $codebase->getFileContents($this->getFilePath());
203+
if ($this->root_scope
204+
&& $this->depth === 1
205+
&& !$context->collect_initializations
206+
&& !$context->collect_mutations
207+
&& $codebase->find_unused_variables
208+
&& $context->check_variables
209+
) {
210+
$this->checkUnreferencedVars($stmts, $context);
211+
}
210212

211-
foreach ($this->vars_to_initialize as $var_id => $branch_point) {
212-
$newline_pos = (int)strrpos($file_contents, "\n", $branch_point - strlen($file_contents)) + 1;
213-
$indentation = substr($file_contents, $newline_pos, $branch_point - $newline_pos);
214-
FileManipulationBuffer::add($this->getFilePath(), [
215-
new FileManipulation($branch_point, $branch_point, $var_id . ' = null;' . "\n" . $indentation),
216-
]);
213+
if ($codebase->alter_code
214+
&& $this->root_scope
215+
&& $this->depth === 1
216+
&& $this->vars_to_initialize
217+
) {
218+
$file_contents = $codebase->getFileContents($this->getFilePath());
219+
220+
foreach ($this->vars_to_initialize as $var_id => $branch_point) {
221+
$newline_pos = (int)strrpos($file_contents, "\n", $branch_point - strlen($file_contents)) + 1;
222+
$indentation = substr($file_contents, $newline_pos, $branch_point - $newline_pos);
223+
FileManipulationBuffer::add($this->getFilePath(), [
224+
new FileManipulation($branch_point, $branch_point, $var_id . ' = null;' . "\n" . $indentation),
225+
]);
226+
}
217227
}
218-
}
219228

220-
return null;
229+
return null;
230+
} finally {
231+
$this->depth--;
232+
}
221233
}
222234

223235
/**

src/Psalm/Internal/Codebase/TaintFlowGraph.php

-2
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,11 @@ public function connectSinksAndSources(Progress $progress): void
236236
}
237237
} unset($map);
238238

239-
$stack = [];
240239
// reprocess resolved descendants up to a maximum nesting level of 40
241240
for ($i = 0; count($sinks) && count($sources) && $i < 40; $i++) {
242241
$new_sources = [];
243242

244243
ksort($sources);
245-
$stack []= $sources;
246244

247245
$progress->expand(count($sources));
248246

0 commit comments

Comments
 (0)