diff --git a/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php b/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php index 3e1fd40823e..90842b37a05 100644 --- a/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php +++ b/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php @@ -12,6 +12,7 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use function array_map; +use function count; use function gettype; use function in_array; @@ -44,6 +45,11 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, $strings, ); + // the error case, when wrong types are passed + if (count($possibleReturnTypes) === 0) { + return null; + } + if (in_array('boolean', $possibleReturnTypes, true) && in_array('object', $possibleReturnTypes, true)) { return null; } diff --git a/tests/PHPStan/Analyser/data/bug-8442.php b/tests/PHPStan/Analyser/data/bug-8442.php index e35ac0547fb..64ed0e66536 100644 --- a/tests/PHPStan/Analyser/data/bug-8442.php +++ b/tests/PHPStan/Analyser/data/bug-8442.php @@ -2,6 +2,7 @@ namespace Bug8442; +use stdClass; use function PHPStan\Testing\assertType; use DateInterval; @@ -33,6 +34,7 @@ function () { assertType('false', DateInterval::createFromDateString($interval)); - DateInterval::createFromDateString(); + assertType('DateInterval|false',DateInterval::createFromDateString()); + assertType('DateInterval|false',DateInterval::createFromDateString(new stdClass())); };