Skip to content

IntBackedEnum and StringBackedEnum constructor not taken in to account #11421

Closed
phpstan/phpstan-src
#6790

Description

@AJenbo

Feature request

PHPStan is not resolving the specific type of backed enum based on type when analyzing the from() method.
Doing so could help catch more bugs.

enum NumberEnum: int {}
$word = 'one';
NumberEnum::from($word);

https://phpstan.org/r/9a2e151f-fef8-4809-ad29-c127222838a3

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

No response

Activity

  1. phpstan-bot commented on Sep 15, 2024

    @phpstan-bot
    Contributor

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

    @@ @@
     
     No errors
     
    -PHP 7.2 – 8.0 (2 errors)
    +PHP 8.0 (4 errors)
    +==========
    +
    + 3: Syntax error, unexpected T_STRING on line 3
    + 4: Syntax error, unexpected '{' on line 4
    + 9: Syntax error, unexpected T_STRING on line 9
    +10: Syntax error, unexpected '{' on line 10
    +
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
      3: Syntax error, unexpected T_STRING on line 3
      5: Syntax error, unexpected T_CASE on line 5
    Full report

    PHP 8.1 – 8.3

    No errors

    PHP 8.0 (4 errors)

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

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    3 Syntax error, unexpected T_STRING on line 3
    5 Syntax error, unexpected T_CASE on line 5
  2. AJenbo commented on Sep 15, 2024

    @AJenbo
    ContributorAuthor

    To be clear there isn't any improvements here, it's just that Enums aren't supported til PHP 8.1 and apparently the way phpstan baubles up parsing errors changed a bit.

  3. phpstan-bot commented on Sep 14, 2026

    @phpstan-bot
    Contributor

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

    @@ @@
    -PHP 8.1 – 8.3
    +PHP 8.1 – 8.3 (2 errors)
     ==========
     
    -No errors
    +16: Call to static method NumberEnum::from() on a separate line has no effect.
    +19: Call to static method WordEnum::from() on a separate line has no effect.
     
    -PHP 7.2 – 8.0 (2 errors)
    +PHP 8.0 (4 errors)
    +==========
    +
    + 3: Syntax error, unexpected T_STRING on line 3
    + 4: Syntax error, unexpected '{' on line 4
    + 9: Syntax error, unexpected T_STRING on line 9
    +10: Syntax error, unexpected '{' on line 10
    +
    +PHP 7.2 – 7.4 (2 errors)
     ==========
     
      3: Syntax error, unexpected T_STRING on line 3
      5: Syntax error, unexpected T_CASE on line 5
    Full report

    PHP 8.1 – 8.3 (2 errors)

    Line Error
    16 Call to static method NumberEnum::from() on a separate line has no effect.
    19 Call to static method WordEnum::from() on a separate line has no effect.

    PHP 8.0 (4 errors)

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

    PHP 7.2 – 7.4 (2 errors)

    Line Error
    3 Syntax error, unexpected T_STRING on line 3
    5 Syntax error, unexpected T_CASE on line 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions