From 8e26d639decdc88a47389ace8939ce70a6d53a0b Mon Sep 17 00:00:00 2001 From: Brown Date: Tue, 8 Jan 2019 12:33:44 -0500 Subject: [PATCH] Fix #1181 - improve handling of complicated conditionals --- .../Internal/Analyzer/AlgebraAnalyzer.php | 2 +- .../Internal/Analyzer/StatementsAnalyzer.php | 2 -- src/Psalm/Type/Algebra.php | 29 ++++++++----------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php b/src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php index 934b238d4..98d92a5d7 100644 --- a/src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php @@ -40,7 +40,7 @@ class AlgebraAnalyzer array $new_assigned_var_ids ) { try { - $negated_formula2 = Algebra::negateFormula($formula2); + $negated_formula2 = Algebra::negateFormula($formula2, 1); } catch (\Psalm\Exception\ComplicatedExpressionException $e) { return; } diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index 1c4f3ae48..dcfb4a5c1 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -165,8 +165,6 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource $original_context = clone $context->loop_scope->loop_parent_context; } - \Psalm\Type\Algebra::resetClauseCount(); - $plugin_classes = $codebase->config->after_statement_checks; foreach ($stmts as $stmt) { diff --git a/src/Psalm/Type/Algebra.php b/src/Psalm/Type/Algebra.php index 7114648fc..cf5797ecd 100644 --- a/src/Psalm/Type/Algebra.php +++ b/src/Psalm/Type/Algebra.php @@ -13,15 +13,6 @@ use Psalm\Type\Algebra; class Algebra { - /** @var int */ - private static $clause_count = 0; - - /** @return void */ - public static function resetClauseCount() - { - self::$clause_count = 0; - } - /** * @param array>> $all_types * @@ -350,9 +341,9 @@ class Algebra * * @return array */ - private static function groupImpossibilities(array $clauses) + private static function groupImpossibilities(array $clauses, int &$complexity = 1) { - if (self::$clause_count > 50000) { + if ($complexity > 50000) { throw new ComplicatedExpressionException(); } @@ -361,9 +352,9 @@ class Algebra $new_clauses = []; if ($clauses) { - $grouped_clauses = self::groupImpossibilities($clauses); + $grouped_clauses = self::groupImpossibilities($clauses, $complexity); - if (self::$clause_count > 50000) { + if ($complexity > 50000) { throw new ComplicatedExpressionException(); } @@ -386,7 +377,7 @@ class Algebra $new_clauses[] = $new_clause; - self::$clause_count++; + $complexity += count($new_clause_possibilities); } } } @@ -401,7 +392,7 @@ class Algebra $new_clauses[] = $new_clause; - self::$clause_count++; + $complexity++; } } } @@ -512,13 +503,17 @@ class Algebra * * @return array */ - public static function negateFormula(array $clauses) + public static function negateFormula(array $clauses, int $complexity = null) { foreach ($clauses as $clause) { self::calculateNegation($clause); } - $negated = self::simplifyCNF(self::groupImpossibilities($clauses)); + if ($complexity === null) { + $complexity = count($clauses); + } + + $negated = self::simplifyCNF(self::groupImpossibilities($clauses, $complexity)); return $negated; }