Skip to content

Commit

Permalink
Add parent constructor call only once (#3094)
Browse files Browse the repository at this point in the history
  • Loading branch information
mttsch committed Nov 26, 2022
1 parent c98ec0e commit 9a93704
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Fixture;

use Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\CategoryRepository;
use Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\ProductRepository;
use Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\SomeProduct;

abstract class ParentFixture3Constructor {
public function __construct(protected SomeProduct $someProduct)
{
}
}

final class Fixture3Controller extends ParentFixture3Constructor
{
public function default(CategoryRepository $categoryRepository, ProductRepository $productRepository)
{
$products = $productRepository->fetchAll();
$categories = $categoryRepository->fetchAll();
}
}

?>
-----
<?php

namespace Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Fixture;

use Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\CategoryRepository;
use Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\ProductRepository;
use Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\SomeProduct;

abstract class ParentFixture3Constructor {
public function __construct(protected SomeProduct $someProduct)
{
}
}

final class Fixture3Controller extends ParentFixture3Constructor
{
public function __construct(\Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\SomeProduct $someProduct, private \Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\CategoryRepository $categoryRepository, private \Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\ProductRepository $productRepository)
{
parent::__construct($someProduct);
}
public function default()
{
$products = $this->productRepository->fetchAll();
$categories = $this->categoryRepository->fetchAll();
}
}

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

declare(strict_types=1);

namespace Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source;

final class CategoryRepository
{

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\CategoryRepository"></service>
<service id="Rector\Tests\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector\Source\ProductRepository"></service>
</services>
</container>
16 changes: 8 additions & 8 deletions src/NodeManipulator/ClassDependencyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ private function addPromotedProperty(Class_ $class, PropertyMetadata $propertyMe
$constructClassMethod = $this->nodeFactory->createPublicMethod(MethodName::CONSTRUCT);
$constructClassMethod->params[] = $param;
$this->classInsertManipulator->addAsFirstMethod($class, $constructClassMethod);
}

/** @var Scope $scope */
$scope = $class->getAttribute(AttributeKey::SCOPE);
/** @var Scope $scope */
$scope = $class->getAttribute(AttributeKey::SCOPE);

$this->dependencyClassMethodDecorator->decorateConstructorWithParentDependencies(
$class,
$constructClassMethod,
$scope
);
$this->dependencyClassMethodDecorator->decorateConstructorWithParentDependencies(
$class,
$constructClassMethod,
$scope
);
}
}

private function hasClassParentClassMethod(Class_ $class, string $methodName): bool
Expand Down

0 comments on commit 9a93704

Please sign in to comment.