1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Add a couple more shortcuts for common pattern

Fixes #3563
This commit is contained in:
Brown 2020-06-12 11:18:34 -04:00
parent 9ca6c868b7
commit 45ea5d0bfe
3 changed files with 47 additions and 3 deletions

View File

@ -2367,7 +2367,7 @@ class AssertionFinder
*
* @return int|null
*/
protected static function hasTrueVariable(PhpParser\Node\Expr\BinaryOp $conditional)
public static function hasTrueVariable(PhpParser\Node\Expr\BinaryOp $conditional)
{
if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch
&& strtolower($conditional->right->name->parts[0]) === 'true'

View File

@ -258,8 +258,11 @@ class Algebra
}
}
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
) {
$false_pos = AssertionFinder::hasFalseVariable($conditional);
$true_pos = AssertionFinder::hasTrueVariable($conditional);
if ($false_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT
&& ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
@ -276,7 +279,9 @@ class Algebra
$inside_negation,
$cache
);
} elseif ($false_pos === AssertionFinder::ASSIGNMENT_TO_LEFT
}
if ($false_pos === AssertionFinder::ASSIGNMENT_TO_LEFT
&& ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr)
) {
@ -292,6 +297,36 @@ class Algebra
$cache
);
}
if ($true_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT
&& ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr)
) {
return self::getFormula(
$object_id,
$conditional->left,
$this_class_name,
$source,
$codebase,
$inside_negation,
$cache
);
}
if ($true_pos === AssertionFinder::ASSIGNMENT_TO_LEFT
&& ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr)
) {
return self::getFormula(
$object_id,
$conditional->right,
$this_class_name,
$source,
$codebase,
$inside_negation,
$cache
);
}
}
$assertions = null;

View File

@ -861,6 +861,15 @@ class SwitchTypeTest extends TestCase
}
}'
],
'switchTruthyWithBoolean' => [
'<?php
$a = rand(0,1) ? new \DateTime() : null;
switch(true) {
case $a !== null && $a->format("Y") === "2020":
$a->format("d-m-Y");
}'
],
];
}