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

Fix #534 - fix bug in type simplification

This commit is contained in:
Matthew Brown 2018-05-06 12:05:55 -04:00
parent 04bd492360
commit 00838d19b5
2 changed files with 30 additions and 4 deletions

View File

@ -425,13 +425,21 @@ class AlgebraChecker
if (count($clause_b->possibilities[$clause_var]) === 0) {
unset($clause_b->possibilities[$clause_var]);
$clause_b->impossibilities = null;
}
}
}
}
$cloned_clauses = array_filter(
$cloned_clauses,
$deduped_clauses = [];
// avoid strict duplicates
foreach ($cloned_clauses as $clause) {
$deduped_clauses[$clause->getHash()] = clone $clause;
}
$deduped_clauses = array_filter(
$deduped_clauses,
/**
* @return bool
*/
@ -442,10 +450,10 @@ class AlgebraChecker
$simplified_clauses = [];
foreach ($cloned_clauses as $clause_a) {
foreach ($deduped_clauses as $clause_a) {
$is_redundant = false;
foreach ($cloned_clauses as $clause_b) {
foreach ($deduped_clauses as $clause_b) {
if ($clause_a === $clause_b || !$clause_b->reconcilable || $clause_b->wedge) {
continue;
}
@ -562,6 +570,11 @@ class AlgebraChecker
if (!empty($clauses)) {
$grouped_clauses = self::groupImpossibilities($clauses);
if (count($grouped_clauses) > 800) {
// too many impossibilities
return [];
}
foreach ($grouped_clauses as $grouped_clause) {
if ($clause->impossibilities === null) {
throw new \UnexpectedValueException('$clause->impossibilities should not be null');

View File

@ -642,6 +642,19 @@ class TypeAlgebraTest extends TestCase
function takesInt(int $c) : void {}',
],
'typeSimplification' => [
'<?php
class A {}
class B extends A {}
function foo(A $a, A $b) : ?B {
if (($a instanceof B || !$b instanceof B) && $a instanceof B && $b instanceof B) {
return $a;
}
return null;
}',
],
];
}