Skip to content

DatePeriod::getEndDate can be null #5953

@Alban-io

Description

@Alban-io

Bug report

Code snippet that reproduces the problem

https://phpstan.org/r/1f444637-6319-49d9-be32-fcabb3f7c84e

Expected output

17    Cannot call method format() on DateTimeInterface|null.

DatePeriod::getEndDate return DateTimeInterface when __construct has for 3rd argument a DateTimeInterface.

'DatePeriod::__construct' => ['void', 'start'=>'DateTimeInterface', 'interval'=>'DateInterval', 'recur'=>'int', 'options='=>'int'],
'DatePeriod::__construct\'1' => ['void', 'start'=>'DateTimeInterface', 'interval'=>'DateInterval', 'end'=>'DateTimeInterface', 'options='=>'int'],
'DatePeriod::__construct\'2' => ['void', 'iso'=>'string', 'options='=>'int'],

PHP doc : public DatePeriod::getEndDate(): ?DateTimeInterface
PHPStan : 'DatePeriod::getEndDate' => ['DateTimeInterface'],

<?php declare(strict_types=1);

$start = new DateTime('2012-07-01');
$interval = new DateInterval('P7D');
$end = new DateTime('2012-07-31');
$recurrences = 4;
$iso = 'R4/2012-07-01T00:00:00Z/P7D';

var_dump([
    (new DatePeriod($start, $interval, $recurrences))->getEndDate(), // null
    (new DatePeriod($start, $interval, $end))->getEndDate(),         // DateTime
    (new DatePeriod($iso))->getEndDate(),                            // null
]);

Did PHPStan help you today? Did it make you happy in any way?

PHPStan is one of the best things that happened in PHP ❤️
Thank Ondřej & contributors 👏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions