Skip to content

strtotime false positive #1469

Closed
Closed

Description

@vasily-kartashov

strtotime can return false when the time format cannot be recognised. For example

php > var_dump(strtotime('garbage'));
bool(false)

Currently PHPStan complains about following code

$expiry = strtotime('+1 week');
if ($expiry === false) {
    throw new RuntimeException("...");
}

with a warning:

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

Activity

  1. ondrejmirtes commented on Oct 2, 2018

    @ondrejmirtes
    Member

    Please always include a link to phpstan.org, but I think that nothing is wrong here: https://phpstan.org/r/316ce1900e6cfc27703552c554a9ffa1

    There's actually a dynamic return type extension that solves this, so please post a more realistic code that you have problem with.

  2. vasily-kartashov commented on Oct 2, 2018

    @vasily-kartashov
    Author

    Great tool, thanks!

  3. vasily-kartashov commented on Oct 2, 2018

    @vasily-kartashov
    Author

    In the example you've provided, why is the second warning

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

    The return type of strtotime is int|false, and not just int. And in such case the strict comparison between int|false and false wouldn't be always meaningless?

  4. ondrejmirtes commented on Oct 2, 2018

    @ondrejmirtes
    Member

    The extension is looking at the values provided to strtotime and parses them by itself. If you pass an unknown string to strtotime(), the result will be int|false.

  5. ondrejmirtes commented on Oct 2, 2018

    @ondrejmirtes
    Member

    wouldn't be always meaningless?

    +1 week is always valid

  6. bitwombat commented on Oct 2, 2018

    @bitwombat

    @ondrejmirtes Can you please explain why this error from PHPStan is valid and useful? The input code is correct, right?

  7. vasily-kartashov commented on Oct 2, 2018

    @vasily-kartashov
    Author

    It's an interesting point. Pretty much you unwrap strtotime into two variants based on argument's value, the first variant returns int and the second one int|false.

    I don't necessarily agree but I do understand the merits of such approach.

  8. bitwombat commented on Oct 2, 2018

    @bitwombat

    "you unwrap" as in a dynamic extension, or you're saying that's what PHP does internally? Missing your point.

    My issue/question is that this situation exists for a whole bunch of PHP built-ins, doesn't it? Isn't the consumer code in the OP correct code?

  9. vasily-kartashov commented on Oct 2, 2018

    @vasily-kartashov
    Author

    @bitwombat, I was addressing the author.

    It is correct to always check for === false if you treat strtotime as a single function and don't consider embedded variants.

    I don't have a proper solution. Let's take the following my_sqrt implementation that returns either square root for non-negatives or null.

    function my_sqrt(int $number): ?float {
        if ($number < 0) return null;
        return sqrt($number);
    } 
    

    This function can be seen as either one mapping from all ints into nullable floats int -> float|null, or effectively two functions, one mapping int (>= 0) -> float and another from int (< 0) -> null.

    Most tools will never be able to look inside of a function and do the unwrapping, so the result for them is always the union type.

  10. locked as resolved and limited conversation to collaborators on Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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