Improve unused code detection by adding class / method references from Symfony compnents#382
Open
tunterreitmeier wants to merge 1 commit intopsalm:5.xfrom
Open
Improve unused code detection by adding class / method references from Symfony compnents#382tunterreitmeier wants to merge 1 commit intopsalm:5.xfrom
tunterreitmeier wants to merge 1 commit intopsalm:5.xfrom
Conversation
tunterreitmeier
commented
Mar 1, 2026
| foreach (self::INVOKABLE_CLASS_ATTRIBUTES as $attributeClass => $callerIdentifier) { | ||
| if ($storage->hasAttributeIncludingParents($attributeClass, $codebase)) { | ||
| $identifier = new MethodIdentifier($storage->name, '__invoke'); | ||
| $codebase->methodExists($identifier, null, $callerIdentifier); |
Author
There was a problem hiding this comment.
alternative to CodeBase::methodExists would be to add the reference manually. methodExists is more convenient as you do not have to bother with code location and the case of classes / methods.
51273c3 to
d568689
Compare
d568689 to
92b2bde
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Currently,
UnusedClassis suppressed for all services in the container. This is a symptom that Psalm does not statically see any usage where Symfony dynamically calls application code. So by adding call references, we can significantly improve dead code detection.As the dumped debug container contains also unused services, they are effectively no longer detected.
Another issue is that when Psalm has no references to a class, member references are not even checked. So a class that is not statically called by application code might be completely unused or have unused methods / properties.
This is true for example for:
I created a dummy Symfony project with examples of dead code that is currently not detected.
The repository has a GitHub Action that runs Psalm with this branch to showcase the difference.
I also ran this on larger code base where I was able to find quite a bit of genuinely unused code, remove suppression statements for false positives, without raising any new false positives.
Changes
All services, constructors, factory methods and setter injections (e.g.
#[Required) are now marked as used.Objects (and their nested sub-objects) created by Symfony Serializer via
Denormalizer::denormalize,Serializer::deserialize,#[MapRequestPayload]or#[MapQueryString]are now marked as used.Event Listener (also Doctrine ORM) and Subscriber methods are now marked as used.
Controller methods used as Routes are now marked as used.