From 58f3e0cb15ccf65e9a364da08b272d9313554623 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Aug 2023 19:53:52 +0200 Subject: [PATCH] [DX] Make use of Laravel container - step #13 (#4708) --- .../Testing/PHPUnit/AbstractLazyTestCase.php | 7 ++- phpstan.neon | 3 ++ src/Console/Command/ListRulesCommand.php | 6 ++- src/Console/ConsoleApplication.php | 8 +-- .../LazyContainerFactory.php | 54 +++++++------------ ...urableRectorImportConfigCallsMergeTest.php | 22 -------- .../config/main_config_with_only_imports.php | 10 ---- .../config/two_sets_with_own_rename.php | 17 ------ 8 files changed, 39 insertions(+), 88 deletions(-) delete mode 100644 tests/DependencyInjection/config/main_config_with_only_imports.php delete mode 100644 tests/DependencyInjection/config/two_sets_with_own_rename.php diff --git a/packages/Testing/PHPUnit/AbstractLazyTestCase.php b/packages/Testing/PHPUnit/AbstractLazyTestCase.php index 2d49166e77a..7d167904af5 100644 --- a/packages/Testing/PHPUnit/AbstractLazyTestCase.php +++ b/packages/Testing/PHPUnit/AbstractLazyTestCase.php @@ -18,12 +18,17 @@ abstract class AbstractLazyTestCase extends TestCase * @return TType */ protected function make(string $class): object + { + return self::getContainer()->make($class); + } + + protected static function getContainer(): Container { if (! self::$container instanceof Container) { $lazyContainerFactory = new LazyContainerFactory(); self::$container = $lazyContainerFactory->create(); } - return self::$container->make($class); + return self::$container; } } diff --git a/phpstan.neon b/phpstan.neon index 8610a76b88e..a6bf89f3a8a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -704,3 +704,6 @@ parameters: - packages/Config/LazyRectorConfig.php - '#Property Rector\\Core\\Configuration\\ConfigInitializer\:\:\$rectors \(array\) does not accept iterable#' + + # false positive + - '#Parameter \#1 \$commands of method Symfony\\Component\\Console\\Application\:\:addCommands\(\) expects array, iterable given#' diff --git a/src/Console/Command/ListRulesCommand.php b/src/Console/Command/ListRulesCommand.php index 74db4feb6f3..02a75067f28 100644 --- a/src/Console/Command/ListRulesCommand.php +++ b/src/Console/Command/ListRulesCommand.php @@ -26,7 +26,7 @@ final class ListRulesCommand extends Command private array $rectors = []; /** - * @param RewindableGenerator $rectors + * @param RewindableGenerator|RectorInterface[] $rectors */ public function __construct( private readonly RectorOutputStyle $rectorOutputStyle, @@ -34,6 +34,10 @@ public function __construct( iterable $rectors ) { parent::__construct(); + if ($rectors instanceof RewindableGenerator) { + $rectors = $rectors->getIterator(); + } + $this->rectors = iterator_to_array($rectors); } diff --git a/src/Console/ConsoleApplication.php b/src/Console/ConsoleApplication.php index 31637648561..4bc2c81edc4 100644 --- a/src/Console/ConsoleApplication.php +++ b/src/Console/ConsoleApplication.php @@ -25,15 +25,17 @@ final class ConsoleApplication extends Application private const NAME = 'Rector'; /** - * @param RewindableGenerator $commands + * @param RewindableGenerator|Command[] $commands */ public function __construct(iterable $commands) { parent::__construct(self::NAME, VersionResolver::PACKAGE_VERSION); - $commands = iterator_to_array($commands->getIterator()); - Assert::allIsInstanceOf($commands, Command::class); + if ($commands instanceof RewindableGenerator) { + $commands = iterator_to_array($commands->getIterator()); + } + Assert::allIsInstanceOf($commands, Command::class); $this->addCommands($commands); // run this command, if no command name is provided diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 5e27faea5f8..64066d3ce8a 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -296,6 +296,18 @@ final class LazyContainerFactory DoctrineAnnotationDecorator::class, ]; + /** + * @var array + */ + private const PUBLIC_PHPSTAN_SERVICE_TYPES = [ + DependencyResolver::class, + ScopeFactory::class, + TypeNodeResolver::class, + FileHelper::class, + NodeScopeResolver::class, + ReflectionProvider::class, + ]; + /** * @var array> */ @@ -626,15 +638,6 @@ private function registerTagged(Container $container, array $classes, string $ta private function createPHPStanServices(LazyRectorConfig $lazyRectorConfig): void { - $lazyRectorConfig->singleton( - ReflectionProvider::class, - static function (Container $container): ReflectionProvider { - $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); - return $phpstanServiceFactory->createReflectionProvider(); - } - ); - - // @todo make generic $lazyRectorConfig->singleton(Parser::class, static function (Container $container) { $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); return $phpstanServiceFactory->createPHPStanParser(); @@ -645,30 +648,13 @@ static function (Container $container): ReflectionProvider { return $phpstanServiceFactory->createEmulativeLexer(); }); - $lazyRectorConfig->singleton(TypeNodeResolver::class, static function (Container $container) { - $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); - return $phpstanServiceFactory->createTypeNodeResolver(); - }); - - $lazyRectorConfig->singleton(NodeScopeResolver::class, static function (Container $container) { - $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); - return $phpstanServiceFactory->createNodeScopeResolver(); - }); - - $lazyRectorConfig->singleton(FileHelper::class, static function (Container $container) { - $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); - return $phpstanServiceFactory->createFileHelper(); - }); - - $lazyRectorConfig->singleton(DependencyResolver::class, static function (Container $container) { - $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); - return $phpstanServiceFactory->createDependencyResolver(); - }); - - // @todo make generic - $lazyRectorConfig->singleton(ScopeFactory::class, static function (Container $container): ScopeFactory { - $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); - return $phpstanServiceFactory->getByType(ScopeFactory::class); - }); + foreach (self::PUBLIC_PHPSTAN_SERVICE_TYPES as $publicPhpstanServiceType) { + $lazyRectorConfig->singleton($publicPhpstanServiceType, static function (Container $container) use ( + $publicPhpstanServiceType + ) { + $phpstanServiceFactory = $container->make(PHPStanServicesFactory::class); + return $phpstanServiceFactory->getByType($publicPhpstanServiceType); + }); + } } } diff --git a/tests/DependencyInjection/ConfigurableRectorImportConfigCallsMergeTest.php b/tests/DependencyInjection/ConfigurableRectorImportConfigCallsMergeTest.php index 678339042c5..ae5dbc7c517 100644 --- a/tests/DependencyInjection/ConfigurableRectorImportConfigCallsMergeTest.php +++ b/tests/DependencyInjection/ConfigurableRectorImportConfigCallsMergeTest.php @@ -32,13 +32,6 @@ public function testMainConfigValues(string $configFile, array $expectedConfigur public static function provideData(): Iterator { - yield [ - __DIR__ . '/config/main_config_with_only_imports.php', [ - 'old_2' => 'new_2', - 'old_1' => 'new_1', - ], - ]; - yield [ __DIR__ . '/config/main_config_with_override_value.php', [ 'old_2' => 'new_2', @@ -58,20 +51,5 @@ public static function provideData(): Iterator 'PHPUnit_Framework_MockObject_Invocation_Object' => 'PHPUnit\Framework\MockObject\Invocation\ObjectInvocation', ], ]; - - yield [ - __DIR__ . '/config/two_sets_with_own_rename.php', [ - 'Old' => 'New', - 'Twig_SimpleFilter' => 'Twig_Filter', - 'Twig_SimpleFunction' => 'Twig_Function', - 'Twig_SimpleTest' => 'Twig_Test', - 'PHPUnit_Framework_MockObject_Stub' => 'PHPUnit\Framework\MockObject\Stub', - 'PHPUnit_Framework_MockObject_Stub_Return' => 'PHPUnit\Framework\MockObject\Stub\ReturnStub', - 'PHPUnit_Framework_MockObject_Matcher_Parameters' => 'PHPUnit\Framework\MockObject\Matcher\Parameters', - 'PHPUnit_Framework_MockObject_Matcher_Invocation' => 'PHPUnit\Framework\MockObject\Matcher\Invocation', - 'PHPUnit_Framework_MockObject_MockObject' => 'PHPUnit\Framework\MockObject\MockObject', - 'PHPUnit_Framework_MockObject_Invocation_Object' => 'PHPUnit\Framework\MockObject\Invocation\ObjectInvocation', - ], - ]; } } diff --git a/tests/DependencyInjection/config/main_config_with_only_imports.php b/tests/DependencyInjection/config/main_config_with_only_imports.php deleted file mode 100644 index 632e17e57d3..00000000000 --- a/tests/DependencyInjection/config/main_config_with_only_imports.php +++ /dev/null @@ -1,10 +0,0 @@ -import(__DIR__ . '/first_config.php'); - $rectorConfig->import(__DIR__ . '/second_config.php'); -}; diff --git a/tests/DependencyInjection/config/two_sets_with_own_rename.php b/tests/DependencyInjection/config/two_sets_with_own_rename.php deleted file mode 100644 index eb4fa496486..00000000000 --- a/tests/DependencyInjection/config/two_sets_with_own_rename.php +++ /dev/null @@ -1,17 +0,0 @@ -import(PHPUnitSetList::PHPUNIT_60); - $rectorConfig->import(TwigSetList::TWIG_20); - - $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [ - 'Old' => 'New', - ]); -};