fix AddArrayReturnDocTypeRector for existing comment

This commit is contained in:
TomasVotruba 2019-12-27 14:35:53 +01:00
parent 7a497d9cd4
commit 97eb67be92
3 changed files with 64 additions and 1 deletions

View File

@ -16,6 +16,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\ArrayType;
@ -288,7 +289,20 @@ final class DocBlockManipulator
return;
}
$this->removeTagFromNode($node, 'return');
if ($node->getDocComment()) {
$phpDocInfo = $this->createPhpDocInfoFromNode($node);
$returnTagValueNode = $phpDocInfo->getByType(ReturnTagValueNode::class);
// overide existing type
if ($returnTagValueNode) {
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
$returnTagValueNode->type = $newPHPStanPhpDocType;
$this->updateNodeWithPhpDocInfo($node, $phpDocInfo);
return;
}
}
$this->addTypeSpecificTag($node, 'return', $newType);
}

View File

@ -101,6 +101,8 @@ final class StaticTypeMapper
$unionTypesNodes[] = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($unionedType);
}
$unionTypesNodes = array_unique($unionTypesNodes);
return new AttributeAwareUnionTypeNode($unionTypesNodes);
}
@ -129,6 +131,10 @@ final class StaticTypeMapper
return new IdentifierTypeNode('\\' . $phpStanType->getClassName());
}
if ($phpStanType instanceof NullType) {
return new IdentifierTypeNode('null');
}
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
}

View File

@ -0,0 +1,43 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
class WithComment
{
/**
* @var int[]
*/
private $values;
/**
* @return mixed[] some integer values
*/
public function getValues()
{
return $this->values;
}
}
?>
-----
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
class WithComment
{
/**
* @var int[]
*/
private $values;
/**
* @return int[] some integer values
*/
public function getValues()
{
return $this->values;
}
}
?>