Skip to content

Commit

Permalink
Fixed bug #2626 : PSR12.Files.FileHeader detects @var annotations as …
Browse files Browse the repository at this point in the history
…file docblocks
  • Loading branch information
gsherwood committed Oct 3, 2019
1 parent 50a20de commit 3fd5bff
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
<notes>
- PSR12.Files.FileHeader now has better detection of file comments
-- No longer ignores comments preceding a use, namespace, or declare statement
- Fixed bug #2615 : Constant visibility false positive on non-class constants
- Fixed bug #2616 : PSR12.Files.FileHeader false positive when file only contains docblock
- Fixed bug #2619 : PSR12.Files.FileHeader locks up when inline comment is the last content in a file
- Fixed bug #2621 : PSR12.Classes.AnonClassDeclaration.CloseBraceSameLine false positive for anon class passed as function argument
-- Thanks to Martins Sipenko for the patch
- Fixed bug #2623 : PSR12.ControlStructures.ControlStructureSpacing not ignoring indentation inside multi-line string arguments
- Fixed bug #2624 : PSR12.Traits.UseDeclaration doesnt apply the correct indent during auto fixing
- Fixed bug #2626 : PSR12.Files.FileHeader detects @var annotations as file docblocks
</notes>
<contents>
<dir name="/">
Expand Down
34 changes: 26 additions & 8 deletions src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function process(File $phpcsFile, $stackPtr)

$foundDocblock = false;

$commentOpeners = Tokens::$scopeOpeners;
unset($commentOpeners[T_NAMESPACE]);
unset($commentOpeners[T_DECLARE]);
unset($commentOpeners[T_USE]);

do {
switch ($tokens[$next]['code']) {
case T_DOC_COMMENT_OPEN_TAG:
Expand All @@ -72,16 +77,29 @@ public function process(File $phpcsFile, $stackPtr)
// Make sure this is not a code-level docblock.
$end = $tokens[$next]['comment_closer'];
$docToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
if (isset(Tokens::$scopeOpeners[$tokens[$docToken]['code']]) === false
if (isset($commentOpeners[$tokens[$docToken]['code']]) === false
&& isset(Tokens::$methodPrefixes[$tokens[$docToken]['code']]) === false
) {
$foundDocblock = true;
$headerLines[] = [
'type' => 'docblock',
'start' => $next,
'end' => $end,
];
}
// Check for an @var annotation.
$annotation = false;
for ($i = $next; $i < $end; $i++) {
if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG
&& strtolower($tokens[$i]['content']) === '@var'
) {
$annotation = true;
break;
}
}

if ($annotation === false) {
$foundDocblock = true;
$headerLines[] = [
'type' => 'docblock',
'start' => $next,
'end' => $end,
];
}
}//end if

$next = $end;
break;
Expand Down
1 change: 0 additions & 1 deletion src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/**
* This file contains an example of coding styles.
*/

declare(strict_types=1);


Expand Down
9 changes: 5 additions & 4 deletions src/Standards/PSR12/Tests/Files/FileHeaderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public function getErrorList($testFile='')
case 'FileHeaderUnitTest.2.inc':
return [
1 => 1,
8 => 1,
19 => 1,
21 => 1,
25 => 1,
6 => 1,
7 => 1,
18 => 1,
20 => 1,
24 => 1,
];
case 'FileHeaderUnitTest.3.inc':
return [
Expand Down

0 comments on commit 3fd5bff

Please sign in to comment.