Skip to content

Commit b4da369

Browse files
committed
feat: report invalid @param tags
1 parent d5f558b commit b4da369

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Parser/NodeVisitor.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ private function findParameterInTags(array $tags, string $tagName): ?array
451451
return null;
452452
}
453453

454+
/**
455+
* @param array[] $tags All the tags
456+
*
457+
* @return string[] The invalid tags
458+
*/
459+
private function getInvalidTags(array $tags): array
460+
{
461+
$invalidTags = [];
462+
foreach ($tags as $tag) {
463+
if (! is_array($tag)) {
464+
$invalidTags[] = $tag;
465+
}
466+
}
467+
return $invalidTags;
468+
}
469+
454470
/**
455471
* @param FunctionReflection|MethodReflection $method
456472
* @param array[] $tags
@@ -496,6 +512,21 @@ protected function updateMethodParametersFromTags(Reflection $method, array $tag
496512
);
497513
}
498514

515+
$invalidTags = $this->getInvalidTags($tags);
516+
if (count($invalidTags) > 0) {
517+
$errors[] = sprintf(
518+
'The method "%s" has "%d" invalid @param tags.',
519+
$method->getName(),
520+
count($invalidTags)
521+
);
522+
foreach ($invalidTags as $invalidTag) {
523+
$errors[] = sprintf(
524+
'Invalid @param tag on "%s": "%s"',
525+
$method->getName(),
526+
$invalidTag
527+
);
528+
}
529+
}
499530
return $errors;
500531
}
501532

tests/Parser/NodeVisitorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,39 @@ public function testUpdateMethodParametersFromInvalidTags(): void
397397
[
398398
'The "param2" parameter of the method "fun1" is missing a @param tag',
399399
'The method "fun1" has "6" @param tags but only "2" where expected.',
400+
'The method "fun1" has "1" invalid @param tags.',
401+
'Invalid @param tag on "fun1": "array[\Illuminate\Notifications\Channels\Notification] $notification"',
402+
],
403+
$this->callMethod($visitor, 'updateMethodParametersFromTags', [
404+
$function,
405+
$docBlockNode->getTag('param')
406+
])
407+
);
408+
}
409+
410+
/**
411+
* @see NodeVisitor::updateMethodParametersFromTags
412+
*/
413+
public function testUpdateMethodParametersFromInvalidTagsReport(): void
414+
{
415+
$docBlockParser = new DocBlockParser();
416+
$docBlockNode = $docBlockParser->parse(
417+
'/**' . "\n"
418+
. '* @param array[\Illuminate\Notifications\Channels\Notification] $notification' . "\n"
419+
. '**/' . "\n"
420+
);
421+
$parserContext = new ParserContext(new TrueFilter(), new DocBlockParser(), new Standard());
422+
$visitor = new NodeVisitor($parserContext);
423+
$function = new FunctionReflection('fun1', 0);
424+
425+
$param1 = (new ParameterReflection('notification', 0));
426+
$function->addParameter($param1);
427+
428+
$this->assertSame(
429+
[
430+
'The "notification" parameter of the method "fun1" is missing a @param tag',
431+
'The method "fun1" has "1" invalid @param tags.',
432+
'Invalid @param tag on "fun1": "array[\Illuminate\Notifications\Channels\Notification] $notification"',
400433
],
401434
$this->callMethod($visitor, 'updateMethodParametersFromTags', [
402435
$function,

0 commit comments

Comments
 (0)