Skip to content

SlevomatCodingStandard.Namespaces.NamespaceSpacing: Fixed false positive for PHPCS annotations before namespace - #1886

Merged
kukulich merged 1 commit into
slevomat:masterfrom
lyn1dev:namespace-spacing-phpcs-annotations
Nov 26, 2026
Merged

kukulich merged 1 commit into
slevomat:masterfrom
lyn1dev:namespace-spacing-phpcs-annotations

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 namespace keyword, so a file like

<?php

// phpcs:ignore Stnd.NamingConventions.NamespaceName -- making an exception.
namespace My\Name;

was 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 breaks phpcs:ignore (it then applies to the empty line) and moves phpcs:disable away 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.UseSpacing for use statements) 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 reported phpcs:ignore case.
  • 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.php was 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 original NamespaceSpacingSniff.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 512M and php bin/phpstan analyse -c build/PHPStan/phpstan.tests.neon: no errors.
  • php bin/parallel-lint SlevomatCodingStandard tests: no syntax errors.

phpcs --sniffs=SlevomatCodingStandard.Namespaces.NamespaceSpacing on both examples from the report now reports no violations, and phpcbf on a file without the empty line adds it above the annotations.

Fixes #1826

AI was used to write this code.

@kukulich

Copy link
Copy Markdown
Contributor

Please squash to one commit, thanks.

@lyn1dev
lyn1dev force-pushed the namespace-spacing-phpcs-annotations branch from 2d96690 to 665995b Compare November 26, 2026 01:02
@lyn1dev

lyn1dev commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@kukulich Squashed to one commit (665995b). �in/parallel-lint, �in/phpcs --standard=build/phpcs.xml, �in/phpunit --no-coverage (1547 tests, 9008 assertions), and �in/phpstan analyse (phpstan.neon and phpstan.tests.neon) pass locally.

…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
kukulich force-pushed the namespace-spacing-phpcs-annotations branch from 665995b to 8a45693 Compare November 26, 2026 05:55
@kukulich
kukulich merged commit 92498ca into slevomat:master Nov 26, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SlevomatCodingStandard.Namespaces.NamespaceSpacing does not recognize PHPCS annotations correctly

2 participants