diff --git a/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php b/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php index 180b5481f25..ec96f494cc3 100644 --- a/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php +++ b/src/Type/Php/DatePeriodConstructorReturnTypeExtension.php @@ -34,21 +34,28 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, $thirdArgType = $scope->getType($methodCall->getArgs()[2]->value); } - if ( - $thirdArgType instanceof ObjectType - && (new ObjectType(DateTimeInterface::class))->isSuperTypeOf($thirdArgType)->yes() - ) { + if (!$thirdArgType instanceof Type) { + return new GenericObjectType(DatePeriod::class, [ + new NullType(), + new IntegerType(), + ]); + } + + if ((new ObjectType(DateTimeInterface::class))->isSuperTypeOf($thirdArgType)->yes()) { + return new GenericObjectType(DatePeriod::class, [ + new ObjectType(DateTimeInterface::class), + new NullType(), + ]); + } + if ((new IntegerType())->isSuperTypeOf($thirdArgType)->yes()) { return new GenericObjectType(DatePeriod::class, [ new ObjectType(DateTimeInterface::class), new NullType(), ]); } - return new GenericObjectType(DatePeriod::class, [ - new NullType(), - new IntegerType(), - ]); + return new ObjectType(DatePeriod::class); } } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 8da3ce9d1d7..292b8f62080 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -623,16 +623,16 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5698-php7.php'); } + if (PHP_VERSION_ID < 70304) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/date-period-return-types.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6404.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6399.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4357.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5817.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/array-column.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/template-null-bound.php'); - - if (PHP_VERSION_ID >= 70304) { - yield from $this->gatherAssertTypes(__DIR__ . '/data/date-period-return-types.php'); - } } /** diff --git a/tests/PHPStan/Analyser/data/date-period-return-types.php b/tests/PHPStan/Analyser/data/date-period-return-types.php index 24654c00b82..2694c556351 100644 --- a/tests/PHPStan/Analyser/data/date-period-return-types.php +++ b/tests/PHPStan/Analyser/data/date-period-return-types.php @@ -11,19 +11,19 @@ $datePeriodList = []; $datePeriod = new DatePeriod($start, $interval, $end); -assertType(\DatePeriod::class, $datePeriod); +assertType(\DatePeriod::class . '', $datePeriod); assertType(\DateTimeInterface::class, $datePeriod->getEndDate()); assertType('null', $datePeriod->getRecurrences()); $datePeriodList[] = $datePeriod; $datePeriod = new DatePeriod($start, $interval, $recurrences); -assertType(\DatePeriod::class, $datePeriod); +assertType(\DatePeriod::class . '', $datePeriod); assertType('null', $datePeriod->getEndDate()); assertType('int', $datePeriod->getRecurrences()); $datePeriodList[] = $datePeriod; $datePeriod = new DatePeriod($iso); -assertType(\DatePeriod::class, $datePeriod); +assertType(\DatePeriod::class . '', $datePeriod); assertType('null', $datePeriod->getEndDate()); assertType('int', $datePeriod->getRecurrences()); $datePeriodList[] = $datePeriod;