Skip to content

Commit e0d7fe2

Browse files
committed
PHPCSDebug/TokenList: visualize whitespace characters
Visualize space and tab characters within whitespace tokens. This is primarily a change to improve usability, but will also make testing this sniff easier as we no longer have to deal with trailing whitespace issues.
1 parent 0f6d944 commit e0d7fe2

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

PHPCSDebug/Sniffs/Debug/TokenListSniff.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ public function process(File $phpcsFile, $stackPtr)
109109
|| (\defined('T_DOC_COMMENT_WHITESPACE')
110110
&& $token['code'] === \T_DOC_COMMENT_WHITESPACE)
111111
) {
112-
if (\strpos($content, "\t") !== false) {
113-
$content = \str_replace("\t", '\t', $content);
114-
}
112+
$content = $this->visualizeWhitespace($content);
113+
115114
if (isset($token['orig_content'])) {
116-
$content .= $sep . 'Orig: ' . \str_replace("\t", '\t', $token['orig_content']);
115+
$content .= $sep . 'Orig: ' . $this->visualizeWhitespace($token['orig_content']);
117116
}
118117
}
119118

@@ -134,4 +133,21 @@ public function process(File $phpcsFile, $stackPtr)
134133
// Only do this once per file.
135134
return ($phpcsFile->numTokens + 1);
136135
}
136+
137+
/**
138+
* Visualize tabs and spaces in arbitrary whitespace tokens.
139+
*
140+
* @param string $text Arbitrary text.
141+
*
142+
* @return string
143+
*/
144+
protected function visualizeWhitespace($text)
145+
{
146+
$whitespaceMap = [
147+
' ' => '', // U+2E31. May not be supported in all CLI clients/fonts. Alternatively, switch to U+00B7.
148+
"\t" => '', // U+2192. The better U+21E5 is not widely enough supported.
149+
];
150+
151+
return \strtr($text, $whitespaceMap);
152+
}
137153
}

0 commit comments

Comments
 (0)