From 927449612108c5afc45de7fa4e67e22f5cdb3bd9 Mon Sep 17 00:00:00 2001 From: mssimi Date: Wed, 8 Aug 2018 14:54:18 +0200 Subject: [PATCH 1/3] ContainerGetToConstructorInjectionRector alias tests --- .../SomeKernelClass.php | 3 +++ ...nerGetToConstructorInjectionRectorTest.php | 1 + .../Correct/correct2.php.inc | 19 +++++++++++++++++++ .../Wrong/wrong2.php.inc | 11 +++++++++++ 4 files changed, 34 insertions(+) create mode 100644 packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc create mode 100644 packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php b/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php index a3f790db74d..f6a4e5959b9 100644 --- a/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php +++ b/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php @@ -21,6 +21,9 @@ final class SomeKernelClass extends Kernel protected function build(ContainerBuilder $containerBuilder): void { $containerBuilder->register('some_service', 'stdClass'); + //Symfony\Component\Translation\TranslatorInterface alias for "translator.data_collector" + //translator alias for "translator.data_collector" + //translator.data_collector Symfony\Component\Translation\DataCollectorTranslator } public function getCacheDir() diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php index baa0d6bf453..3ecbe4b5377 100644 --- a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php +++ b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php @@ -21,6 +21,7 @@ final class ContainerGetToConstructorInjectionRectorTest extends AbstractRectorT public function provideWrongToFixedFiles(): Iterator { yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; + yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc']; } protected function provideConfig(): string diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc new file mode 100644 index 00000000000..f9b0ed7f631 --- /dev/null +++ b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc @@ -0,0 +1,19 @@ +translator = $translator; + } + protected function execute() + { + $someService = $this->translator; + } +} diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc new file mode 100644 index 00000000000..a785afe076e --- /dev/null +++ b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc @@ -0,0 +1,11 @@ +getContainer()->get('translator'); + } +} From 2d3f64532f5cd94bf7d1fc834eb327bc4cd24ffc Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 8 Aug 2018 22:07:17 +0200 Subject: [PATCH 2/3] make tests pass --- .../ContainerGetToConstructorInjectionRector.php | 4 ++-- .../SomeKernelClass.php | 10 +++++++--- .../SomeTranslator.php | 8 ++++++++ .../SomeTranslatorInterface.php | 8 ++++++++ .../ContainerGetToConstructorInjectionRectorTest.php | 2 +- .../Correct/correct2.php.inc | 10 +++++----- 6 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeTranslator.php create mode 100644 packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeTranslatorInterface.php diff --git a/packages/Symfony/src/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector.php b/packages/Symfony/src/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector.php index c1c4162dfab..9f763d1d739 100644 --- a/packages/Symfony/src/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector.php +++ b/packages/Symfony/src/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector.php @@ -35,7 +35,7 @@ final class ContainerGetToConstructorInjectionRector extends AbstractToConstruct [ new CodeSample( <<<'CODE_SAMPLE' -class MyCommand extends ContainerAwareCommand +final class SomeCommand extends ContainerAwareCommand { public function someMethod() { @@ -47,7 +47,7 @@ class MyCommand extends ContainerAwareCommand CODE_SAMPLE , <<<'CODE_SAMPLE' -class MyCommand extends Command +final class SomeCommand extends Command { public function __construct(SomeService $someService) { diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php b/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php index f6a4e5959b9..f505786bde4 100644 --- a/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php +++ b/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeKernelClass.php @@ -2,6 +2,8 @@ namespace Rector\Symfony\Tests\FrameworkBundle\AbstractToConstructorInjectionRectorSource; +use Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator; +use Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslatorInterface; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -21,9 +23,11 @@ final class SomeKernelClass extends Kernel protected function build(ContainerBuilder $containerBuilder): void { $containerBuilder->register('some_service', 'stdClass'); - //Symfony\Component\Translation\TranslatorInterface alias for "translator.data_collector" - //translator alias for "translator.data_collector" - //translator.data_collector Symfony\Component\Translation\DataCollectorTranslator + + $containerBuilder->register('translator.data_collector', SomeTranslator::class); + + $containerBuilder->setAlias('translator', 'translator.data_collector'); + $containerBuilder->setAlias(SomeTranslatorInterface::class, 'translator.data_collector'); } public function getCacheDir() diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeTranslator.php b/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeTranslator.php new file mode 100644 index 00000000000..583c1fb2566 --- /dev/null +++ b/packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource/SomeTranslator.php @@ -0,0 +1,8 @@ +translator = $translator; + $this->someTranslator = $someTranslator; } protected function execute() { - $someService = $this->translator; + $someService = $this->someTranslator; } } From d42c3e711df4a267d7ad3a88c93553cd1e2c2d46 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 8 Aug 2018 22:09:35 +0200 Subject: [PATCH 3/3] fixup! make tests pass --- .../ContainerGetToConstructorInjectionRectorTest.php | 2 +- .../Correct/correct2.php.inc | 4 +++- .../Wrong/wrong2.php.inc | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php index 978b1644fa9..3ecbe4b5377 100644 --- a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php +++ b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/ContainerGetToConstructorInjectionRectorTest.php @@ -20,7 +20,7 @@ final class ContainerGetToConstructorInjectionRectorTest extends AbstractRectorT public function provideWrongToFixedFiles(): Iterator { -// yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; + yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc']; } diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc index db598e33f9e..e0238c6fc9b 100644 --- a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc +++ b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Correct/correct2.php.inc @@ -2,7 +2,7 @@ use Rector\Symfony\Tests\FrameworkBundle\ContainerGetToConstructorInjectionRector\Source\ContainerAwareParentClass; -final class MyCommand extends ContainerAwareParentClass +final class AnotherCommand extends ContainerAwareParentClass { /** * @var \Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator @@ -15,5 +15,7 @@ final class MyCommand extends ContainerAwareParentClass protected function execute() { $someService = $this->someTranslator; + + $someService = $this->someTranslator->translateSomething(); } } diff --git a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc index a785afe076e..801bbc060d0 100644 --- a/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc +++ b/packages/Symfony/tests/Rector/FrameworkBundle/ContainerGetToConstructorInjectionRector/Wrong/wrong2.php.inc @@ -2,10 +2,12 @@ use Rector\Symfony\Tests\FrameworkBundle\ContainerGetToConstructorInjectionRector\Source\ContainerAwareParentClass; -final class MyCommand extends ContainerAwareParentClass +final class AnotherCommand extends ContainerAwareParentClass { protected function execute() { $someService = $this->getContainer()->get('translator'); + + $someService = $this->getContainer()->get('translator')->translateSomething(); } }