diff --git a/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php b/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php index bcdf1032c7..b6f2acd839 100644 --- a/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php +++ b/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php @@ -32,19 +32,19 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo return $methodReflection->getName() === '__construct'; } - public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type + public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type { - if (!isset($methodCall->getArgs()[0])) { - return new ObjectType(DatePeriod::class); - } - if (!$methodCall->class instanceof Name) { - return new ObjectType(DatePeriod::class); + return null; } $className = $scope->resolveName($methodCall->class); if (strtolower($className) !== 'dateperiod') { - return new ObjectType($className); + return null; + } + + if (!isset($methodCall->getArgs()[0])) { + return null; } $firstArgType = $scope->getType($methodCall->getArgs()[0]->value); @@ -80,7 +80,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, ]); } - return new ObjectType(DatePeriod::class); + return null; } } diff --git a/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php index 663f2ae211..0daa90073a 100644 --- a/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php @@ -357,6 +357,14 @@ public function testBug12274(): void ]); } + public function testBug13484(): void + { + $this->checkExplicitMixed = true; + $this->checkNullables = true; + + $this->analyse([__DIR__ . '/data/bug-13484.php'], []); + } + public function testBug9401(): void { $this->checkExplicitMixed = true; diff --git a/tests/PHPStan/Rules/Functions/data/bug-13484.php b/tests/PHPStan/Rules/Functions/data/bug-13484.php new file mode 100644 index 0000000000..31f805b597 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-13484.php @@ -0,0 +1,15 @@ +arguments = $arguments; + } +} + +function test(): MyPeriod +{ + return new MyPeriod(); +}