rector/rules/DowngradePhp80/Rector/Property/DowngradeUnionTypeTypedPropertyRector.php
Tomas Votruba f41cfb1de8 Updated Rector to commit b4392749f4
b4392749f4 Fix some code samples (#535)
2021-07-28 12:55:48 +00:00

85 lines
2.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\DowngradePhp80\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector\DowngradeUnionTypeTypedPropertyRectorTest
*/
final class DowngradeUnionTypeTypedPropertyRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
public function __construct(\Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger)
{
$this->phpDocTypeChanger = $phpDocTypeChanger;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Property::class];
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Removes union type property type definition, adding `@var` annotations instead.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
private string|int $property;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var string|int
*/
private $property;
}
CODE_SAMPLE
)]);
}
/**
* @param Property $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($node->type === null) {
return null;
}
if (!$this->shouldRemoveProperty($node)) {
return null;
}
$this->decoratePropertyWithDocBlock($node, $node->type);
$node->type = null;
return $node;
}
private function decoratePropertyWithDocBlock(\PhpParser\Node\Stmt\Property $property, \PhpParser\Node $typeNode) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
if ($phpDocInfo->getVarTagValueNode() !== null) {
return;
}
$newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($typeNode);
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $newType);
}
private function shouldRemoveProperty(\PhpParser\Node\Stmt\Property $property) : bool
{
if ($property->type === null) {
return \false;
}
// Check it is the union type
return $property->type instanceof \PhpParser\Node\UnionType;
}
}