rector/vendor/rector/rector-downgrade-php/src/NodeManipulator/PropertyDecorator.php
Tomas Votruba 740fedd8a5 Updated Rector to commit 74a7c51d383b8ad5c6adb10cd67fe5f7f8ff290d
74a7c51d38 [automated] Re-Generate Nodes/Rectors Documentation (#5664)
2024-02-25 00:28:09 +00:00

52 lines
1.8 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\NodeManipulator;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\StaticTypeMapper\StaticTypeMapper;
final class PropertyDecorator
{
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
public function __construct(PhpDocInfoFactory $phpDocInfoFactory, StaticTypeMapper $staticTypeMapper, PhpDocTypeChanger $phpDocTypeChanger)
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->staticTypeMapper = $staticTypeMapper;
$this->phpDocTypeChanger = $phpDocTypeChanger;
}
/**
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $property
* @param \PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|\PhpParser\Node\Name $typeNode
*/
public function decorateWithDocBlock($property, $typeNode) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
if ($phpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
return;
}
$newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($typeNode);
$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $newType);
}
}