Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class WithArrayType
/**
* @var array<string, bool>
*/
public array $someProperty;
public $someProperty;
public function addSome(string $name)
{
$this->someProperty[$name] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 5 additions & 62 deletions rules/CodeQuality/NodeFactory/PropertyTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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);
}

/**
Expand All @@ -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;
}
}