diff --git a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/fixture.php.inc index cfdb69c468e..71bcd344f6b 100644 --- a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/fixture.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/fixture.php.inc @@ -18,7 +18,10 @@ namespace Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector class Fixture { - public int $value; + /** + * @var int + */ + public $value; public function set() { $this->value = 5; diff --git a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/with_array_type.php.inc b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/with_array_type.php.inc index 19d3bd586a6..ceb7537d426 100644 --- a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/with_array_type.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/Fixture/with_array_type.php.inc @@ -21,7 +21,7 @@ final class WithArrayType /** * @var array */ - public array $someProperty; + public $someProperty; public function addSome(string $name) { $this->someProperty[$name] = true; diff --git a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/FixtureUnionTypes/multiple_types.php.inc b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/FixtureUnionTypes/multiple_types.php.inc index 3a14aedb13e..78c0bc77575 100644 --- a/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/FixtureUnionTypes/multiple_types.php.inc +++ b/rules-tests/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector/FixtureUnionTypes/multiple_types.php.inc @@ -22,7 +22,10 @@ namespace Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector class MultipleTypes { - public int|string|bool $value; + /** + * @var int|string|bool + */ + public $value; public function set() { $this->value = 5; diff --git a/rules/CodeQuality/NodeFactory/PropertyTypeDecorator.php b/rules/CodeQuality/NodeFactory/PropertyTypeDecorator.php index 75a5dcf83ba..7df81835b8f 100644 --- a/rules/CodeQuality/NodeFactory/PropertyTypeDecorator.php +++ b/rules/CodeQuality/NodeFactory/PropertyTypeDecorator.php @@ -4,30 +4,23 @@ namespace Rector\CodeQuality\NodeFactory; -use PhpParser\Node; use PhpParser\Node\ComplexType; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Property; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; -use Rector\Core\Php\PhpVersionProvider; -use Rector\Core\ValueObject\PhpVersionFeature; -use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Privatization\TypeManipulator\TypeNormalizer; use Rector\StaticTypeMapper\StaticTypeMapper; final class PropertyTypeDecorator { public function __construct( - private readonly PhpVersionProvider $phpVersionProvider, private readonly StaticTypeMapper $staticTypeMapper, private readonly PhpDocTypeChanger $phpDocTypeChanger, private readonly PhpDocInfoFactory $phpDocInfoFactory, - private readonly TypeNormalizer $typeNormalizer, + private readonly TypeNormalizer $typeNormalizer ) { } @@ -36,8 +29,10 @@ public function decorateProperty(Property $property, Type $propertyType): void // generalize false/true type to bool, as mostly default value but accepts both $propertyType = $this->typeNormalizer->generalizeConstantBoolTypes($propertyType); - $this->decoratePropertyWithVarDoc($property, $propertyType); - $this->decoratePropertyWithType($property, $propertyType); + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); + $phpDocInfo->makeMultiLined(); + + $this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType); } /** @@ -53,56 +48,4 @@ public function decoratePropertyWithDocBlock(Property $property, ComplexType|Ide $newType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($typeNode); $this->phpDocTypeChanger->changeVarType($phpDocInfo, $newType); } - - private function decoratePropertyWithVarDoc(Property $property, Type $propertyType): void - { - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); - $phpDocInfo->makeMultiLined(); - - if ($this->isNonMixedArrayType($propertyType)) { - $this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType); - $property->type = new Identifier('array'); - return; - } - - if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) { - $phpParserNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( - $propertyType, - TypeKind::PROPERTY - ); - if (! $phpParserNode instanceof Node) { - // fallback to doc type in PHP 7.4 - $this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType); - } - } else { - $this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType); - } - } - - private function decoratePropertyWithType(Property $property, Type $propertyType): void - { - if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) { - return; - } - - $phpParserNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY); - if (! $phpParserNode instanceof Node) { - return; - } - - $property->type = $phpParserNode; - } - - private function isNonMixedArrayType(Type $type): bool - { - if (! $type instanceof ArrayType) { - return false; - } - - if ($type->getKeyType() instanceof MixedType) { - return false; - } - - return ! $type->getItemType() instanceof MixedType; - } }