Skip to content

Commit 1138f04

Browse files
authored
[DependencyInjection] Add GetBySymfonyStringToConstructorInjectionRector (#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
1 parent da54cd8 commit 1138f04

File tree

34 files changed

+391
-492
lines changed

34 files changed

+391
-492
lines changed

config/sets/symfony/symfony-constructor-injection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
use Rector\Config\RectorConfig;
66
use Rector\Symfony\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector;
77
use Rector\Symfony\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector;
8+
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;
9+
use Rector\Symfony\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector;
810
use Rector\Symfony\Symfony28\Rector\MethodCall\GetToConstructorInjectionRector;
911
use Rector\Symfony\Symfony34\Rector\Closure\ContainerGetNameToTypeInTestsRector;
10-
use Rector\Symfony\Symfony42\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
1112

1213
return static function (RectorConfig $rectorConfig): void {
1314
$rectorConfig->rules([
1415
// modern step-by-step narrow approach
1516
ControllerGetByTypeToConstructorInjectionRector::class,
1617
CommandGetByTypeToConstructorInjectionRector::class,
18+
GetBySymfonyStringToConstructorInjectionRector::class,
19+
TraitGetByTypeToInjectRector::class,
1720

1821
// legacy rules that require container fetch
19-
ContainerGetToConstructorInjectionRector::class,
2022
ContainerGetNameToTypeInTestsRector::class,
2123
GetToConstructorInjectionRector::class,
2224
]);

config/sets/symfony/symfony42.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
1616
use Rector\Renaming\Rector\Name\RenameClassRector;
1717
use Rector\Renaming\ValueObject\MethodCallRename;
18-
use Rector\Symfony\Symfony42\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
1918
use Rector\Symfony\Symfony42\Rector\New_\RootNodeTreeBuilderRector;
2019
use Rector\Symfony\Symfony42\Rector\New_\StringToArrayArgumentProcessRector;
2120
use Rector\Transform\Rector\ClassMethod\WrapReturnRector;
@@ -48,13 +47,11 @@
4847
'Symfony\Component\Translation\TranslatorInterface' => 'Symfony\Contracts\Translation\TranslatorInterface',
4948
]);
5049

51-
# related to "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" deprecation, @see https://github.com/rectorphp/rector/issues/1629
52-
$rectorConfig->rule(ContainerGetToConstructorInjectionRector::class);
53-
54-
# https://symfony.com/blog/new-in-symfony-4-2-important-deprecations
55-
$rectorConfig->rule(StringToArrayArgumentProcessRector::class);
56-
57-
$rectorConfig->rule(RootNodeTreeBuilderRector::class);
50+
$rectorConfig->rules([
51+
# https://symfony.com/blog/new-in-symfony-4-2-important-deprecations
52+
StringToArrayArgumentProcessRector::class,
53+
RootNodeTreeBuilderRector::class,
54+
]);
5855

5956
$rectorConfig->ruleWithConfiguration(ArgumentAdderRector::class, [
6057
new ArgumentAdder(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
7+
final class IncludeGetContainer extends ContainerAwareCommand
8+
{
9+
public function configure()
10+
{
11+
$someType = $this->getContainer()->get(SomeService::class);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector\Fixture;
20+
21+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22+
23+
final class IncludeGetContainer extends ContainerAwareCommand
24+
{
25+
public function __construct(private \Rector\Symfony\Tests\DependencyInjection\Rector\Class_\CommandGetByTypeToConstructorInjectionRector\Fixture\SomeService $someService)
26+
{
27+
parent::__construct();
28+
}
29+
public function configure()
30+
{
31+
$someType = $this->someService;
32+
}
33+
}
34+
35+
?>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture;
4+
5+
use Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService;
6+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7+
8+
final class CoverContainerProperty extends Controller
9+
{
10+
public function configure()
11+
{
12+
$someType = $this->container->get(SomeService::class);
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture;
21+
22+
use Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService;
23+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24+
25+
final class CoverContainerProperty extends Controller
26+
{
27+
public function __construct(private \Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Source\SomeService $someService)
28+
{
29+
}
30+
public function configure()
31+
{
32+
$someType = $this->someService;
33+
}
34+
}
35+
36+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
7+
final class CommandValidator extends ContainerAwareCommand
8+
{
9+
public function configure()
10+
{
11+
$someType = $this->get('validator');
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;
20+
21+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22+
23+
final class CommandValidator extends ContainerAwareCommand
24+
{
25+
public function __construct(private \Symfony\Component\Validator\Validator\ValidatorInterface $validator)
26+
{
27+
}
28+
public function configure()
29+
{
30+
$someType = $this->validator;
31+
}
32+
}
33+
34+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
7+
final class EventDispatcherController extends Controller
8+
{
9+
public function configure()
10+
{
11+
$someType = $this->get('event_dispatcher');
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector\Fixture;
20+
21+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
22+
23+
final class EventDispatcherController extends Controller
24+
{
25+
public function __construct(private \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher)
26+
{
27+
}
28+
public function configure()
29+
{
30+
$someType = $this->eventDispatcher;
31+
}
32+
}
33+
34+
?>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Symfony\Tests\Symfony42\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
5+
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
1010

11-
final class ContainerGetToConstructorInjectionRectorTest extends AbstractRectorTestCase
11+
final class GetBySymfonyStringToConstructorInjectionRectorTest extends AbstractRectorTestCase
1212
{
1313
#[DataProvider('provideData')]
1414
public function test(string $filePath): void
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Symfony\DependencyInjection\Rector\Class_\GetBySymfonyStringToConstructorInjectionRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(GetBySymfonyStringToConstructorInjectionRector::class);
10+
};

rules-tests/Symfony42/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Fixture/constant_as_service_regression.php.inc

Lines changed: 0 additions & 25 deletions
This file was deleted.

rules-tests/Symfony42/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Fixture/first_class.php.inc

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)