Generic/DuplicateClassName: 4 bug fixes + performance improvement #653
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.
Description
Generic/DuplicateClassName: bug fix - ignore namespace operator
The
namespace
keyword can also be used as an operator, in which case, the sniff as it were would throw false positives.The fix now applied for this incidentally also fixes a nasty bug which could throw the sniff into an eternal loop when an unfinished namespace statement was encountered during live coding.
Includes unit tests.
Note: the unit test for the live coding situation has to be the last unit test file for this sniff so as to not influence the other tests. To that end, it has been numbered
99
.Generic/DuplicateClassName: bug fix - ignore comments in namespace declarations
A comment in a namespace declaration statement would throw the sniff off, leading to false positives and false negatives.
Fixed now.
Includes unit tests. Additionally, a unit test for classes declared within a scoped global namespace has also been added.
Generic/DuplicateClassName: bug fix - namespace is reset on PHP open tag
As things were, the sniff listens to the
T_OPEN_TAG
token, processes the file until the firstT_CLOSE_TAG
and then waits again for a newT_OPEN_TAG
.However, every time the sniff is called, the namespace is reset, even when still processing the same file, i.e. when the namespace is still in effect.
This led to both false positives as well as false negatives.
Fixed now, by always processing the complete file in one go and not returning on a
T_CLOSE_TAG
.Includes unit tests.
Generic/DuplicateClassName: use helper method
Use the
File::getDeclarationName()
method instead of finding the name of an OO structure in the sniff itself.This also prevents a potential situation where if a class name could not be found, the
findNext()
method would returnfalse
, which would then be used in the$tokens[$nameToken]['content']
leading tofalse
being juggled to0
and a class name of<?php
being stored in the class name cache.Includes tests.
Generic/DuplicateClassName: performance fix
As things were, the sniff would unconditionally walk a complete file, making it one of the top 15 slowest sniffs.
However, OO structures in PHP cannot be nested, so once we've found an OO declaration, we can skip to the end of it before continuing the token walking. See: https://3v4l.org/pbSTG
This small tweak makes a significant difference in the sniff performance without any impact on the sniff results.
Suggested changelog entry
Types of changes