Skip to content

Sniffs: exclude space from prepareForOutput() in error messages#1389

Merged
jrfnl merged 2 commits intoPHPCSStandards:4.xfrom
rodrigoprimo:exclude-space-from-prepare-for-output
Mar 18, 2026
Merged

Sniffs: exclude space from prepareForOutput() in error messages#1389
jrfnl merged 2 commits intoPHPCSStandards:4.xfrom
rodrigoprimo:exclude-space-from-prepare-for-output

Conversation

@rodrigoprimo
Copy link
Copy Markdown
Contributor

@rodrigoprimo rodrigoprimo commented Mar 6, 2026

Description

As suggested in squizlabs/PHP_CodeSniffer#2652 (comment), this PR updates the Generic.WhiteSpace.LanguageConstructSpacing and Squiz.Functions.FunctionDeclarationArgumentSpacing sniffs to pass [' '] as the $exclude parameter to Common::prepareForOutput(), so that spaces are no longer replaced with UTF-8 middot characters (·) in error messages. Tabs and newlines continue to be rendered as \t and \n.

Additionally, for the IncorrectSingle error in Generic.WhiteSpace.LanguageConstructSpacing, the spaces-only case now reports a descriptive count like "found 2 spaces" instead of displaying middot characters.

The middot character in the output can break tools that pipe PHPCS output through non-UTF-8 environments.

This is a follow-up to #1379, which addressed the same issue in Generic.PHP.LowerCaseKeyword.

Suggested changelog entry

Changed: Generic.WhiteSpace.LanguageConstructSpacing and Squiz.Functions.FunctionDeclarationArgumentSpacing no longer embed UTF-8 middot characters for spaces in error messages.

Related issues/external references

Fixes squizlabs/PHP_CodeSniffer#2652

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.
  • I have opened a sister-PR in the documentation repository to update the Wiki.

…orOutput()

The `SpacingAfterHint` error uses `Common::prepareForOutput()` to make
whitespace characters visible in the error message when the gap between
a type hint and argument contains tabs or newlines. Among other things,
the method replaces spaces with a UTF-8 middot character.

This commit changes the call to `Common::prepareForOutput()`, to pass
`[' ']` as the `$exclude` parameter so that spaces are displayed as-is
while tabs and newlines continue to be rendered as `\t` and `\n`. This
change is necessary because the middot character in the output can break
tools that pipe PHPCS output through non-UTF-8 environments (see
squizlabs/PHP_CodeSniffer 2652).

`Common::prepareForOutput()` was added to this sniff in PR 620.
Copy link
Copy Markdown
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Thanks for setting this up. Just one minor tweak suggestion, but not a blocker.

@jrfnl jrfnl added this to the 4.0.2 milestone Mar 18, 2026
Copy link
Copy Markdown
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rodrigoprimo !

This sniff uses `Common::prepareForOutput()` in two places to make
whitespace visible in error messages. Among other things, the method
replaces spaces with a UTF-8 middot character. The middot character
in the output can break tools that pipe PHPCS output through non-UTF-8
environments (see squizlabs/PHP_CodeSniffer 2652).

For the `IncorrectSingle` error, the spaces-only case now reports a
descriptive count like "found 2 spaces" instead of displaying
middot characters. For tabs or newlines, `prepareForOutput()` is kept
but with `[' ']` as the `$exclude` parameter, so that any spaces in the
content are displayed as-is.

For the `IncorrectYieldFrom` and `IncorrectYieldFromWithComment` errors,
the same `$exclude` parameter is passed, so that spaces between `yield`
and `from` are no longer replaced with middots.
@jrfnl jrfnl force-pushed the exclude-space-from-prepare-for-output branch from c6b8515 to 7c86cae Compare March 18, 2026 12:53
@jrfnl jrfnl merged commit fab6c1e into PHPCSStandards:4.x Mar 18, 2026
53 checks passed
@jrfnl jrfnl deleted the exclude-space-from-prepare-for-output branch March 18, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LanguageConstructSpacingSniff changes spaces to ·· (utf-8 middot)

2 participants