From df217132f08d68736be15bf877286ec3e2d558fe Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 6 Mar 2026 17:42:52 -0300 Subject: [PATCH 1/2] Squiz/FunctionDeclarationArgumentSpacing: exclude space from prepareForOutput() 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. --- .../Functions/FunctionDeclarationArgumentSpacingSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 6143b0972f..cb0e492332 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -280,7 +280,7 @@ public function processBracket(File $phpcsFile, int $openBracket) $data[] = strlen($gap); } else { // Gap contains more than just spaces: render these for better clarity. - $data[] = '"' . Common::prepareForOutput($gap) . '"'; + $data[] = '"' . Common::prepareForOutput($gap, [' ']) . '"'; } $fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data); From 7c86caec4bce8c0f87cfca4a89ca3c5b5c565fdc Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 6 Mar 2026 21:01:47 +0000 Subject: [PATCH 2/2] Generic/LanguageConstructSpacing: exclude space from prepareForOutput() 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. --- .../WhiteSpace/LanguageConstructSpacingSniff.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php index 42fd6573ab..5970431142 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php @@ -113,7 +113,7 @@ public function process(File $phpcsFile, int $stackPtr) } $error = 'Language constructs must be followed by a single space; expected 1 space between YIELD FROM found "%s"'; - $data = [Common::prepareForOutput($found)]; + $data = [Common::prepareForOutput($found, [' '])]; if ($hasComment === true) { $phpcsFile->addError($error, $stackPtr, 'IncorrectYieldFromWithComment', $data); @@ -139,8 +139,16 @@ public function process(File $phpcsFile, int $stackPtr) if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { $content = $tokens[($stackPtr + 1)]['content']; if ($content !== ' ') { - $error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"'; - $data = [Common::prepareForOutput($content)]; + if (trim($content, ' ') === '') { + // Only space characters: report the count. + $found = $tokens[($stackPtr + 1)]['length'] . ' spaces'; + } else { + // Contains tabs or newlines: make them visible. + $found = '"' . Common::prepareForOutput($content, [' ']) . '"'; + } + + $error = 'Language constructs must be followed by a single space; expected 1 space but found %s'; + $data = [$found]; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data); if ($fix === true) { $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');