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
The fix for this might be outlined here in this issue: #6705 - do not remember only that the array is non-empty-array but also remember which kind of value it contains too. It'd help us get rid of this bug too.
Forgive me if I am not getting it, but I read it 10 times and still don't see why if in_array(Status::Expired, $statuses, true) is true it means in_array(Status::Live, $statuses, true) was also true and therefore $fromDeadline is not possible to be null. I mean, even if $statuses is non-empty-array this does not mean it is guaranteed to have both values in there. I tried adding a third option and it did not change the assumption.
Do you need anything from me here? I mean, I could get into the code, but my head is already full of other shit (projects which go slower than predicted and clients expecting results) and as much I'd love to I'd rather not do it now.
Your code is fine. I was just describing why PHPStan is interpreting the code the wrong way. After both in_array calls the type of $statuses for PHPStan is non-empty-array<Status> which makes it reapply the non-nullability of $fromDeadline although it shouldn't.
The solution would be for $statuses to be of a type like non-empty-array<Status>&array-has-value<Status::Live> in the first example, and non-empty-array<Status>&array-has-value<Status::Expired> in the second example. Which would also help us get rid of the bug. But right now the typesystem has no concept of array-has-value<...>.
This is by no means an obligation for you to fix it, touching and changing the typesystem part of PHPStan is one of the more complex tasks in this codebase.
Hey, I really appreciate your careful explanation. Let me clarify that I did not think you were blaming my code. I just tried to follow the logic and could piece it together. I also did not feel you were saying it was up to me to fix it. I am just not the kind of guy to do hit & run with the bugs - I appreciate people taking the time to view my report and make sure I've provided the best conditions for them to fix it. And, whenever I am up to the task I enjoy helping because a) learning from other people's logic is priceless and b) I use the tool every day, surely I can give back some of my time. That's why I explained that currently I am not in a position to help.
So, with that out of the way, let me see if I understood the logic this time. Do you mean to say that:
// $statuses is array<Status>$fromDeadline = null;
if (in_array(Status::Live, $statuses, true)) {
// $statuses is non-empty-array<Status> here$fromDeadline = (newDateTimeImmutable())->setTime(23, 59, 59);
}
// $statuses is array<Status>if (in_array(Status::Expired, $statuses, true)) {
// $statuses is non-empty-array<Status> here// and because in the last scope where $statuses was non-empty-array<Status>, $fromDeadline had a value// therefore $fromDeadline has a value here as well.
}
Am I correct? If yes, then all your explanations make sense now.
Yes 😊 PHPStan uses this logic to understand that if you define variable conditionally in if ($debug) and then repeat the same condition, the variable seen as defined. But it works for all kinds of scenarios.
So basically you need some way to mark the scope of the first if and you're suggesting to assign an additional type to $statuses and this type is "an array of values of type Status that we know contains the value Status::Live". That is certainly one way to do it. The other, more temporary way, is to ignore that scope alltogether as something that gives additional knowedge about the variables inside.
Activity
BladeMF commentedon Jul 12, 2024
Here's the code for easy reading:
ondrejmirtes commentedon Jul 12, 2024
The reason for this behaviour is that PHPStan sees the same
$statusestypenon-empty-array<Status>even in the 2nd condition and that's why it narrows the type toDateTimeImmutableagain: https://phpstan.org/r/8930b09a-9a4d-47b0-b974-1b24f1e33bb7The fix for this might be outlined here in this issue: #6705 - do not remember only that the array is
non-empty-arraybut also remember which kind of value it contains too. It'd help us get rid of this bug too.BladeMF commentedon Jul 13, 2024
Forgive me if I am not getting it, but I read it 10 times and still don't see why if
in_array(Status::Expired, $statuses, true)istrueit meansin_array(Status::Live, $statuses, true)was alsotrueand therefore$fromDeadlineis not possible to be null. I mean, even if$statusesisnon-empty-arraythis does not mean it is guaranteed to have both values in there. I tried adding a third option and it did not change the assumption.Do you need anything from me here? I mean, I could get into the code, but my head is already full of other shit (projects which go slower than predicted and clients expecting results) and as much I'd love to I'd rather not do it now.
ondrejmirtes commentedon Jul 13, 2024
Your code is fine. I was just describing why PHPStan is interpreting the code the wrong way. After both
in_arraycalls the type of$statusesfor PHPStan isnon-empty-array<Status>which makes it reapply the non-nullability of$fromDeadlinealthough it shouldn't.The solution would be for
$statusesto be of a type likenon-empty-array<Status>&array-has-value<Status::Live>in the first example, andnon-empty-array<Status>&array-has-value<Status::Expired>in the second example. Which would also help us get rid of the bug. But right now the typesystem has no concept ofarray-has-value<...>.This is by no means an obligation for you to fix it, touching and changing the typesystem part of PHPStan is one of the more complex tasks in this codebase.
BladeMF commentedon Jul 14, 2024
Hey, I really appreciate your careful explanation. Let me clarify that I did not think you were blaming my code. I just tried to follow the logic and could piece it together. I also did not feel you were saying it was up to me to fix it. I am just not the kind of guy to do hit & run with the bugs - I appreciate people taking the time to view my report and make sure I've provided the best conditions for them to fix it. And, whenever I am up to the task I enjoy helping because a) learning from other people's logic is priceless and b) I use the tool every day, surely I can give back some of my time. That's why I explained that currently I am not in a position to help.
So, with that out of the way, let me see if I understood the logic this time. Do you mean to say that:
Am I correct? If yes, then all your explanations make sense now.
ondrejmirtes commentedon Jul 14, 2024
Yes 😊 PHPStan uses this logic to understand that if you define variable conditionally in
if ($debug)and then repeat the same condition, the variable seen as defined. But it works for all kinds of scenarios.BladeMF commentedon Jul 14, 2024
So basically you need some way to mark the scope of the first
ifand you're suggesting to assign an additional type to$statusesand this type is "an array of values of typeStatusthat we know contains the valueStatus::Live". That is certainly one way to do it. The other, more temporary way, is to ignore that scope alltogether as something that gives additional knowedge about the variables inside.phpstan-bot commentedon Sep 15, 2024
@BladeMF After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.1 – 8.3 (3 errors)
Dumped type: DateTimeImmutableStrict comparison using === between DateTimeImmutable and null will always evaluate to false.PHP 8.0 (2 errors)
Syntax error, unexpected '{' on line 3Syntax error, unexpected T_STRING on line 3PHP 7.2 – 7.4 (2 errors)
Syntax error, unexpected T_STRING on line 3Syntax error, unexpected T_CASE on line 4phpstan-bot commentedon Sep 15, 2024
@ondrejmirtes After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
Full report
PHP 8.1 – 8.3 (5 errors)
Dumped type: non-empty-array<Status>Dumped type: non-empty-array<Status>Dumped type: DateTimeImmutableStrict comparison using === between DateTimeImmutable and null will always evaluate to false.PHP 8.0 (2 errors)
Syntax error, unexpected '{' on line 3Syntax error, unexpected T_STRING on line 3PHP 7.2 – 7.4 (2 errors)
Syntax error, unexpected T_STRING on line 3Syntax error, unexpected T_CASE on line 4Add regression tests for related dependent type narrowing issues
phpstan-bot commentedon Apr 1, 2026
Closed via merging phpstan/phpstan-src#5373
github-actions commentedon May 3, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.