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

Improve algebra handling a little better

This commit is contained in:
Matt Brown 2020-10-26 16:18:42 -04:00
parent aadc52fda1
commit 8dd5dc5b72
3 changed files with 37 additions and 4 deletions

View File

@ -474,12 +474,10 @@ class Algebra
continue 2;
}
if ($opposing_keys) {
if (count($opposing_keys) === 1) {
unset($cloned_clauses[$clause_a_hash]);
while ($opposing_keys && $clause_a) {
$clause_a = $clause_a->removePossibilities(\array_shift($opposing_keys));
}
$clause_a = $clause_a->removePossibilities($opposing_keys[0]);
if (!$clause_a) {
continue 2;

View File

@ -156,6 +156,20 @@ class AlgebraTest extends TestCase
$this->assertSame(['$b' => ['!falsy']], $simplified_formula[0]->possibilities);
}
public function testSimplifyCNFWithNonUselessTerm(): void
{
$formula = [
new Clause(['$a' => ['!falsy'], '$b' => ['!falsy']], 1, 1),
new Clause(['$a' => ['falsy'], '$b' => ['falsy']], 1, 2),
];
$simplified_formula = Algebra::simplifyCNF($formula);
$this->assertCount(2, $simplified_formula);
$this->assertSame(['$a' => ['!falsy'], '$b' => ['!falsy']], $simplified_formula[0]->possibilities);
$this->assertSame(['$a' => ['falsy'], '$b' => ['falsy']], $simplified_formula[1]->possibilities);
}
public function testSimplifyCNFWithUselessTermAndOneInMiddle(): void
{
$formula = [

View File

@ -1063,6 +1063,27 @@ class TypeAlgebraTest extends \Psalm\Tests\TestCase
return $b;
}'
],
'moreChecks' => [
'<?php
class B {}
class C {}
function foo(?B $b, ?C $c): B|C {
if (!$b && !$c) {
throw new Exception("bad");
}
if ($b && $c) {
return rand(0, 1) ? $b : $c;
}
if ($b) {
return $b;
}
return $c;
}'
],
];
}