Repository navigation
SlevomatCodingStandard.Namespaces.NamespaceSpacing: Fixed false positive for PHPCS annotations before namespace - #1886
Merged
kukulich merged 1 commit intoNov 26, 2026
Conversation
Contributor
|
Please squash to one commit, thanks. |
lyn1dev
force-pushed
the
namespace-spacing-phpcs-annotations
branch
from
November 26, 2026 01:02
2d96690 to
665995b
Compare
Contributor
Author
…ive for PHPCS annotations before namespace PHPCS annotations apply to the code below them, so they belong to the namespace statement. The required empty lines are now checked before the annotations (both line and block comments) instead of between the annotations and the namespace, and the fixer adds the empty lines above the annotations so that e.g. a phpcs:ignore annotation keeps applying to the namespace declaration.
kukulich
force-pushed
the
namespace-spacing-phpcs-annotations
branch
from
November 26, 2026 05:55
665995b to
8a45693
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.
PHPCS annotations (
phpcs:ignore,phpcs:disable,phpcs:enable,phpcs:set, ...) apply to the code below them, so a namespace declaration that is directly preceded by an annotation cannot be separated from it.The sniff only looked at the token right before the
namespacekeyword, so a file likewas reported as
IncorrectLinesCountBeforeNamespace, even though the required empty line is present above the annotation. The fixer then made things worse: it inserted the empty line between the annotation and the namespace declaration, which breaksphpcs:ignore(it then applies to the empty line) and movesphpcs:disableaway from the code it was meant for.Fix
When the token directly above the namespace is a PHPCS annotation, the annotation block is now considered part of the namespace statement: the number of empty lines is checked before the annotations (using the same approach as
SlevomatCodingStandard.Namespaces.UseSpacingforusestatements) and the fixer adds the empty line above the annotations, keeping them attached to the namespace. Both the line comment and the block comment form of annotations are covered.Regular comments are not affected; a file comment above the namespace is still separated from it by the fixer.
Tests
New fixture files:
namespaceSpacingWithPhpcsAnnotationsNoErrors.php- the reportedphpcs:ignorecase.namespaceSpacingWithMultiplePhpcsAnnotationsNoErrors.php- two annotations in a row.namespaceSpacingWithPhpcsBlockAnnotationNoErrors.php- the block comment form of an annotation.namespaceSpacingWithPhpcsAnnotationsErrors.php- no empty line at all; the fixed file keeps the annotations next to the namespace.namespaceSpacingWithPhpcsCommentBefore.fixed.phpwas removed and its test now expects no error, as that is the reported false positive.Results:
php bin/phpunit --no-configuration --bootstrap tests/bootstrap.php tests/Sniffs/Namespaces/NamespaceSpacingSniffTest.php: 4 failures on the originalNamespaceSpacingSniff.php(3 false positives and the broken fixer output), 21 tests / 57 assertions passing with the fix.php bin/phpunit --no-configuration --bootstrap tests/bootstrap.php tests: 1547 tests, 9008 assertions, OK.php bin/phpcs --standard=build/phpcs.xml: no errors.php bin/phpstan analyse -c build/PHPStan/phpstan.neon --memory-limit 512Mandphp bin/phpstan analyse -c build/PHPStan/phpstan.tests.neon: no errors.php bin/parallel-lint SlevomatCodingStandard tests: no syntax errors.phpcs --sniffs=SlevomatCodingStandard.Namespaces.NamespaceSpacingon both examples from the report now reports no violations, andphpcbfon a file without the empty line adds it above the annotations.Fixes #1826
AI was used to write this code.