diff --git a/src/Type/Accessory/OversizedArrayType.php b/src/Type/Accessory/OversizedArrayType.php index 77fdec48fb..f608d9602d 100644 --- a/src/Type/Accessory/OversizedArrayType.php +++ b/src/Type/Accessory/OversizedArrayType.php @@ -21,7 +21,6 @@ use PHPStan\Type\Traits\NonGenericTypeTrait; use PHPStan\Type\Traits\NonObjectTypeTrait; use PHPStan\Type\Traits\NonRemoveableTypeTrait; -use PHPStan\Type\Traits\TruthyBooleanTypeTrait; use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; use PHPStan\Type\Type; use PHPStan\Type\UnionType; @@ -32,7 +31,6 @@ class OversizedArrayType implements CompoundType, AccessoryType use MaybeCallableTypeTrait; use NonObjectTypeTrait; - use TruthyBooleanTypeTrait; use NonGenericTypeTrait; use UndecidedComparisonCompoundTypeTrait; use NonRemoveableTypeTrait; @@ -414,6 +412,11 @@ public function toAbsoluteNumber(): Type return new ErrorType(); } + public function toBoolean(): BooleanType + { + return new BooleanType(); + } + public function toInteger(): Type { return new ConstantIntegerType(1); diff --git a/tests/PHPStan/Rules/Comparison/BooleanNotConstantConditionRuleTest.php b/tests/PHPStan/Rules/Comparison/BooleanNotConstantConditionRuleTest.php index 24df1d9ea1..2b1833c369 100644 --- a/tests/PHPStan/Rules/Comparison/BooleanNotConstantConditionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/BooleanNotConstantConditionRuleTest.php @@ -145,6 +145,12 @@ public function testBug8797(): void $this->analyse([__DIR__ . '/data/bug-8797.php'], []); } + public function testBug13797(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-13797.php'], []); + } + public function testBug7937(): void { $this->treatPhpDocTypesAsCertain = true; diff --git a/tests/PHPStan/Rules/Comparison/data/bug-13797.php b/tests/PHPStan/Rules/Comparison/data/bug-13797.php new file mode 100644 index 0000000000..8a17ac3b2f --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-13797.php @@ -0,0 +1,82 @@ + */ +function alert(): ?array +{ + $alerts = []; + + if (rand()) { + $alerts[] = [ + 'message' => "Foo", + 'details' => "bar", + 'duration' => rand() ?: null, + 'severity' => 100, + ]; + } + + if (rand()) { + $alerts[] = [ + 'message' => 'Offline', + 'duration' => rand() ?: null, + 'severity' => 99, + ]; + } + + if (rand()) { + $alerts[] = [ + 'message' => 'Running W/O Operator', + 'duration' => rand() ?: null, + 'severity' => 75, + ]; + } + + if (rand()) { + $alerts[] = [ + 'message' => 'No Queue', + 'duration' => rand() ?: null, + 'severity' => 60, + ]; + } + + if (rand()) { + if (rand()) { + $alerts[] = [ + 'message' => 'Not Scheduled', + 'duration' => null, + 'severity' => 25, + ]; + } + + if (rand()) { + $alerts[] = [ + 'message' => 'On Lunch', + 'duration' => rand() ?: null, + 'severity' => 24, + ]; + } + + if (rand()) { + $alerts[] = [ + 'message' => 'On Break', + 'duration' => rand() ?: null, + 'severity' => 24, + ]; + } + } + + if (rand()) { + $alerts[] = [ + 'message' => 'Idle', + 'duration' => rand() ?: null, + 'severity' => 23, + ]; + } + + if (!$alerts) { + return null; + } + + return $alerts[0]; +}