Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -288,7 +289,20 @@ public function addReturnTag(Node $node, Type $newType): void
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);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/NodeTypeResolver/src/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): TypeNo
$unionTypesNodes[] = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($unionedType);
}

$unionTypesNodes = array_unique($unionTypesNodes);

return new AttributeAwareUnionTypeNode($unionTypesNodes);
}

Expand Down Expand Up @@ -129,6 +131,10 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): TypeNo
return new IdentifierTypeNode('\\' . $phpStanType->getClassName());
}

if ($phpStanType instanceof NullType) {
return new IdentifierTypeNode('null');
}

throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>