-
Notifications
You must be signed in to change notification settings - Fork 599
Do not report a private method overriding a private trait method as unused #6191
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
Merged
staabm
merged 4 commits into
phpstan:2.3.x
from
Jean-Beru:fix-12201-private-method-overriding-trait
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cc29965
Do not report a private method overriding a private trait method as u…
Jean-Beru bed4221
Do not report a private constant redeclared from a private trait cons…
Jean-Beru d171b76
Do not report a private property redeclared from a private trait prop…
Jean-Beru 12c374c
Say in each trait fixture why it is not analysed
Jean-Beru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/PHPStan/Rules/DeadCode/data/bug-12201-constant-traits.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php // lint >= 8.2 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug12201Constant; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a comment what this file is supposed to be and why itself is not analyzed |
||
|
|
||
| // Stands for a dependency shipped in vendor/: bug-12201-constant.php uses these traits, but | ||
| // this file is never passed to analyse(), so PHPStan does not traverse the trait bodies. | ||
|
|
||
| trait KernelTrait | ||
| { | ||
|
|
||
| private const ALLOWED_ENVS = ['prod', 'dev', 'test']; | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| protected function getAllowedEnvs(): array | ||
| { | ||
| return self::ALLOWED_ENVS; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| trait MicroKernelTrait | ||
| { | ||
|
|
||
| use KernelTrait; | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php // lint >= 8.2 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug12201Constant; | ||
|
|
||
| // The traits live in bug-12201-constant-traits.php which is not analysed on purpose: | ||
| // it stands for a dependency living outside of the analysed paths. | ||
| class AppKernel | ||
| { | ||
|
|
||
| use MicroKernelTrait; | ||
|
|
||
| private const ALLOWED_ENVS = ['prod', 'dev', 'test']; | ||
|
|
||
| } | ||
|
|
||
| class AnotherKernel | ||
| { | ||
|
|
||
| use MicroKernelTrait; | ||
|
|
||
| private const UNUSED = 'unused'; | ||
|
|
||
| } | ||
|
|
||
| trait NeverFetchedTrait | ||
| { | ||
|
|
||
| private const NEVER_FETCHED = 'never fetched'; | ||
|
|
||
| } | ||
|
|
||
| class UsesNeverFetchedTrait | ||
| { | ||
|
|
||
| use NeverFetchedTrait; | ||
|
|
||
| } | ||
|
|
||
| class ParentKernel | ||
| { | ||
|
|
||
| use MicroKernelTrait; | ||
|
|
||
| } | ||
|
|
||
| // The trait is used by the parent, so it cannot reach this separate private slot. | ||
| class ChildKernel extends ParentKernel | ||
| { | ||
|
|
||
| private const ALLOWED_ENVS = ['prod', 'dev', 'test']; | ||
|
|
||
| } | ||
|
|
||
| trait RedeclaredTrait | ||
| { | ||
|
|
||
| private const REDECLARED = 'redeclared'; | ||
|
|
||
| } | ||
|
|
||
| // This trait is analysed, so its fetches are visible and nothing needs to be assumed. | ||
| class RedeclaresAnalysedTrait | ||
| { | ||
|
|
||
| use RedeclaredTrait; | ||
|
|
||
| private const REDECLARED = 'redeclared'; | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.