Skip to content

Commit

Permalink
Fix #3125 - only apply clauses where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 13, 2020
1 parent 633b210 commit e17cfd8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ public static function analyze(
$codebase = $statements_analyzer->getCodebase();

if ($pre_conditions) {
foreach ($pre_conditions as $pre_condition) {
$pre_condition_clauses = array_merge(
$pre_condition_clauses,
Algebra::getFormula(
\spl_object_id($pre_condition),
$pre_condition,
$loop_scope->loop_context->self,
$statements_analyzer,
$codebase
)
foreach ($pre_conditions as $i => $pre_condition) {
$pre_condition_clauses[$i] = Algebra::getFormula(
\spl_object_id($pre_condition),
$pre_condition,
$loop_scope->loop_context->self,
$statements_analyzer,
$codebase
);
}
} else {
Expand Down Expand Up @@ -117,11 +114,11 @@ public static function analyze(
$old_referenced_var_ids = $inner_context->referenced_var_ids;
$inner_context->referenced_var_ids = [];

foreach ($pre_conditions as $pre_condition) {
foreach ($pre_conditions as $condition_offset => $pre_condition) {
self::applyPreConditionToLoopContext(
$statements_analyzer,
$pre_condition,
$pre_condition_clauses,
$pre_condition_clauses[$condition_offset],
$inner_context,
$loop_scope->loop_parent_context,
$is_do
Expand Down Expand Up @@ -160,12 +157,12 @@ public static function analyze(

IssueBuffer::startRecording();

foreach ($pre_conditions as $pre_condition) {
foreach ($pre_conditions as $condition_offset => $pre_condition) {
$asserted_var_ids = array_merge(
self::applyPreConditionToLoopContext(
$statements_analyzer,
$pre_condition,
$pre_condition_clauses,
$pre_condition_clauses[$condition_offset],
$loop_scope->loop_context,
$loop_scope->loop_parent_context,
$is_do
Expand Down Expand Up @@ -318,11 +315,11 @@ public static function analyze(
$analyzer->setMixedCountsForFile($statements_analyzer->getFilePath(), $original_mixed_counts);
IssueBuffer::startRecording();

foreach ($pre_conditions as $pre_condition) {
foreach ($pre_conditions as $condition_offset => $pre_condition) {
self::applyPreConditionToLoopContext(
$statements_analyzer,
$pre_condition,
$pre_condition_clauses,
$pre_condition_clauses[$condition_offset],
$inner_context,
$loop_scope->loop_parent_context,
false
Expand Down Expand Up @@ -436,7 +433,7 @@ public static function analyze(
// if the loop contains an assertion and there are no break statements, we can negate that assertion
// and apply it to the current context
$negated_pre_condition_types = Algebra::getTruthsFromFormula(
Algebra::negateFormula($pre_condition_clauses)
Algebra::negateFormula(array_merge(...$pre_condition_clauses))
);

if ($negated_pre_condition_types) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Loop/WhileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ function changeVarAfterUse(array $values, array $fields): void {
}
}',
],
'invalidateWhileAssertion' => [
'<?php
function test(array $x, int $i) : void {
while (isset($x[$i]) && is_array($x[$i])) {
$i++;
}
}'
]
];
}

Expand Down

0 comments on commit e17cfd8

Please sign in to comment.