Skip to content

Commit

Permalink
FindReturnType: fix
Browse files Browse the repository at this point in the history
-> Call to undefined method phpDocumentor\Reflection\DocBlock\Tags\InvalidTag::getType()

ref:
- phpDocumentor/ReflectionDocBlock#260
- Roave#700
  • Loading branch information
voku committed Oct 29, 2020
1 parent 806105a commit 8792383
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/TypesFinder/FindParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,25 @@ public function __invoke(ReflectionFunctionAbstract $function, ?Namespace_ $name

$context = $this->makeContext->__invoke($namespace);

/** @var Param[] $paramTags */
$paramTags = $this
->docBlockFactory
->create($docComment, $context)
->getTagsByName('param');

foreach ($paramTags as $paramTag) {
if (! $paramTag instanceof Param) {
continue;
}

if ($node->var instanceof Error) {
throw new LogicException('PhpParser left an "Error" node in the parameters AST, this should NOT happen');
}

if ($paramTag->getVariableName() === $node->var->name) {
return $this->resolveTypes->__invoke(explode('|', (string) $paramTag->getType()), $context);
if ($paramTag->getVariableName() !== $node->var->name) {
continue;
}

return $this->resolveTypes->__invoke(explode('|', (string) $paramTag->getType()), $context);
}

return [];
Expand Down
14 changes: 10 additions & 4 deletions src/TypesFinder/FindPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ public function __invoke(ReflectionProperty $reflectionProperty, ?Namespace_ $na
}

$context = $this->makeContext->__invoke($namespace);
/** @var Var_[] $varTags */
$varTags = $this->docBlockFactory->create($docComment, $context)->getTagsByName('var');

$varTags = $this->docBlockFactory
->create($docComment, $context)
->getTagsByName('var');

return array_merge(
[],
...array_map(function (Var_ $varTag) use ($context) {
return $this->resolveTypes->__invoke(explode('|', (string) $varTag->getType()), $context);
...array_map(function ($varTag) use ($context) {
if ($varTag instanceof Var_) {
return $this->resolveTypes->__invoke(explode('|', (string) $varTag->getType()), $context);
}

return [];
}, $varTags)
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/TypesFinder/FindReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ public function __invoke(ReflectionFunctionAbstract $function, ?Namespace_ $name

$context = $this->makeContext->__invoke($namespace);

/** @var Return_[] $returnTags */
$returnTags = $this
->docBlockFactory
->create($docComment, $context)
->getTagsByName('return');

foreach ($returnTags as $returnTag) {
if (! $returnTag instanceof Return_) {
continue;
}

return $this->resolveTypes->__invoke(explode('|', (string) $returnTag->getType()), $context);
}

Expand Down
1 change: 1 addition & 0 deletions test/unit/TypesFinder/FindParameterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function parameterTypeProvider() : array
['@param int|string $foo', 'foo', [Types\Integer::class, Types\String_::class]],
['@param array $foo', 'foo', [Types\Array_::class]],
['@param \stdClass $foo', 'foo', [Types\Object_::class]],
['@param array{foo: string|int} $foo', 'foo', []],
['@param int|int[]|int[][] $foo', 'foo', [Types\Integer::class, Types\Array_::class, Types\Array_::class]],
['', 'foo', []],
['@param ?string $foo', 'foo', [Types\Nullable::class]],
Expand Down
1 change: 1 addition & 0 deletions test/unit/TypesFinder/FindPropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function propertyTypeProvider() : array
return [
['@var int|string $foo', [Types\Integer::class, Types\String_::class]],
['@var array $foo', [Types\Array_::class]],
['@var array{foo: string|int} $foo', []],
['@var \stdClass $foo', [Types\Object_::class]],
['@var int|int[]|int[][] $foo', [Types\Integer::class, Types\Array_::class, Types\Array_::class]],
['', []],
Expand Down
1 change: 1 addition & 0 deletions test/unit/TypesFinder/FindReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function returnTypeProvider() : array
return [
['@return int|string', [Types\Integer::class, Types\String_::class]],
['@return array', [Types\Array_::class]],
['@return array{foo: string|int}', []],
['@return \stdClass', [Types\Object_::class]],
['@return int|int[]|int[][]', [Types\Integer::class, Types\Array_::class, Types\Array_::class]],
['@return int A comment about the return type', [Types\Integer::class]],
Expand Down

0 comments on commit 8792383

Please # to comment.