You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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.
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.
Activity
ondrejmirtes commentedon Oct 2, 2018
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.
vasily-kartashov commentedon Oct 2, 2018
Great tool, thanks!
vasily-kartashov commentedon Oct 2, 2018
In the example you've provided, why is the second warning
The return type of
strtotimeisint|false, and not justint. And in such case the strict comparison betweenint|falseandfalsewouldn't be always meaningless?ondrejmirtes commentedon Oct 2, 2018
The extension is looking at the values provided to
strtotimeand parses them by itself. If you pass an unknown string tostrtotime(), the result will beint|false.ondrejmirtes commentedon Oct 2, 2018
+1 weekis always validbitwombat commentedon Oct 2, 2018
@ondrejmirtes Can you please explain why this error from PHPStan is valid and useful? The input code is correct, right?
vasily-kartashov commentedon Oct 2, 2018
It's an interesting point. Pretty much you unwrap
strtotimeinto two variants based on argument's value, the first variant returnsintand the second oneint|false.I don't necessarily agree but I do understand the merits of such approach.
bitwombat commentedon Oct 2, 2018
"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?
vasily-kartashov commentedon Oct 2, 2018
@bitwombat, I was addressing the author.
It is correct to always check for
=== falseif you treatstrtotimeas a single function and don't consider embedded variants.I don't have a proper solution. Let's take the following
my_sqrtimplementation that returns either square root for non-negatives or null.This function can be seen as either one mapping from all ints into nullable floats
int -> float|null, or effectively two functions, one mappingint (>= 0) -> floatand another fromint (< 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.