diff --git a/src/Rules/BooleansInConditions/BooleanRuleHelper.php b/src/Rules/BooleansInConditions/BooleanRuleHelper.php index 4ecba32..2a45005 100644 --- a/src/Rules/BooleansInConditions/BooleanRuleHelper.php +++ b/src/Rules/BooleansInConditions/BooleanRuleHelper.php @@ -22,8 +22,11 @@ public function __construct(RuleLevelHelper $ruleLevelHelper) public function passesAsBoolean(Scope $scope, Expr $expr): bool { $type = $scope->getType($expr); - if ($type instanceof MixedType) { - return !$type->isExplicitMixed(); + if ($type instanceof ErrorType) { + return true; + } + if ($type instanceof MixedType && $type->isExplicitMixed()) { + return false; } $typeToCheck = $this->ruleLevelHelper->findTypeToCheck( $scope, diff --git a/tests/Levels/data/onlyBooleans-0.json b/tests/Levels/data/onlyBooleans-0.json index 6c8e974..5df3a01 100644 --- a/tests/Levels/data/onlyBooleans-0.json +++ b/tests/Levels/data/onlyBooleans-0.json @@ -3,5 +3,15 @@ "message": "Only booleans are allowed in an if condition, mixed given.", "line": 32, "ignorable": true + }, + { + "message": "Function doesNotExist not found.", + "line": 52, + "ignorable": true + }, + { + "message": "Undefined variable: $undefined", + "line": 55, + "ignorable": true } ] \ No newline at end of file diff --git a/tests/Levels/data/onlyBooleans-10.json b/tests/Levels/data/onlyBooleans-10.json new file mode 100644 index 0000000..9210267 --- /dev/null +++ b/tests/Levels/data/onlyBooleans-10.json @@ -0,0 +1,17 @@ +[ + { + "message": "Only booleans are allowed in an if condition, mixed given.", + "line": 29, + "ignorable": true + }, + { + "message": "Only booleans are allowed in an if condition, mixed given.", + "line": 45, + "ignorable": true + }, + { + "message": "Only booleans are allowed in an if condition, mixed given.", + "line": 49, + "ignorable": true + } +] \ No newline at end of file diff --git a/tests/Levels/data/onlyBooleans-6.json b/tests/Levels/data/onlyBooleans-6.json index e868831..b283315 100644 --- a/tests/Levels/data/onlyBooleans-6.json +++ b/tests/Levels/data/onlyBooleans-6.json @@ -3,5 +3,10 @@ "message": "Method PHPStan\\Levels\\OnlyBooleans\\Foo::doFoo() has parameter $mixed with no type specified.", "line": 14, "ignorable": true + }, + { + "message": "Method PHPStan\\Levels\\OnlyBooleans\\Foo::doBar() has parameter $array with no value type specified in iterable type array.", + "line": 43, + "ignorable": true } ] \ No newline at end of file diff --git a/tests/Levels/data/onlyBooleans.php b/tests/Levels/data/onlyBooleans.php index 615dc76..b3e3616 100644 --- a/tests/Levels/data/onlyBooleans.php +++ b/tests/Levels/data/onlyBooleans.php @@ -40,4 +40,21 @@ public function doFoo( } } + public function doBar(array $array): void + { + if ($array['foo']) { + + } + $value = $array[0] ?? null; + if ($value) { + + } + if (doesNotExist()) { + + } + if ($undefined) { + + } + } + }