Skip to content

Commit

Permalink
Improve TypedPropertyFromAssignsRector performance (#3749)
Browse files Browse the repository at this point in the history
* Improve TypedPropertyFromAssignsRector performance

* implement MinPhpVersionInterface

* drop PhpVersionProvider
  • Loading branch information
staabm committed May 7, 2023
1 parent ffec9f0 commit 34eddac
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\Php74\Guard\MakePropertyTypedGuard;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\NodeTypeAnalyzer\PropertyTypeDecorator;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\TypedPropertyFromAssignsRectorTest
*/
final class TypedPropertyFromAssignsRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface
final class TypedPropertyFromAssignsRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface, MinPhpVersionInterface
{
/**
* @api
Expand All @@ -49,7 +49,6 @@ public function __construct(
private readonly PropertyTypeDecorator $propertyTypeDecorator,
private readonly VarTagRemover $varTagRemover,
private readonly MakePropertyTypedGuard $makePropertyTypedGuard,
private readonly PhpVersionProvider $phpVersionProvider,
) {
}

Expand Down Expand Up @@ -102,6 +101,11 @@ public function getNodeTypes(): array
return [Property::class];
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}

/**
* @param Property $node
*/
Expand All @@ -111,6 +115,11 @@ public function refactor(Node $node): ?Node
return null;
}

// non-private property can be anything with not inline public configured
if (! $node->isPrivate() && ! $this->inlinePublic) {
return null;
}

$inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty($node);
if (! $inferredType instanceof Type) {
return null;
Expand All @@ -121,23 +130,12 @@ public function refactor(Node $node): ?Node
}

$inferredType = $this->decorateTypeWithNullableIfDefaultPropertyNull($node, $inferredType);

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($inferredType, TypeKind::PROPERTY);
if ($typeNode === null) {
return null;
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
return null;
}

// non-private property can be anything with not inline public configured
if (! $node->isPrivate() && ! $this->inlinePublic) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
if ($inferredType instanceof UnionType) {
$this->propertyTypeDecorator->decoratePropertyUnionType(
$inferredType,
Expand Down Expand Up @@ -176,4 +174,5 @@ private function decorateTypeWithNullableIfDefaultPropertyNull(Property $propert

return TypeCombinator::addNull($inferredType);
}

}

0 comments on commit 34eddac

Please sign in to comment.