1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Ignore precondition issues in loop

This commit is contained in:
Matt Brown 2020-10-07 20:24:50 -04:00
parent 3b19913b44
commit e9b520d72d
2 changed files with 107 additions and 20 deletions

View File

@ -619,16 +619,14 @@ class LoopAnalyzer
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
if ($is_do) {
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
}
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
}
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;
}
if ($is_do) {
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
}
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
}
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);
}
}
}'
],
];
}