rector/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictCons...

174 lines
6.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\TypedPropertyFromStrictConstructorRectorTest
*/
final class TypedPropertyFromStrictConstructorRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer
*/
private $trustedClassMethodPropertyTypeInferer;
/**
* @readonly
* @var \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover
*/
private $varTagRemover;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
/**
* @readonly
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
*/
private $constructorAssignDetector;
/**
* @readonly
* @var \Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard
*/
private $propertyTypeOverrideGuard;
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, PhpDocTypeChanger $phpDocTypeChanger, ConstructorAssignDetector $constructorAssignDetector, PropertyTypeOverrideGuard $propertyTypeOverrideGuard)
{
$this->trustedClassMethodPropertyTypeInferer = $trustedClassMethodPropertyTypeInferer;
$this->varTagRemover = $varTagRemover;
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->constructorAssignDetector = $constructorAssignDetector;
$this->propertyTypeOverrideGuard = $propertyTypeOverrideGuard;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add typed properties based only on strict constructor types', [new CodeSample(<<<'CODE_SAMPLE'
class SomeObject
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeObject
{
private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod) {
return null;
}
$hasChanged = \false;
foreach ($node->getProperties() as $property) {
if (!$this->propertyTypeOverrideGuard->isLegal($property)) {
continue;
}
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($property, $constructClassMethod);
if ($this->shouldSkipPropertyType($propertyType)) {
continue;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
// public property can be anything
if ($property->isPublic()) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType);
$hasChanged = \true;
continue;
}
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
if (!$propertyTypeNode instanceof Node) {
continue;
}
$propertyProperty = $property->props[0];
$propertyName = $this->nodeNameResolver->getName($property);
if ($this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {
$propertyProperty->default = null;
$hasChanged = \true;
}
if ($this->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
continue;
}
$property->type = $propertyTypeNode;
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $property);
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}
private function doesConflictWithDefaultValue(PropertyProperty $propertyProperty, Type $propertyType) : bool
{
if (!$propertyProperty->default instanceof Expr) {
return \false;
}
// the defaults can be in conflict
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default);
// type is not matching, skip it
return !$defaultType->isSuperTypeOf($propertyType)->yes();
}
private function isDoctrineCollectionType(Type $type) : bool
{
if (!$type instanceof ObjectType) {
return \false;
}
return $type->isInstanceOf('Doctrine\\Common\\Collections\\Collection')->yes();
}
private function shouldSkipPropertyType(Type $propertyType) : bool
{
if ($propertyType instanceof MixedType) {
return \true;
}
return $this->isDoctrineCollectionType($propertyType);
}
}