Skip to content

Commit

Permalink
[DependencyInjection] Add GetBySymfonyStringToConstructorInjectionRec…
Browse files Browse the repository at this point in the history
…tor (#688)

* register rule

* conver getContainer() as well

* covert this container property fetch as well

* deprecate ContainerGetToConstructorInjectionRector

* deprecate ContainerGetToConstructorInjectionRector, as handles same logic as GetToConstructorInjectionRector

* mark

* misc
  • Loading branch information
TomasVotruba authored Dec 1, 2024
1 parent da54cd8 commit 1138f04
Show file tree
Hide file tree
Showing 34 changed files with 391 additions and 492 deletions.
6 changes: 4 additions & 2 deletions config/sets/symfony/symfony-constructor-injection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
use Rector\Config\RectorConfig;
use Rector\Symfony\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector;
use Rector\Symfony\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector;
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;
use Rector\Symfony\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector;
use Rector\Symfony\Symfony28\Rector\MethodCall\GetToConstructorInjectionRector;
use Rector\Symfony\Symfony34\Rector\Closure\ContainerGetNameToTypeInTestsRector;
use Rector\Symfony\Symfony42\Rector\MethodCall\ContainerGetToConstructorInjectionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
// modern step-by-step narrow approach
ControllerGetByTypeToConstructorInjectionRector::class,
CommandGetByTypeToConstructorInjectionRector::class,
GetBySymfonyStringToConstructorInjectionRector::class,
TraitGetByTypeToInjectRector::class,

// legacy rules that require container fetch
ContainerGetToConstructorInjectionRector::class,
ContainerGetNameToTypeInTestsRector::class,
GetToConstructorInjectionRector::class,
]);
Expand Down
13 changes: 5 additions & 8 deletions config/sets/symfony/symfony42.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Symfony\Symfony42\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
use Rector\Symfony\Symfony42\Rector\New_\RootNodeTreeBuilderRector;
use Rector\Symfony\Symfony42\Rector\New_\StringToArrayArgumentProcessRector;
use Rector\Transform\Rector\ClassMethod\WrapReturnRector;
Expand Down Expand Up @@ -48,13 +47,11 @@
'Symfony\Component\Translation\TranslatorInterface' => 'Symfony\Contracts\Translation\TranslatorInterface',
]);

# related to "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" deprecation, @see https://github.com/rectorphp/rector/issues/1629
$rectorConfig->rule(ContainerGetToConstructorInjectionRector::class);

# https://symfony.com/blog/new-in-symfony-4-2-important-deprecations
$rectorConfig->rule(StringToArrayArgumentProcessRector::class);

$rectorConfig->rule(RootNodeTreeBuilderRector::class);
$rectorConfig->rules([
# https://symfony.com/blog/new-in-symfony-4-2-important-deprecations
StringToArrayArgumentProcessRector::class,
RootNodeTreeBuilderRector::class,
]);

$rectorConfig->ruleWithConfiguration(ArgumentAdderRector::class, [
new ArgumentAdder(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

final class IncludeGetContainer extends ContainerAwareCommand
{
public function configure()
{
$someType = $this->getContainer()->get(SomeService::class);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

final class IncludeGetContainer extends ContainerAwareCommand
{
public function __construct(private \Rector\Symfony\Tests\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector\Fixture\SomeService $someService)
{
parent::__construct();
}
public function configure()
{
$someType = $this->someService;
}
}

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

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture;

use Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class CoverContainerProperty extends Controller
{
public function configure()
{
$someType = $this->container->get(SomeService::class);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture;

use Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class CoverContainerProperty extends Controller
{
public function __construct(private \Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService $someService)
{
}
public function configure()
{
$someType = $this->someService;
}
}

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

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

final class CommandValidator extends ContainerAwareCommand
{
public function configure()
{
$someType = $this->get('validator');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

final class CommandValidator extends ContainerAwareCommand
{
public function __construct(private \Symfony\Component\Validator\Validator\ValidatorInterface $validator)
{
}
public function configure()
{
$someType = $this->validator;
}
}

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

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class EventDispatcherController extends Controller
{
public function configure()
{
$someType = $this->get('event_dispatcher');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

final class EventDispatcherController extends Controller
{
public function __construct(private \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher)
{
}
public function configure()
{
$someType = $this->eventDispatcher;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony42\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ContainerGetToConstructorInjectionRectorTest extends AbstractRectorTestCase
final class GetBySymfonyStringToConstructorInjectionRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(GetBySymfonyStringToConstructorInjectionRector::class);
};

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1138f04

Please sign in to comment.