Skip to content

Commit c1d5942

Browse files
committed
Ignore invalid @template and @type annotations
1 parent dcc4d9e commit c1d5942

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

SlevomatCodingStandard/Helpers/TypeHintHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ private static function isTemplate(File $phpcsFile, int $docCommentOpenPointer,
246246

247247
/** @var TemplateAnnotation $templateAnnotation */
248248
foreach ($annotations[$templateAnnotationName] as $templateAnnotation) {
249+
if ($templateAnnotation->isInvalid()) {
250+
continue;
251+
}
252+
249253
if ($templateAnnotation->getTemplateName() === $typeHint) {
250254
return true;
251255
}
@@ -329,6 +333,10 @@ private static function isAlias(File $phpcsFile, int $docCommentOpenPointer, str
329333

330334
/** @var TypeAliasAnnotation|TypeImportAnnotation $aliasAnnotation */
331335
foreach ($annotations[$aliasAnnotationName] as $aliasAnnotation) {
336+
if ($aliasAnnotation->isInvalid()) {
337+
continue;
338+
}
339+
332340
if ($aliasAnnotation->getAlias() === $typeHint) {
333341
return true;
334342
}

tests/Helpers/TypeHintHelperTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,31 @@ public function testIsTypeDefinedInAnnotation(string $typeHintName, bool $isTemp
346346
self::assertSame($isTemplate, TypeHintHelper::isTypeDefinedInAnnotation($phpcsFile, $docCommentOpenPointer, $typeHintName));
347347
}
348348

349+
/**
350+
* @return mixed[][]
351+
*/
352+
public function dataIsTypeDefinedInAnnotationWhenAnnotationIsInvalid(): array
353+
{
354+
return [
355+
[22, 'Alias'],
356+
[29, 'Template'],
357+
];
358+
}
359+
360+
/**
361+
* @dataProvider dataIsTypeDefinedInAnnotationWhenAnnotationIsInvalid
362+
* @param int $line
363+
* @param string $type
364+
*/
365+
public function testIsTypeDefinedInAnnotationWhenAnnotationIsInvalid(int $line, string $type): void
366+
{
367+
$phpcsFile = $this->getCodeSnifferFile(__DIR__ . '/data/typeHintsDefinedInAnnotation.php');
368+
369+
$docCommentOpenPointer = $this->findPointerByLineAndType($phpcsFile, $line, T_DOC_COMMENT_OPEN_TAG);
370+
371+
self::assertNotNull($docCommentOpenPointer);
372+
373+
self::assertFalse(TypeHintHelper::isTypeDefinedInAnnotation($phpcsFile, $docCommentOpenPointer, $type));
374+
}
375+
349376
}

tests/Helpers/data/typeHintsDefinedInAnnotation.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,32 @@
66
function simpleFunction() {
77

88
}
9+
10+
/**
11+
* @psalm-type Alias array<
12+
* string,
13+
* (class-string<Factory\FactoryInterface>|Factory\FactoryInterface)
14+
* |callable(ContainerInterface,string,array<mixed>|null)
15+
* >
16+
*
17+
* @template
18+
*/
19+
class WithInvalidTypes
20+
{
21+
22+
/**
23+
* @return Alias
24+
*/
25+
public function withInvalidAlias() {
26+
27+
}
28+
29+
/**
30+
* @return Template
31+
*/
32+
public function withInvalidTemplate() {
33+
34+
}
35+
36+
}
37+

0 commit comments

Comments
 (0)