Skip to content

Commit

Permalink
[Transform] Handle property exists via Property Promotion on NewToMet…
Browse files Browse the repository at this point in the history
…hodCallRector (#1915)
  • Loading branch information
samsonasik committed Mar 8, 2022
1 parent 72710eb commit 7330674
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\Fixture;

use Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\Source\MyClass;

final class PropertyExistsViaPropertyPromotion
{
public function __construct(private \Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\Source\MyClassFactory $mySomeFactory)
{
}
public function default()
{
new MyClass('abcd');
$class = new MyClass('abcd');
}
}
?>
-----
<?php

namespace Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\Fixture;

use Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\Source\MyClass;

final class PropertyExistsViaPropertyPromotion
{
public function __construct(private \Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\Source\MyClassFactory $mySomeFactory)
{
}
public function default()
{
$this->mySomeFactory->create('abcd');
$class = $this->mySomeFactory->create('abcd');
}
}
?>
18 changes: 3 additions & 15 deletions rules/Transform/Rector/New_/NewToMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\PostRector\ValueObject\PropertyMetadata;
Expand All @@ -33,6 +33,7 @@ final class NewToMethodCallRector extends AbstractRector implements Configurable

public function __construct(
private readonly ClassNaming $classNaming,
private readonly PropertyManipulator $propertyManipulator,
private readonly PropertyToAddCollector $propertyToAddCollector
) {
}
Expand Down Expand Up @@ -102,7 +103,7 @@ public function refactor(Node $node): ?Node
continue;
}

$propertyName = $this->getExistingFactoryPropertyName(
$propertyName = $this->propertyManipulator->resolveExistingClassPropertyNameByType(
$class,
$newToMethodCall->getServiceObjectType()
);
Expand Down Expand Up @@ -137,17 +138,4 @@ public function configure(array $configuration): void

$this->newsToMethodCalls = $configuration;
}

private function getExistingFactoryPropertyName(Class_ $class, ObjectType $factoryObjectType): ?string
{
foreach ($class->getProperties() as $property) {
if (! $this->isObjectType($property, $factoryObjectType)) {
continue;
}

return $this->getName($property);
}

return null;
}
}
32 changes: 31 additions & 1 deletion src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand All @@ -33,7 +35,9 @@
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php80\NodeAnalyzer\PromotedPropertyResolver;
use Rector\ReadWrite\Guard\VariableToConstantGuard;
use Rector\ReadWrite\NodeAnalyzer\ReadWritePropertyAnalyzer;
use Symplify\PackageBuilder\Php\TypeChecker;
Expand Down Expand Up @@ -75,7 +79,9 @@ public function __construct(
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly ReflectionResolver $reflectionResolver,
private readonly NodeNameResolver $nodeNameResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PromotedPropertyResolver $promotedPropertyResolver
) {
}

Expand Down Expand Up @@ -193,6 +199,30 @@ public function isPropertyChangeable(Property $property): bool
return false;
}

public function resolveExistingClassPropertyNameByType(Class_ $class, Type $type): ?string
{
foreach ($class->getProperties() as $property) {
$propertyType = $this->nodeTypeResolver->getType($property);
if (! $propertyType->equals($type)) {
continue;
}

return $this->nodeNameResolver->getName($property);
}

$promotedPropertyParams = $this->promotedPropertyResolver->resolveFromClass($class);
foreach ($promotedPropertyParams as $promotedPropertyParam) {
$paramType = $this->nodeTypeResolver->getType($promotedPropertyParam);
if (! $paramType->equals($type)) {
continue;
}

return $this->nodeNameResolver->getName($promotedPropertyParam);
}

return null;
}

private function isInlineStmtWithConstructMethod(
PropertyFetch|StaticPropertyFetch $propertyFetch,
ClassMethod $classMethod
Expand Down

0 comments on commit 7330674

Please sign in to comment.