Skip to content

Commit d5f558b

Browse files
committed
fix: add a test case for invalid @param tag values
1 parent f9aa867 commit d5f558b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Parser/NodeVisitor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,17 @@ protected function addConstant(ClassConstNode $node)
430430

431431
/**
432432
* @param array[] $tags
433-
* @phpstan-param array{array{string,bool}|empty-array,string,string} $tags
433+
* @phpstan-param array{array{string,bool}|empty-array|string,string,string} $tags
434434
*
435435
* @return array|null
436436
* @phpstan-return non-empty-array{non-empty-array{string,bool},string,string}|null
437437
*/
438438
private function findParameterInTags(array $tags, string $tagName): ?array
439439
{
440440
foreach ($tags as $tag) {
441+
if (! is_array($tag)) {
442+
continue;
443+
}
441444
if (count($tag) < 2) {
442445
continue;
443446
}

tests/Parser/NodeVisitorTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,43 @@ public function testUpdateMethodParametersFromTags(): void
365365
])
366366
);
367367
}
368+
369+
/**
370+
* @see NodeVisitor::updateMethodParametersFromTags
371+
*/
372+
public function testUpdateMethodParametersFromInvalidTags(): void
373+
{
374+
$docBlockParser = new DocBlockParser();
375+
$docBlockNode = $docBlockParser->parse(
376+
'/**' . "\n"
377+
. '* @param type1 $param1 Description' . "\n"
378+
. '* @param $param8 Description 4' . "\n"
379+
. '* @param $param9' . "\n"
380+
. '* @param foo' . "\n"
381+
. '* @param type1 $param4 Description 4' . "\n"
382+
. '* @param array[\Illuminate\Notifications\Channels\Notification] $notification' . "\n"
383+
. '**/' . "\n"
384+
);
385+
$parserContext = new ParserContext(new TrueFilter(), new DocBlockParser(), new Standard());
386+
$visitor = new NodeVisitor($parserContext);
387+
$function = new FunctionReflection('fun1', 0);
388+
389+
$param1 = (new ParameterReflection('param1', 0));
390+
$param1->setHint('array');
391+
$function->addParameter($param1);
392+
393+
$param2 = (new ParameterReflection('param2', 0));
394+
$function->addParameter($param2);
395+
396+
$this->assertSame(
397+
[
398+
'The "param2" parameter of the method "fun1" is missing a @param tag',
399+
'The method "fun1" has "6" @param tags but only "2" where expected.',
400+
],
401+
$this->callMethod($visitor, 'updateMethodParametersFromTags', [
402+
$function,
403+
$docBlockNode->getTag('param')
404+
])
405+
);
406+
}
368407
}

0 commit comments

Comments
 (0)