1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Ignore precondition issues in loop

This commit is contained in:
Matt Brown 2020-10-07 20:24:50 -04:00 committed by Daniil Gentili
parent 96af3e041c
commit 6bd6a4cbc3
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 107 additions and 20 deletions

View File

@ -619,16 +619,14 @@ class LoopAnalyzer
$suppressed_issues = $statements_analyzer->getSuppressedIssues(); $suppressed_issues = $statements_analyzer->getSuppressedIssues();
if ($is_do) { if (!in_array('RedundantCondition', $suppressed_issues, true)) {
if (!in_array('RedundantCondition', $suppressed_issues, true)) { $statements_analyzer->addSuppressedIssues(['RedundantCondition']);
$statements_analyzer->addSuppressedIssues(['RedundantCondition']); }
} if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) { $statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']); }
} if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) { $statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
$statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
}
} }
if (ExpressionAnalyzer::analyze($statements_analyzer, $pre_condition, $loop_context) === false) { if (ExpressionAnalyzer::analyze($statements_analyzer, $pre_condition, $loop_context) === false) {
@ -672,16 +670,14 @@ class LoopAnalyzer
$loop_context->vars_in_scope = $pre_condition_vars_in_scope_reconciled; $loop_context->vars_in_scope = $pre_condition_vars_in_scope_reconciled;
} }
if ($is_do) { if (!in_array('RedundantCondition', $suppressed_issues, true)) {
if (!in_array('RedundantCondition', $suppressed_issues, true)) { $statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']); }
} if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) { $statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']); }
} if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) { $statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
$statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
}
} }
if ($is_do) { if ($is_do) {

View File

@ -495,6 +495,97 @@ class WhileTest extends \Psalm\Tests\TestCase
} }
}' }'
], ],
'propertyAssertionInsideWhile' => [
'<?php
class Foo {
public array $a = [];
public array $b = [];
public array $c = [];
public function one(): bool {
$has_changes = false;
while ($this->a) {
$has_changes = true;
$this->alter();
}
return $has_changes;
}
public function two(): bool {
$has_changes = false;
while ($this->a || $this->b) {
$has_changes = true;
$this->alter();
}
return $has_changes;
}
public function three(): bool {
$has_changes = false;
while ($this->a || $this->b || $this->c) {
$has_changes = true;
$this->alter();
}
return $has_changes;
}
public function four(): bool {
$has_changes = false;
while (($this->a && $this->b) || $this->c) {
$has_changes = true;
$this->alter();
}
return $has_changes;
}
public function alter() : void {
if (rand(0, 1)) {
array_pop($this->a);
} elseif (rand(0, 1)) {
array_pop($this->a);
} else {
array_pop($this->c);
}
}
}'
],
'propertyAssertionInsideWhileNested' => [
'<?php
class Foo {
public array $a = [];
public array $b = [];
public array $c = [];
public function five(): bool {
$has_changes = false;
while ($this->a || ($this->b && $this->c)) {
$has_changes = true;
$this->alter();
}
return $has_changes;
}
public function alter() : void {
if (rand(0, 1)) {
array_pop($this->a);
} elseif (rand(0, 1)) {
array_pop($this->a);
} else {
array_pop($this->c);
}
}
}'
],
]; ];
} }