Skip to content

Commit 3fd5bff

Browse files
committed
Fixed bug #2626 : PSR12.Files.FileHeader detects @var annotations as file docblocks
1 parent 50a20de commit 3fd5bff

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29+
- PSR12.Files.FileHeader now has better detection of file comments
30+
-- No longer ignores comments preceding a use, namespace, or declare statement
2931
- Fixed bug #2615 : Constant visibility false positive on non-class constants
3032
- Fixed bug #2616 : PSR12.Files.FileHeader false positive when file only contains docblock
3133
- Fixed bug #2619 : PSR12.Files.FileHeader locks up when inline comment is the last content in a file
3234
- Fixed bug #2621 : PSR12.Classes.AnonClassDeclaration.CloseBraceSameLine false positive for anon class passed as function argument
3335
-- Thanks to Martins Sipenko for the patch
3436
- Fixed bug #2623 : PSR12.ControlStructures.ControlStructureSpacing not ignoring indentation inside multi-line string arguments
3537
- Fixed bug #2624 : PSR12.Traits.UseDeclaration doesnt apply the correct indent during auto fixing
38+
- Fixed bug #2626 : PSR12.Files.FileHeader detects @var annotations as file docblocks
3639
</notes>
3740
<contents>
3841
<dir name="/">

src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function process(File $phpcsFile, $stackPtr)
6161

6262
$foundDocblock = false;
6363

64+
$commentOpeners = Tokens::$scopeOpeners;
65+
unset($commentOpeners[T_NAMESPACE]);
66+
unset($commentOpeners[T_DECLARE]);
67+
unset($commentOpeners[T_USE]);
68+
6469
do {
6570
switch ($tokens[$next]['code']) {
6671
case T_DOC_COMMENT_OPEN_TAG:
@@ -72,16 +77,29 @@ public function process(File $phpcsFile, $stackPtr)
7277
// Make sure this is not a code-level docblock.
7378
$end = $tokens[$next]['comment_closer'];
7479
$docToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
75-
if (isset(Tokens::$scopeOpeners[$tokens[$docToken]['code']]) === false
80+
if (isset($commentOpeners[$tokens[$docToken]['code']]) === false
7681
&& isset(Tokens::$methodPrefixes[$tokens[$docToken]['code']]) === false
7782
) {
78-
$foundDocblock = true;
79-
$headerLines[] = [
80-
'type' => 'docblock',
81-
'start' => $next,
82-
'end' => $end,
83-
];
84-
}
83+
// Check for an @var annotation.
84+
$annotation = false;
85+
for ($i = $next; $i < $end; $i++) {
86+
if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG
87+
&& strtolower($tokens[$i]['content']) === '@var'
88+
) {
89+
$annotation = true;
90+
break;
91+
}
92+
}
93+
94+
if ($annotation === false) {
95+
$foundDocblock = true;
96+
$headerLines[] = [
97+
'type' => 'docblock',
98+
'start' => $next,
99+
'end' => $end,
100+
];
101+
}
102+
}//end if
85103

86104
$next = $end;
87105
break;

src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.2.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/**
55
* This file contains an example of coding styles.
66
*/
7-
87
declare(strict_types=1);
98

109

src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public function getErrorList($testFile='')
3131
case 'FileHeaderUnitTest.2.inc':
3232
return [
3333
1 => 1,
34-
8 => 1,
35-
19 => 1,
36-
21 => 1,
37-
25 => 1,
34+
6 => 1,
35+
7 => 1,
36+
18 => 1,
37+
20 => 1,
38+
24 => 1,
3839
];
3940
case 'FileHeaderUnitTest.3.inc':
4041
return [

0 commit comments

Comments
 (0)