mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix #1181 - improve handling of complicated conditionals
This commit is contained in:
parent
b6865b130b
commit
8e26d639de
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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<string, array<int, array<int, string>>> $all_types
|
||||
*
|
||||
@ -350,9 +341,9 @@ class Algebra
|
||||
*
|
||||
* @return array<int, Clause>
|
||||
*/
|
||||
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<int, Clause>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user