Skip to content

Deprecation found that is actually the solution to the problem #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
heiglandreas opened this issue Jun 15, 2023 · 2 comments
Closed

Deprecation found that is actually the solution to the problem #100

heiglandreas opened this issue Jun 15, 2023 · 2 comments

Comments

@heiglandreas
Copy link

heiglandreas commented Jun 15, 2023

I encountered a rather specific problem recently with the phpstan-deprecation-rule, more concrete with the FetchingClassConstOfDeprecatedClassRule.

When I run phpstan over a sample project I get the following message:

  5      Fetching deprecated class constant LAST_USERNAME of class       
         Symfony\Bundle\SecurityBundle\Security:                         
         since Symfony 6.2, use                                          
         \Symfony\Bundle\SecurityBundle\Security::LAST_USERNAME instead 

This is somewhat puzzling...

I have prepared a small gist that one can check out to reproduce the issue:

git clone https://gist.github.com/ddb23f85813820a24c4cd26250d844ce.git
cd ddb23f85813820a24c4cd26250d844ce
composer install
./vendor/bin/phpstan

Sadly so far I wasn't able to track down where the issue is actually happening. I only dug down so far that the \Symfony\Bundle\SecurityBundle\Security class is extending a deprecated class and the \Symfony\Bundle\SecurityBundle\Security::LAST_USERNAME const is overwriting the constant of the extended, deprecated, class.

But it looks like there is more to it as trying to reproduce that with an exaple where I recreate the classes myself didn't work. So there seems to be a bit more involved.

When I comment out the FetchingClassConstOfDeprecatedClassRule in the config file of the phpstan-deprecation-rule everything works as intended. So that one is for sure involved somehow...

@ondrejmirtes
Copy link
Member

The issue is that class Security extends LegacySecurity, LegacySecurity is Symfony\Component\Security\Core\Security and that class has this:

    /**
     * @deprecated since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security::LAST_USERNAME instead
     */
    public const LAST_USERNAME = '_security.last_username';

@deprecated annotations are inherited, you can break the inheritance chain by introducing @not-deprecated. Described in the documentation: https://phpstan.org/writing-php-code/phpdocs-basics#deprecations

You can do that either in the original source code, or by providing a stub file: https://phpstan.org/user-guide/stub-files

I'd accept these stubs in phpstan-symfony, ideally for all similar situations in Symfony at once.

heiglandreas added a commit to heiglandreas/security-bundle that referenced this issue Jun 16, 2023
Static analysis tools like PHPStan treat the `@deprecated` flag as inherited. So when a class constant is marked as deprecated, then the same class constant in an extending class is also marked deprecated unless it gets explicitly marked as `@non-deprecated`.

Without this flag PHPStan will return the message 

Fetching deprecated class constant LAST_USERNAME of class Symfony\Bundle\SecurityBundle\Security:                         
since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security::LAST_USERNAME instead 

which is not really helpful as that is exactly what one did at that point.

By marking these class-constants as @non-deprecated PHPStan and other static analysis tools can recognize these class constants as being the rightly used ones.

For more information have a look at the discussion at phpstan/phpstan-deprecation-rules#100
heiglandreas added a commit to heiglandreas/symfony that referenced this issue Jun 16, 2023
Static analysis tools like PHPStan treat the `@deprecated` flag as inherited. So when a class constant is marked as deprecated, then the same class constant in an extending class is also marked deprecated unless it gets explicitly marked as `@non-deprecated`.

Without this flag PHPStan will return the message 

```
Fetching deprecated class constant LAST_USERNAME of class Symfony\Bundle\SecurityBundle\Security:                          
since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security::LAST_USERNAME instead 
```
which is not really helpful as that is exactly what one did at that point.

By marking these class-constants as `@non-deprecated` PHPStan and other static analysis tools can recognize these class constants as being the rightly used ones.

For more information have a look at the discussion at phpstan/phpstan-deprecation-rules#100
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants