mirror of
https://github.com/danog/psalm.git
synced 2024-12-02 09:37:59 +01:00
Fix #3125 - only apply clauses where applicable
This commit is contained in:
parent
633b210a32
commit
e17cfd8dba
@ -66,16 +66,13 @@ class LoopAnalyzer
|
|||||||
$codebase = $statements_analyzer->getCodebase();
|
$codebase = $statements_analyzer->getCodebase();
|
||||||
|
|
||||||
if ($pre_conditions) {
|
if ($pre_conditions) {
|
||||||
foreach ($pre_conditions as $pre_condition) {
|
foreach ($pre_conditions as $i => $pre_condition) {
|
||||||
$pre_condition_clauses = array_merge(
|
$pre_condition_clauses[$i] = Algebra::getFormula(
|
||||||
$pre_condition_clauses,
|
\spl_object_id($pre_condition),
|
||||||
Algebra::getFormula(
|
$pre_condition,
|
||||||
\spl_object_id($pre_condition),
|
$loop_scope->loop_context->self,
|
||||||
$pre_condition,
|
$statements_analyzer,
|
||||||
$loop_scope->loop_context->self,
|
$codebase
|
||||||
$statements_analyzer,
|
|
||||||
$codebase
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -117,11 +114,11 @@ class LoopAnalyzer
|
|||||||
$old_referenced_var_ids = $inner_context->referenced_var_ids;
|
$old_referenced_var_ids = $inner_context->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(
|
self::applyPreConditionToLoopContext(
|
||||||
$statements_analyzer,
|
$statements_analyzer,
|
||||||
$pre_condition,
|
$pre_condition,
|
||||||
$pre_condition_clauses,
|
$pre_condition_clauses[$condition_offset],
|
||||||
$inner_context,
|
$inner_context,
|
||||||
$loop_scope->loop_parent_context,
|
$loop_scope->loop_parent_context,
|
||||||
$is_do
|
$is_do
|
||||||
@ -160,12 +157,12 @@ class LoopAnalyzer
|
|||||||
|
|
||||||
IssueBuffer::startRecording();
|
IssueBuffer::startRecording();
|
||||||
|
|
||||||
foreach ($pre_conditions as $pre_condition) {
|
foreach ($pre_conditions as $condition_offset => $pre_condition) {
|
||||||
$asserted_var_ids = array_merge(
|
$asserted_var_ids = array_merge(
|
||||||
self::applyPreConditionToLoopContext(
|
self::applyPreConditionToLoopContext(
|
||||||
$statements_analyzer,
|
$statements_analyzer,
|
||||||
$pre_condition,
|
$pre_condition,
|
||||||
$pre_condition_clauses,
|
$pre_condition_clauses[$condition_offset],
|
||||||
$loop_scope->loop_context,
|
$loop_scope->loop_context,
|
||||||
$loop_scope->loop_parent_context,
|
$loop_scope->loop_parent_context,
|
||||||
$is_do
|
$is_do
|
||||||
@ -318,11 +315,11 @@ class LoopAnalyzer
|
|||||||
$analyzer->setMixedCountsForFile($statements_analyzer->getFilePath(), $original_mixed_counts);
|
$analyzer->setMixedCountsForFile($statements_analyzer->getFilePath(), $original_mixed_counts);
|
||||||
IssueBuffer::startRecording();
|
IssueBuffer::startRecording();
|
||||||
|
|
||||||
foreach ($pre_conditions as $pre_condition) {
|
foreach ($pre_conditions as $condition_offset => $pre_condition) {
|
||||||
self::applyPreConditionToLoopContext(
|
self::applyPreConditionToLoopContext(
|
||||||
$statements_analyzer,
|
$statements_analyzer,
|
||||||
$pre_condition,
|
$pre_condition,
|
||||||
$pre_condition_clauses,
|
$pre_condition_clauses[$condition_offset],
|
||||||
$inner_context,
|
$inner_context,
|
||||||
$loop_scope->loop_parent_context,
|
$loop_scope->loop_parent_context,
|
||||||
false
|
false
|
||||||
@ -436,7 +433,7 @@ class LoopAnalyzer
|
|||||||
// if the loop contains an assertion and there are no break statements, we can negate that assertion
|
// if the loop contains an assertion and there are no break statements, we can negate that assertion
|
||||||
// and apply it to the current context
|
// and apply it to the current context
|
||||||
$negated_pre_condition_types = Algebra::getTruthsFromFormula(
|
$negated_pre_condition_types = Algebra::getTruthsFromFormula(
|
||||||
Algebra::negateFormula($pre_condition_clauses)
|
Algebra::negateFormula(array_merge(...$pre_condition_clauses))
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($negated_pre_condition_types) {
|
if ($negated_pre_condition_types) {
|
||||||
|
@ -387,6 +387,14 @@ class WhileTest extends \Psalm\Tests\TestCase
|
|||||||
}
|
}
|
||||||
}',
|
}',
|
||||||
],
|
],
|
||||||
|
'invalidateWhileAssertion' => [
|
||||||
|
'<?php
|
||||||
|
function test(array $x, int $i) : void {
|
||||||
|
while (isset($x[$i]) && is_array($x[$i])) {
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user