Make DocBlockAnalyzer::getTagByName() always return a tag

This commit is contained in:
Tomas Votruba 2018-09-28 00:11:39 +08:00
parent fe7ab04430
commit 5337e6cf84
3 changed files with 19 additions and 4 deletions

View File

@ -229,11 +229,12 @@ final class MergeIsCandidateRector extends AbstractRector
private function resolveSingleParamTypesFromClassMethod(ClassMethod $classMethod): array
{
// add getNodeType() by $refactorClassMethod "@param" doc type
$paramNode = $this->docBlockAnalyzer->getTagByName($classMethod, 'param');
if ($paramNode === null) {
if (! $this->docBlockAnalyzer->hasTag($classMethod, 'param')) {
return [];
}
$paramNode = $this->docBlockAnalyzer->getTagByName($classMethod, 'param');
/** @var ParamTagValueNode $paramTagValueNode */
$paramTagValueNode = $paramNode->value;

View File

@ -0,0 +1,9 @@
<?php declare(strict_types=1);
namespace Rector\NodeTypeResolver\Exception;
use Exception;
final class MissingTagException extends Exception
{
}

View File

@ -7,6 +7,7 @@ use PhpParser\Comment\Doc;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Exception\MissingTagException;
use Rector\PhpParser\CurrentNodeProvider;
use Symplify\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Symplify\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
@ -149,9 +150,13 @@ final class DocBlockAnalyzer
/**
* @final
*/
public function getTagByName(Node $node, string $name): ?PhpDocTagNode
public function getTagByName(Node $node, string $name): PhpDocTagNode
{
return $this->getTagsByName($node, $name)[0] ?? null;
if (! $this->hasTag($node, $name)) {
throw new MissingTagException('Tag "%s" was not found at "%s" node.', $name, get_class($node));
}
return $this->getTagsByName($node, $name)[0];
}
private function updateNodeWithPhpDocInfo(Node $node, PhpDocInfo $phpDocInfo): void