From 2eca28c9124ca375687877819fadcb05ac7257aa Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Wed, 9 Feb 2022 16:37:15 -0500 Subject: [PATCH] Remove more unused code from LoopAnalyzer --- .../Statements/Block/LoopAnalyzer.php | 31 +-- tests/Loop/WhileTest.php | 91 -------- tests/PropertyTypeTest.php | 205 ++++++++++++++++++ 3 files changed, 210 insertions(+), 117 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index 6b99ff7f5..e0eb239cf 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -157,6 +157,9 @@ class LoopAnalyzer $original_mixed_counts = $analyzer->getMixedCountsForFile($statements_analyzer->getFilePath()); + // record all the vars that existed before we did the first pass through the loop + $pre_loop_context = clone $loop_context; + IssueBuffer::startRecording(); if (!$is_do) { @@ -172,9 +175,6 @@ class LoopAnalyzer } } - // record all the vars that existed before we did the first pass through the loop - $pre_loop_context = clone $loop_context; - $continue_context = clone $loop_context; foreach ($continue_context->vars_in_scope as $context_var_id => $context_type) { @@ -597,18 +597,6 @@ class LoopAnalyzer $loop_context->inside_conditional = true; - $suppressed_issues = $statements_analyzer->getSuppressedIssues(); - - 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) { $loop_context->inside_conditional = $was_inside_conditional; @@ -631,7 +619,8 @@ class LoopAnalyzer $reconcilable_while_types = Algebra::getTruthsFromFormula( $loop_context->clauses, spl_object_id($pre_condition), - $new_referenced_var_ids + $new_referenced_var_ids, + $active_while_types ); $changed_var_ids = []; @@ -653,16 +642,6 @@ class LoopAnalyzer $loop_context->vars_in_scope = $pre_condition_vars_in_scope_reconciled; } - 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) { return []; } diff --git a/tests/Loop/WhileTest.php b/tests/Loop/WhileTest.php index 9e09b4916..26f252455 100644 --- a/tests/Loop/WhileTest.php +++ b/tests/Loop/WhileTest.php @@ -497,97 +497,6 @@ class WhileTest extends TestCase } }' ], - 'propertyAssertionInsideWhile' => [ - 'code' => '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' => [ - 'code' => '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); - } - } - }' - ], 'ifNestedInsideLoop' => [ 'code' => 'analyzeFile('somefile.php', new Context()); } + public function testAssertionInsideWhileOne(): void + { + Config::getInstance()->remember_property_assignments_after_call = false; + + $this->addFile( + 'somefile.php', + 'a) { + $has_changes = true; + $this->alter(); + } + + return $has_changes; + } + + public function alter() : void { + if (rand(0, 1)) { + array_pop($this->a); + } + + if (rand(0, 1)) { + array_pop($this->b); + } + + if (rand(0, 1)) { + array_pop($this->c); + } + } + }' + ); + + $this->analyzeFile('somefile.php', new Context()); + } + + public function testAssertionInsideWhileTwo(): void + { + Config::getInstance()->remember_property_assignments_after_call = false; + + $this->addFile( + 'somefile.php', + 'a || $this->b) { + $has_changes = true; + $this->alter(); + } + + return $has_changes; + } + + public function alter() : void { + if (rand(0, 1)) { + array_pop($this->a); + } + + if (rand(0, 1)) { + array_pop($this->b); + } + } + }' + ); + + $this->analyzeFile('somefile.php', new Context()); + } + + public function testAssertionInsideWhileThree(): void + { + Config::getInstance()->remember_property_assignments_after_call = false; + + $this->addFile( + 'somefile.php', + '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); + } + + if (rand(0, 1)) { + array_pop($this->b); + } + + if (rand(0, 1)) { + array_pop($this->c); + } + } + }' + ); + + $this->analyzeFile('somefile.php', new Context()); + } + + public function testAssertionInsideWhileFour(): void + { + Config::getInstance()->remember_property_assignments_after_call = false; + + $this->addFile( + 'somefile.php', + '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); + } + + if (rand(0, 1)) { + array_pop($this->b); + } + + if (rand(0, 1)) { + array_pop($this->c); + } + } + }' + ); + + $this->analyzeFile('somefile.php', new Context()); + } + + public function testAssertionInsideWhileFive(): void + { + Config::getInstance()->remember_property_assignments_after_call = false; + + $this->addFile( + 'somefile.php', + '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); + } + + if (rand(0, 1)) { + array_pop($this->b); + } + + if (rand(0, 1)) { + array_pop($this->c); + } + } + }' + ); + + $this->analyzeFile('somefile.php', new Context()); + } + public function testUniversalObjectCrates(): void { Config::getInstance()->addUniversalObjectCrate(DateTime::class);