Skip to content

Commit

Permalink
Make ServiceGetterToConstructorInjectionRector report only if node ha…
Browse files Browse the repository at this point in the history
…s changed, retarget to Class_ node with higher priority (#2772)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Aug 17, 2022
1 parent d86e599 commit 27b891b
Showing 1 changed file with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,43 +119,61 @@ public function run()
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
return [Class_::class];
}

/**
* @param MethodCall $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$classLike = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $classLike instanceof Class_) {
if ($this->classAnalyzer->isAnonymousClass($node)) {
return null;
}

if ($this->classAnalyzer->isAnonymousClass($classLike)) {
// skip empty class
$classStmts = $node->stmts;
if ($classStmts === []) {
return null;
}

foreach ($this->methodCallToServices as $methodCallToService) {
if (! $this->isObjectType($node->var, $methodCallToService->getOldObjectType())) {
continue;
}
$hasChanged = false;
$class = $node;

if (! $this->isName($node->name, $methodCallToService->getOldMethod())) {
continue;
$this->traverseNodesWithCallable($classStmts, function (Node $node) use ($class, &$hasChanged): ?PropertyFetch {
if (! $node instanceof MethodCall) {
return null;
}

$serviceObjectType = new ObjectType($methodCallToService->getServiceType());
foreach ($this->methodCallToServices as $methodCallToService) {
if (! $this->isObjectType($node->var, $methodCallToService->getOldObjectType())) {
continue;
}

if (! $this->isName($node->name, $methodCallToService->getOldMethod())) {
continue;
}

$serviceObjectType = new ObjectType($methodCallToService->getServiceType());

$propertyName = $this->propertyNaming->fqnToVariableName($serviceObjectType);

$propertyName = $this->propertyNaming->fqnToVariableName($serviceObjectType);
$propertyMetadata = new PropertyMetadata($propertyName, $serviceObjectType, Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);

$propertyMetadata = new PropertyMetadata($propertyName, $serviceObjectType, Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($classLike, $propertyMetadata);
$hasChanged = true;

return new PropertyFetch(new Variable('this'), new Identifier($propertyName));
}

return null;
});

return new PropertyFetch(new Variable('this'), new Identifier($propertyName));
if ($hasChanged) {
return $node;
}

return $node;
return null;
}

/**
Expand Down

0 comments on commit 27b891b

Please sign in to comment.