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
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;

use Psr\Log\LoggerInterface;
use Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Source\ParentControllerWithPrivatePromotedProperty;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;

final class ReUseExistingRequiredMethod extends ParentControllerWithPrivatePromotedProperty
{
private TranslatorInterface $translator;

#[Required]
public function autowireTranslator(TranslatorInterface $translator): void
{
$this->translator = $translator;
}

#[Route('/example', name: 'example')]
public function example(LoggerInterface $logger)
{
$logger->log('level', $this->translator->trans('value'));
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;

use Psr\Log\LoggerInterface;
use Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Source\ParentControllerWithPrivatePromotedProperty;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;

final class ReUseExistingRequiredMethod extends ParentControllerWithPrivatePromotedProperty
{
private TranslatorInterface $translator;
private \Psr\Log\LoggerInterface $logger;

#[Required]
public function autowireTranslator(TranslatorInterface $translator, \Psr\Log\LoggerInterface $logger): void
{
$this->translator = $translator;
$this->logger = $logger;
}

#[Route('/example', name: 'example')]
public function example()
{
$this->logger->log('level', $this->translator->trans('value'));
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,13 @@ private function hasAccessibleParentProperty(Class_ $class, string $propertyName
*/
private function addRequiredAutowireClassMethod(Class_ $class, array $propertyMetadatas): void
{
$autowireMethodName = $this->resolveAutowireMethodName($class);

$autowireClassMethod = $class->getMethod($autowireMethodName);
// re-use existing #[Required] method, to keep single injection point
$autowireClassMethod = $this->resolveExistingRequiredClassMethod($class);
$isNewClassMethod = ! $autowireClassMethod instanceof ClassMethod;

if (! $autowireClassMethod instanceof ClassMethod) {
$autowireMethodName = $this->resolveAutowireMethodName($class);

$autowireClassMethod = new ClassMethod(new Identifier($autowireMethodName), [
'flags' => Modifiers::PUBLIC,
'returnType' => new Identifier('void'),
Expand Down Expand Up @@ -495,6 +496,45 @@ private function addRequiredAutowireClassMethod(Class_ $class, array $propertyMe
}
}

/**
* Existing #[Required] setter method, or a method named autowire*(), to re-use as injection point
*/
private function resolveExistingRequiredClassMethod(Class_ $class): ?ClassMethod
{
foreach ($class->getMethods() as $classMethod) {
if (! $classMethod->isPublic()) {
continue;
}

if ($classMethod->isStatic() || $classMethod->stmts === null) {
continue;
}

if ($this->hasRequiredAttribute($classMethod)) {
return $classMethod;
}

if (str_starts_with($classMethod->name->toString(), self::AUTOWIRE_METHOD_NAME_PREFIX)) {
return $classMethod;
}
}

return null;
}

private function hasRequiredAttribute(ClassMethod $classMethod): bool
{
foreach ($classMethod->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if ($this->isName($attribute->name, SymfonyAttribute::REQUIRED)) {
return true;
}
}
}

return false;
}

/**
* Suffix with the short class name, to keep the method unique in case of inheritance
*/
Expand Down
Loading