rector/src/NodeDecorator/PropertyTypeDecorator.php

64 lines
2.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\NodeDecorator;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\PhpVersionFeature;
final class PropertyTypeDecorator
{
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\Php\PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
public function __construct(PhpDocInfoFactory $phpDocInfoFactory, PhpVersionProvider $phpVersionProvider, StaticTypeMapper $staticTypeMapper, PhpDocTypeChanger $phpDocTypeChanger)
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->phpVersionProvider = $phpVersionProvider;
$this->staticTypeMapper = $staticTypeMapper;
$this->phpDocTypeChanger = $phpDocTypeChanger;
}
public function decorate(Property $property, ?Type $type) : void
{
if (!$type instanceof Type) {
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
$phpParserType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PROPERTY);
if ($phpParserType instanceof Node) {
$property->type = $phpParserType;
if ($type instanceof GenericObjectType) {
$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $type);
}
return;
}
}
$this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $type);
}
}