Skip to content

Commit ec13aba

Browse files
committed
FullyQualifiedClassNameInAnnotationSniff supports more ways how to write inline doccomment
1 parent 7f1e7ea commit ec13aba

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/FullyQualifiedClassNameInAnnotationSniff.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $annotationTagPointer)
4040
return;
4141
}
4242

43-
$typeHints = explode('|', preg_split('~\\s+~', trim($tokens[$annotationContentPointer]['content']))[0]);
43+
$annotationContent = trim($tokens[$annotationContentPointer]['content']);
44+
if ($annotationTagName === '@var' && preg_match('~^\$\\S+\\s+(.+)~', $annotationContent, $matches)) {
45+
$typeHintsDefinition = $matches[1];
46+
} else {
47+
$typeHintsDefinition = preg_split('~\\s+~', $annotationContent)[0];
48+
}
49+
50+
$typeHints = explode('|', $typeHintsDefinition);
4451
foreach ($typeHints as $typeHint) {
4552
$typeHint = preg_replace('~(\[\])+$~', '', $typeHint);
4653
$lowercasedTypeHint = strtolower($typeHint);

tests/Sniffs/Namespaces/FullyQualifiedClassNameInAnnotationSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testErrors()
1414
{
1515
$report = $this->checkFile(__DIR__ . '/data/fullyQualifiedClassNameInAnnotationErrors.php');
1616

17-
$this->assertSame(11, $report->getErrorCount());
17+
$this->assertSame(12, $report->getErrorCount());
1818

1919
$this->assertSniffError($report, 13, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME);
2020
$this->assertSniffError($report, 17, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME);
@@ -27,6 +27,7 @@ public function testErrors()
2727
$this->assertSniffError($report, 49, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME);
2828
$this->assertSniffError($report, 57, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \FooNamespace\FooClass in @return should be referenced via a fully qualified name');
2929
$this->assertSniffError($report, 57, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME, 'Class name \Iterator in @return should be referenced via a fully qualified name');
30+
$this->assertSniffError($report, 61, FullyQualifiedClassNameInAnnotationSniff::CODE_NON_FULLY_QUALIFIED_CLASS_NAME);
3031
}
3132

3233
}

tests/Sniffs/Namespaces/data/fullyQualifiedClassNameInAnnotationErrors.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function traversable()
5858
*/
5959
public function resultSet()
6060
{
61-
61+
/** @var $coo FooClass */
62+
$coo = $this->get();
6263
}
6364

6465
}

tests/Sniffs/Namespaces/data/fullyQualifiedClassNameInAnnotationNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public function __construct(string $foo)
2020
{
2121
/** @var self $boo */
2222
$boo = $this->get();
23+
24+
/** @var $coo self */
25+
$coo = $this->get();
2326
}
2427

2528
/**

0 commit comments

Comments
 (0)