Skip to content

Commit c54cb10

Browse files
committed
Tokenizers/PHP: bug fix - empty block comment
This commit fixes an edge case tokenizer bug, where a - completely empty, not even whitespace - _block comment_, would be tokenized as a docblock. Without this commit, the `/**/` code snippet was tokenized as: ``` 8 | L07 | C 1 | CC 0 | ( 0) | T_DOC_COMMENT_OPEN_TAG | [ 4]: /**/ 9 | L07 | C 5 | CC 0 | ( 0) | T_DOC_COMMENT_CLOSE_TAG | [ 0]: ``` With the fix applied, it will be tokenized as: ``` 8 | L07 | C 1 | CC 0 | ( 0) | T_COMMENT | [ 4]: /**/ ```
1 parent d96c7d6 commit c54cb10

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ protected function tokenize($string)
786786

787787
if ($tokenIsArray === true
788788
&& ($token[0] === T_DOC_COMMENT
789-
|| ($token[0] === T_COMMENT && strpos($token[1], '/**') === 0))
789+
|| ($token[0] === T_COMMENT && strpos($token[1], '/**') === 0 && $token[1] !== '/**/'))
790790
) {
791791
$commentTokens = $commentTokenizer->tokenizeString($token[1], $this->eolChar, $newStackPtr);
792792
foreach ($commentTokens as $commentToken) {

tests/Core/Tokenizer/Comment/SingleLineDocBlockTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
/* testEmptyBlockCommentNoWhiteSpace */
4+
/**/
5+
36
/* testEmptyDocblockWithWhiteSpace */
47
/** */
58

tests/Core/Tokenizer/Comment/SingleLineDocBlockTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ public static function dataDocblockOpenerCloser()
5858
}//end dataDocblockOpenerCloser()
5959

6060

61+
/**
62+
* Verify an empty block comment is tokenized as T_COMMENT, not as a docblock.
63+
*
64+
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
65+
*
66+
* @return void
67+
*/
68+
public function testEmptyBlockCommentNoWhiteSpace()
69+
{
70+
$expectedSequence = [
71+
[T_COMMENT => '/**/'],
72+
];
73+
74+
$target = $this->getTargetToken('/* '.__FUNCTION__.' */', [T_COMMENT, T_DOC_COMMENT_OPEN_TAG]);
75+
76+
$this->checkTokenSequence($target, $expectedSequence);
77+
78+
}//end testEmptyBlockCommentNoWhiteSpace()
79+
80+
6181
/**
6282
* Verify tokenization of an empty, single line DocBlock.
6383
*

0 commit comments

Comments
 (0)