@@ -149,6 +149,8 @@ final class StatementsAnalyzer extends SourceAnalyzer
149
149
*/
150
150
public array $ foreach_var_locations = [];
151
151
152
+ private int $ depth = 0 ;
153
+
152
154
public function __construct (
153
155
protected SourceAnalyzer $ source ,
154
156
public NodeDataProvider $ node_data ,
@@ -181,43 +183,53 @@ public function analyze(
181
183
if (!$ stmts ) {
182
184
return null ;
183
185
}
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 ) ;
188
190
189
- if ($ codebase ->config ->hoist_constants ) {
190
- self ::hoistConstants ($ this , $ stmts , $ context );
191
- }
191
+ $ codebase = $ this ->codebase ;
192
192
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 );
196
195
}
197
- }
198
196
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
+ }
207
202
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
+ }
210
212
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
+ }
217
227
}
218
- }
219
228
220
- return null ;
229
+ return null ;
230
+ } finally {
231
+ $ this ->depth --;
232
+ }
221
233
}
222
234
223
235
/**
0 commit comments