Skip to content

Commit 9704d3b

Browse files
committed
FullyQualifiedClassNameInAnnotationSniff: Fixed false positive
1 parent 8080bb3 commit 9704d3b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

SlevomatCodingStandard/Helpers/TypeHintHelper.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,20 @@ public static function isTemplate(File $phpcsFile, int $docCommentOpenPointer, s
8989
if ($tokens[$docCommentOwnerPointer]['code'] !== T_FUNCTION) {
9090
return false;
9191
}
92+
} else {
93+
$docCommentOwnerPointer = null;
9294
}
9395

94-
$classPointer = ClassHelper::getClassPointer($phpcsFile, $docCommentOpenPointer);
95-
if ($classPointer === null) {
96-
return false;
96+
$pointerToFindClass = $docCommentOpenPointer;
97+
if ($docCommentOwnerPointer === null) {
98+
$functionPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $docCommentOpenPointer - 1);
99+
if ($functionPointer !== null) {
100+
$pointerToFindClass = $functionPointer;
101+
}
97102
}
98103

104+
$classPointer = ClassHelper::getClassPointer($phpcsFile, $pointerToFindClass);
105+
99106
$classDocCommentOpenPointer = DocCommentHelper::findDocCommentOpenToken($phpcsFile, $classPointer);
100107
if ($classDocCommentOpenPointer === null) {
101108
return false;

tests/Sniffs/Namespaces/data/fullyQualifiedClassNameInAnnotationNoErrors.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace FooNamespace;
44

5+
use stdClass;
6+
57
/**
68
* @template TemplateAboveClass
79
*/
@@ -119,7 +121,10 @@ public function usingTemplateAboveMethod()
119121
*/
120122
public function usingTemplateAboveClass()
121123
{
124+
/** @var TemplateAboveClass $instance */
125+
$instance = new \stdClass();
122126

127+
return $instance;
123128
}
124129

125130
/** @var TemplateAboveClass */

0 commit comments

Comments
 (0)