Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Rules/BooleansInConditions/BooleanRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/Levels/data/onlyBooleans-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
17 changes: 17 additions & 0 deletions tests/Levels/data/onlyBooleans-10.json
Original file line number Diff line number Diff line change
@@ -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
}
]
5 changes: 5 additions & 0 deletions tests/Levels/data/onlyBooleans-6.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
17 changes: 17 additions & 0 deletions tests/Levels/data/onlyBooleans.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}
}

}
Loading