This commit is contained in:
Tomas Votruba 2019-09-11 16:10:47 +02:00
parent 408f5372dd
commit 4dcdf4c890
4 changed files with 24 additions and 3 deletions

View File

@ -15,6 +15,7 @@ use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwarePhpDocNode;
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareReturnTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareVarTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Contract\Ast\AttributeAwareNodeInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\StaticTypeMapper;
/**
@ -189,6 +190,8 @@ final class PhpDocInfo
public function getByType(string $type): ?PhpDocTagValueNode
{
$this->ensureTypeIsTagValueNode($type, __METHOD__);
foreach ($this->phpDocNode->children as $phpDocChildNode) {
if ($phpDocChildNode instanceof PhpDocTagNode) {
if (is_a($phpDocChildNode->value, $type, true)) {
@ -213,4 +216,18 @@ final class PhpDocInfo
return null;
}
private function ensureTypeIsTagValueNode(string $type, string $location): void
{
if (is_a($type, PhpDocTagValueNode::class, true)) {
return;
}
throw new ShouldNotHappenException(sprintf(
'Type "%s" passed to "%s()" method must be child of "%s"',
$type,
$location,
PhpDocTagValueNode::class
));
}
}

View File

@ -2,7 +2,9 @@
namespace Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc;
interface DoctrineRelationTagValueNodeInterface
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
interface DoctrineRelationTagValueNodeInterface extends PhpDocTagValueNode
{
public function getTargetEntity(): ?string;

View File

@ -57,7 +57,9 @@ final class PropertyTypeDeclarationRector extends AbstractRector
return null;
}
if ($this->docBlockManipulator->hasTag($node, '@var')) {
// is already set
$currentVarType = $this->docBlockManipulator->getVarType($node);
if (! $currentVarType instanceof MixedType) {
return null;
}

View File

@ -165,7 +165,7 @@ CODE_SAMPLE
return null;
}
if (! $this->docBlockManipulator->hasTag($property, 'var')) {
if (! $this->docBlockManipulator->getVarType()) {
$this->docBlockManipulator->changeVarTag($property, $type);
}