Skip to content

Commit

Permalink
[CodeQuality] Remove type addition on CompleteDynamicPropertiesRector (
Browse files Browse the repository at this point in the history
…#3248)

* [CodeQuality] Remove type on CompleteDynamicPropertiesRector

* use PhpDocTypeChanger

* remove unused public method

* remove unused public method

* Fix cs

* [ci-review] Rector Rectify

* Final touch: keep bool type

* Really Really Final touch: clean up

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 24, 2022
1 parent e920ade commit f8814b1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 65 deletions.
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;
}
}

0 comments on commit f8814b1

Please sign in to comment.