Skip to content

Commit

Permalink
Generalise init vars inside for loops
Browse files Browse the repository at this point in the history
Ref #3085
  • Loading branch information
muglug committed Jul 3, 2020
1 parent 3d0a8c4 commit 44d7f51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static function analyze(
&& \is_string($init->var->name)
&& ($init_var_type = $statements_analyzer->node_data->getType($init->expr))
) {
if ($init_var_type->isSingleIntLiteral()) {
$context->vars_in_scope['$' . $init->var->name] = Type::getInt();
}

$init_var_types[$init->var->name] = $init_var_type;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Loop/ForTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ function test(Node $head) {
echo $i * $j;'
],
'reconcileOuterVars' => [
'<?php
for ($i = 0; $i < 2; $i++) {
if ($i === 0) {
continue;
}
}'
],
];
}

Expand Down

0 comments on commit 44d7f51

Please sign in to comment.