Skip to content

Backed enum-related type assumption wrong #11328

Closed

Description

@BladeMF

Bug report

Phpstan wrongly assumes that a variable always has a value when it does not.

Code snippet that reproduces the problem

https://phpstan.org/r/2a7f78ca-7ce8-406c-937e-a9d631143494

Expected output

No errors.

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

Gets my errors ever time :-)

Activity

  1. BladeMF commented on Jul 12, 2024

    @BladeMF
    Author

    Here's the code for easy reading:

    <?php declare(strict_types = 1);
    
    enum Status: string {
    	case Live = 's1';
    	case Expired = 's2';
    }
    
    /** @param Status[] $statuses */
    function check(array $statuses):void {
    	$fromDeadline = null;
    	$toDeadline = null;
    	if (in_array(Status::Live, $statuses, true)) {
    		$fromDeadline = (new DateTimeImmutable())->setTime(23, 59, 59);
    	}
    	\PHPStan\dumpType($fromDeadline);
    	if (in_array(Status::Expired, $statuses, true)) {
    		\PHPStan\dumpType($fromDeadline);
    		if ($fromDeadline === null) { // <-- error is here. Assumes $fromDeadline is never null
    			$toDeadline = (new DateTimeImmutable())->setTime(0, 0, 0);
    		} else {
    			$fromDeadline = null;
    		}
    	}
    }
  2. ondrejmirtes commented on Jul 12, 2024

    @ondrejmirtes
    Member

    The reason for this behaviour is that PHPStan sees the same $statuses type non-empty-array<Status> even in the 2nd condition and that's why it narrows the type to DateTimeImmutable again: https://phpstan.org/r/8930b09a-9a4d-47b0-b974-1b24f1e33bb7

    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.

  3. added this to the Dependent types milestone on Jul 12, 2024
  4. BladeMF commented on Jul 13, 2024

    @BladeMF
    Author

    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.

  5. ondrejmirtes commented on Jul 13, 2024

    @ondrejmirtes
    Member

    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.

  6. BladeMF commented on Jul 14, 2024

    @BladeMF
    Author

    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 = (new DateTimeImmutable())->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.

  7. ondrejmirtes commented on Jul 14, 2024

    @ondrejmirtes
    Member

    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.

  8. BladeMF commented on Jul 14, 2024

    @BladeMF
    Author

    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.

  9. phpstan-bot commented on Sep 15, 2024

    @phpstan-bot
    Collaborator

    @BladeMF After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
     17: Dumped type: DateTimeImmutable
     18: Strict comparison using === between DateTimeImmutable and null will always evaluate to false.
     
    -PHP 7.2 – 8.0 (2 errors)
    +PHP 8.0 (2 errors)
    +==========
    +
    + 3: Syntax error, unexpected '{' on line 3
    + 3: Syntax error, unexpected T_STRING on line 3
    +
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
      3: Syntax error, unexpected T_STRING on line 3
      4: Syntax error, unexpected T_CASE on line 4
    Full report

    PHP 8.1 – 8.3 (3 errors)

    Line Error
    15 `Dumped type: DateTimeImmutable
    17 Dumped type: DateTimeImmutable
    18 Strict comparison using === between DateTimeImmutable and null will always evaluate to false.

    PHP 8.0 (2 errors)

    Line Error
    3 Syntax error, unexpected '{' on line 3
    3 Syntax error, unexpected T_STRING on line 3

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    3 Syntax error, unexpected T_STRING on line 3
    4 Syntax error, unexpected T_CASE on line 4
  10. phpstan-bot commented on Sep 15, 2024

    @phpstan-bot
    Collaborator

    @ondrejmirtes After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
     19: Dumped type: DateTimeImmutable
     20: Strict comparison using === between DateTimeImmutable and null will always evaluate to false.
     
    -PHP 7.2 – 8.0 (2 errors)
    +PHP 8.0 (2 errors)
    +==========
    +
    + 3: Syntax error, unexpected '{' on line 3
    + 3: Syntax error, unexpected T_STRING on line 3
    +
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
      3: Syntax error, unexpected T_STRING on line 3
      4: Syntax error, unexpected T_CASE on line 4
    Full report

    PHP 8.1 – 8.3 (5 errors)

    Line Error
    13 Dumped type: non-empty-array<Status>
    16 `Dumped type: DateTimeImmutable
    18 Dumped type: non-empty-array<Status>
    19 Dumped type: DateTimeImmutable
    20 Strict comparison using === between DateTimeImmutable and null will always evaluate to false.

    PHP 8.0 (2 errors)

    Line Error
    3 Syntax error, unexpected '{' on line 3
    3 Syntax error, unexpected T_STRING on line 3

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    3 Syntax error, unexpected T_STRING on line 3
    4 Syntax error, unexpected T_CASE on line 4
  11. phpstan-bot commented on Apr 1, 2026

    @phpstan-bot
    Collaborator

    Closed via merging phpstan/phpstan-src#5373

  12. github-actions commented on May 3, 2026

    @github-actions

    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.

  13. locked as resolved and limited conversation to collaborators on May 3, 2026
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions