mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 18:00:10 +01:00
Merge pull request #2507 from rectorphp/fix-return-array-comment
Fix AddArrayReturnDocTypeRector for existing comment
This commit is contained in:
commit
21bbcff91b
@ -16,6 +16,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
|
|||||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
||||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
||||||
|
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
||||||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
||||||
use PHPStan\Type\ArrayType;
|
use PHPStan\Type\ArrayType;
|
||||||
@ -288,7 +289,20 @@ final class DocBlockManipulator
|
|||||||
return;
|
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);
|
$this->addTypeSpecificTag($node, 'return', $newType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,6 +101,8 @@ final class StaticTypeMapper
|
|||||||
$unionTypesNodes[] = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($unionedType);
|
$unionTypesNodes[] = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($unionedType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$unionTypesNodes = array_unique($unionTypesNodes);
|
||||||
|
|
||||||
return new AttributeAwareUnionTypeNode($unionTypesNodes);
|
return new AttributeAwareUnionTypeNode($unionTypesNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,6 +131,10 @@ final class StaticTypeMapper
|
|||||||
return new IdentifierTypeNode('\\' . $phpStanType->getClassName());
|
return new IdentifierTypeNode('\\' . $phpStanType->getClassName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($phpStanType instanceof NullType) {
|
||||||
|
return new IdentifierTypeNode('null');
|
||||||
|
}
|
||||||
|
|
||||||
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
|
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