Simplify detecting class properties to improve detection for anonymous classes #337
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 this sniff allows class properties to be unused without a warning. To determine if a variable it finds is a class property, it performs several checks; first it looks for a visibility keyword (eg:
public
) as the previous token. If that exists, then we assume it is a property. If that isn't found, it looks for astatic
keyword preceded by a visibility keyword. If there is nostatic
, then it assumes there is no property access. If there is astatic
, we repeat the check for the visibility keyword. If there is astatic
token but no visibility keyword, we instead look to see if we are inside a class definition but not inside a function. If that is true, then we are a class property.This logic is causing some problems for typed properties inside anonymous classes because the type keyword(s) are interfering with finding the visibility keyword. We can fix this by looking more carefully for the visibility keyword, like so:
But that doesn't totally solve the problem for anonymous classes and I realized that we probably don't need to do it anyway. The last check that looks to see if we are inside a class definition but outside a function should be enough to find any property.
This PR removes all the logic described above and leaves only the final check.
Fixes #332