Skip to content

Commit 7c86cae

Browse files
rodrigoprimojrfnl
authored andcommitted
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.
1 parent df21713 commit 7c86cae

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function process(File $phpcsFile, int $stackPtr)
113113
}
114114

115115
$error = 'Language constructs must be followed by a single space; expected 1 space between YIELD FROM found "%s"';
116-
$data = [Common::prepareForOutput($found)];
116+
$data = [Common::prepareForOutput($found, [' '])];
117117

118118
if ($hasComment === true) {
119119
$phpcsFile->addError($error, $stackPtr, 'IncorrectYieldFromWithComment', $data);
@@ -139,8 +139,16 @@ public function process(File $phpcsFile, int $stackPtr)
139139
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
140140
$content = $tokens[($stackPtr + 1)]['content'];
141141
if ($content !== ' ') {
142-
$error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"';
143-
$data = [Common::prepareForOutput($content)];
142+
if (trim($content, ' ') === '') {
143+
// Only space characters: report the count.
144+
$found = $tokens[($stackPtr + 1)]['length'] . ' spaces';
145+
} else {
146+
// Contains tabs or newlines: make them visible.
147+
$found = '"' . Common::prepareForOutput($content, [' ']) . '"';
148+
}
149+
150+
$error = 'Language constructs must be followed by a single space; expected 1 space but found %s';
151+
$data = [$found];
144152
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data);
145153
if ($fix === true) {
146154
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');

0 commit comments

Comments
 (0)