mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
fix AddArrayReturnDocTypeRector for existing comment
This commit is contained in:
parent
7a497d9cd4
commit
97eb67be92
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user