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,41 @@
<?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\ParentControllerWithProtectedProperty;
use Symfony\Component\Routing\Annotation\Route;

final class ReUseParentProtectedProperty extends ParentControllerWithProtectedProperty
{
#[Route('/example', name: 'example')]
public function example(LoggerInterface $logger)
{
$logger->log('level', '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\ParentControllerWithProtectedProperty;
use Symfony\Component\Routing\Annotation\Route;

final class ReUseParentProtectedProperty extends ParentControllerWithProtectedProperty
{
#[Route('/example', name: 'example')]
public function example()
{
$this->logger->log('level', 'value');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

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

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class ParentControllerWithProtectedProperty extends AbstractController
{
public function __construct(protected LoggerInterface $logger)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\NodeManipulator\ClassDependencyManipulator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer;
use Rector\Symfony\CodeQuality\NodeAnalyzer\ParamConverterClassesResolver;
Expand Down Expand Up @@ -47,7 +49,8 @@ public function __construct(
private readonly ClassDependencyManipulator $classDependencyManipulator,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly ParamConverterClassesResolver $paramConverterClassesResolver,
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -225,14 +228,20 @@ public function refactor(Node $node): ?Node

$paramName = $this->getName($param->var);
$paramsToRemove[] = [$classMethod, $key];
$propertyMetadatas[$paramName] = new PropertyMetadata($paramName, $paramType);
$methodParamNamesToReplace[$classMethod->name->toString()][] = $paramName;
$removedMethodArgPositions[$classMethod->name->toString()][] = $key;

// the parent class already provides the very same service, re-use it instead of adding own one
if ($this->hasAccessibleParentProperty($node, $paramName, $paramType)) {
continue;
}

$propertyMetadatas[$paramName] = new PropertyMetadata($paramName, $paramType);
}
}

// nothing to move
if ($propertyMetadatas === []) {
if ($paramsToRemove === []) {
return null;
}

Expand Down Expand Up @@ -350,6 +359,33 @@ private function hasConflictedParamName(
return false;
}

/**
* Is there a protected/public property of the same name and compatible type in a parent class?
*/
private function hasAccessibleParentProperty(Class_ $class, string $propertyName, ObjectType $objectType): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if (! $classReflection instanceof ClassReflection) {
return false;
}

foreach ($classReflection->getParents() as $parentClassReflection) {
if (! $parentClassReflection->hasNativeProperty($propertyName)) {
continue;
}

$nativePropertyReflection = $parentClassReflection->getNativeProperty($propertyName);
if ($nativePropertyReflection->isPrivate()) {
continue;
}

return $objectType->isSuperTypeOf($nativePropertyReflection->getReadableType())
->yes();
}

return false;
}

private function isUsedInStaticClosureUse(ClassMethod $classMethod, string $paramName): bool
{
if ($classMethod->stmts === null) {
Expand Down
Loading