Skip to content

Commit 79a70d6

Browse files
committed
PHPCSDebug/TokenList: visualize whitespace characters (take two)
Follow up after PR 102. Comment tokens, most notably `T_COMMENT` and `T_DOC_COMMENT_STRING` will have the new line character following it included in the token content. This implies that potential _trailing whitespace_ (between the text and the new line character) will also be included in the token content. In a similar vein as for _plain_ whitespace, this trailing whitespace should be visualized. On top of that, for multi-line `/* */`-style comments, _leading_ whitespace will also be included in the token, so should also be visualized. Again, this is primarily a change to improve usability, but it will also make testing this sniff easier as we no longer have to deal with trailing whitespace issues.
1 parent e0d7fe2 commit 79a70d6

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

PHPCSDebug/Sniffs/Debug/TokenListSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use PHP_CodeSniffer\Sniffs\Sniff;
1414
use PHP_CodeSniffer\Files\File;
15+
use PHP_CodeSniffer\Util\Tokens;
1516

1617
/**
1718
* Lists how PHPCS tokenizes code.
@@ -116,6 +117,17 @@ public function process(File $phpcsFile, $stackPtr)
116117
}
117118
}
118119

120+
if (isset(Tokens::$commentTokens[$token['code']]) === true) {
121+
/*
122+
* Comment tokens followed by a new line, will have trailing whitespace
123+
* included in the token, so visualize it.
124+
* For multi-line star comments (like this one), this also applies to leading whitespace.
125+
*/
126+
$comment = \trim($content);
127+
$whitespace = \str_replace($comment, '###', $content);
128+
$content = \str_replace('###', $comment, $this->visualizeWhitespace($whitespace));
129+
}
130+
119131
$parenthesesCount = 0;
120132
if (isset($token['nested_parenthesis'])) {
121133
$parenthesesCount = \count($token['nested_parenthesis']);

0 commit comments

Comments
 (0)