Skip to content

Commit 1ce9081

Browse files
committed
DisallowSpaceIndent: Allow the sniff to fix space indents for multi-line /* */ comments
Code sample: ```php /* * Some text */ ``` The tokenizer tokenizes this code as: ``` T_WHITESPACE T_COMMENT T_COMMENT T_COMMENT ``` In other words, the whitespace at the start of subsequent comment lines is tokenized as part of the `T_COMMENT`. In effect, this meant that these lines would not throw the appropriate error, nor be fixed by this sniff. This commit fixes that. Includes unit tests.
1 parent dc9c345 commit 1ce9081

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function process(File $phpcsFile, $stackPtr)
7272
T_WHITESPACE => true,
7373
T_INLINE_HTML => true,
7474
T_DOC_COMMENT_WHITESPACE => true,
75+
T_COMMENT => true,
7576
);
7677

7778
$tokens = $phpcsFile->getTokens();
@@ -88,10 +89,13 @@ public function process(File $phpcsFile, $stackPtr)
8889
$content = $tokens[$i]['content'];
8990
}
9091

91-
// If this is an inline HTML token, split the content into
92-
// indentation whitespace and the actual HTML/text.
92+
// If this is an inline HTML token or a subsequent line of a multi-line comment,
93+
// split the content into indentation whitespace and the actual HTML/text.
9394
$nonWhitespace = '';
94-
if ($tokens[$i]['code'] === T_INLINE_HTML && preg_match('`^(\s*)(\S.*)`s', $content, $matches) > 0) {
95+
if (($tokens[$i]['code'] === T_INLINE_HTML
96+
|| $tokens[$i]['code'] === T_COMMENT)
97+
&& preg_match('`^(\s*)(\S.*)`s', $content, $matches) > 0
98+
) {
9599
if (isset($matches[1]) === true) {
96100
$content = $matches[1];
97101
}
@@ -115,8 +119,11 @@ public function process(File $phpcsFile, $stackPtr)
115119
continue;
116120
}
117121

118-
if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE && $content === ' ') {
119-
// Ignore file/class-level DocBlock, especially for recording metrics.
122+
if (($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE
123+
|| $tokens[$i]['code'] === T_COMMENT)
124+
&& $content === ' '
125+
) {
126+
// Ignore all non-indented comments, especially for recording metrics.
120127
continue;
121128
}
122129

0 commit comments

Comments
 (0)