Skip to content

Conditionally defined classes will be evaluated regardless of the current version #13133

Closed

Description

@pascalheidmann

Bug report

We encountered the problem that we extend a library class which uses typed const in its newest release. To avoid PHP issues we have a conditionally defined sub-class like in this example (stripped away the inheritance):
https://phpstan.org/r/c0813fbe-6808-41e2-995b-217349960e6a

Although phpstan runs on php 8.2 it still evaluates the code that is just supposed to run on PHP 8.3 and later. This means we will get an error that cannot be ignored via baseline: Class constants with native types are supported only on PHP 8.3 and later.

Code snippet that reproduces the problem

https://phpstan.org/r/c0813fbe-6808-41e2-995b-217349960e6a

Expected output

PHPStan should ignore the branch that is not available in PHP 8.2 and therefore not report any error. As an alternative being able to ignore this kind of error would be a workable workaround (not prefered).

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

No response

Activity

  1. staabm commented on Jun 10, 2025

    @staabm
    Contributor

    A similar thing was implemented for a different rule in phpstan/phpstan-src#3662 - which might be useful as inspiration for a fix

  2. staabm commented on Jun 10, 2025

    @staabm
    Contributor

    There is even a blog post about this topic in case you are interested in the bigger picture

    https://staabm.github.io/2024/11/28/phpstan-php-version-in-scope.html

  3. added this to the Easy fixes milestone on Aug 30, 2025
  4. staabm commented on Sep 21, 2026

    @staabm
    Contributor

    the rule which emits "Class constants with native types are supported only on PHP 8.3 and later." should use $scope->getPhpVersion() instead of a DI injected PhpVersion. that way the error would not be emitted in a if (PHP_VERSION_ID >= 80300) { guarded scope.

  5. github-actions commented on Oct 23, 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.

  6. locked as resolved and limited conversation to collaborators on Oct 23, 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