Skip to content

DateTime and DateTimeImmutable can return FALSE on failure, comparison issue #1934

@odan

Description

@odan

phpstan version: 0.11.2

Summary of a problem or a feature request

Multiple DateTime and DateTimeImmutable methods like modify, diff, format, getOffset, getTimezone and setTime can return false on failure.

Code snippet that reproduces the problem

Example:

$validTo = new \DateTimeImmutable();
$validTo = $validTo->setTime(23, 59, 59);

// the problem is here
if ($validTo === false) {
    throw new RuntimeException(__('Invalid date'));
}

The output:

Strict comparison using === between DateTimeImmutable and false will  always evaluate to false.                     

Expected output

No error, because the the PHP documentation says:

https://secure.php.net/manual/en/datetime.settime.php#refsect1-datetime.settime-returnvalues

Returns the DateTime object for method chaining or FALSE on failure.

The same kind of error applies to multiple other DateTime and DateTimeImmutable methods like modify, diff, format, getOffset, getTimezone.

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