Skip to content

Commit fab6c1e

Browse files
authored
Merge pull request #1389 from rodrigoprimo/exclude-space-from-prepare-for-output
Sniffs: exclude space from prepareForOutput() in error messages
2 parents 8b1c267 + 7c86cae commit fab6c1e

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

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), ' ');

src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function processBracket(File $phpcsFile, int $openBracket)
280280
$data[] = strlen($gap);
281281
} else {
282282
// Gap contains more than just spaces: render these for better clarity.
283-
$data[] = '"' . Common::prepareForOutput($gap) . '"';
283+
$data[] = '"' . Common::prepareForOutput($gap, [' ']) . '"';
284284
}
285285

286286
$fix = $phpcsFile->addFixableError($error, $typeHintToken, 'SpacingAfterHint', $data);

0 commit comments

Comments
 (0)