Skip to content
Permalink
Browse files

Fix #1700 - interpret false===($a || $b) correctly

  • Loading branch information...
muglug committed May 29, 2019
1 parent 9b508d9 commit 4f48c6388d08955020ac9667b13faf87c05c33a4
@@ -1905,7 +1905,7 @@ protected static function hasNullVariable(PhpParser\Node\Expr\BinaryOp $conditio
*
* @return int|null
*/
protected static function hasFalseVariable(PhpParser\Node\Expr\BinaryOp $conditional)
public static function hasFalseVariable(PhpParser\Node\Expr\BinaryOp $conditional)
{
if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch
&& strtolower($conditional->right->name->parts[0]) === 'false'
@@ -173,6 +173,38 @@ public static function getFormula(
}
}
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
$false_pos = AssertionFinder::hasFalseVariable($conditional);
if ($false_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT
&& ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr)
) {
$inside_negation = !$inside_negation;
return self::getFormula(
$conditional->left,
$this_class_name,
$source,
$codebase,
$inside_negation
);
} elseif ($false_pos === AssertionFinder::ASSIGNMENT_TO_LEFT
&& ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr)
) {
$inside_negation = !$inside_negation;
return self::getFormula(
$conditional->right,
$this_class_name,
$source,
$codebase,
$inside_negation
);
}
}
AssertionFinder::scrapeAssertions(
$conditional,
$this_class_name,
@@ -1333,6 +1333,28 @@ function foo(?array $start_pos) : ?array {
return $start_pos;
}',
],
'falseEqualsBoolean' => [
'<?php
class A {}
class B extends A {
public function foo() : void {}
}
class C extends A {
public function foo() : void {}
}
function bar(A $a) : void {
if (false === (!$a instanceof B || !$a instanceof C)) {
return;
}
$a->foo();
}
function baz(A $a) : void {
if ((!$a instanceof B || !$a instanceof C) === false) {
return;
}
$a->foo();
}',
],
];
}

0 comments on commit 4f48c63

Please sign in to comment.
You can’t perform that action at this time.