Skip to content

Commit

Permalink
[DX] Make use of Laravel container - step #13 (#4708)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 7, 2023
1 parent e2a6636 commit 58f3e0c
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 88 deletions.
7 changes: 6 additions & 1 deletion packages/Testing/PHPUnit/AbstractLazyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,6 @@ parameters:
- packages/Config/LazyRectorConfig.php

- '#Property Rector\\Core\\Configuration\\ConfigInitializer\:\:\$rectors \(array<Rector\\Core\\Contract\\Rector\\RectorInterface>\) does not accept iterable<Rector\\Core\\Contract\\Rector\\RectorInterface>#'

# false positive
- '#Parameter \#1 \$commands of method Symfony\\Component\\Console\\Application\:\:addCommands\(\) expects array<Symfony\\Component\\Console\\Command\\Command>, iterable<Symfony\\Component\\Console\\Command\\Command> given#'
6 changes: 5 additions & 1 deletion src/Console/Command/ListRulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ final class ListRulesCommand extends Command
private array $rectors = [];

/**
* @param RewindableGenerator<RectorInterface> $rectors
* @param RewindableGenerator<RectorInterface>|RectorInterface[] $rectors
*/
public function __construct(
private readonly RectorOutputStyle $rectorOutputStyle,
private readonly SkippedClassResolver $skippedClassResolver,
iterable $rectors
) {
parent::__construct();
if ($rectors instanceof RewindableGenerator) {
$rectors = $rectors->getIterator();
}

$this->rectors = iterator_to_array($rectors);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ final class ConsoleApplication extends Application
private const NAME = 'Rector';

/**
* @param RewindableGenerator<int, Command> $commands
* @param RewindableGenerator<int, Command>|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
Expand Down
54 changes: 20 additions & 34 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ final class LazyContainerFactory
DoctrineAnnotationDecorator::class,
];

/**
* @var array<class-string>
*/
private const PUBLIC_PHPSTAN_SERVICE_TYPES = [
DependencyResolver::class,
ScopeFactory::class,
TypeNodeResolver::class,
FileHelper::class,
NodeScopeResolver::class,
ReflectionProvider::class,
];

/**
* @var array<class-string<OutputFormatterInterface>>
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
],
];
}
}
10 changes: 0 additions & 10 deletions tests/DependencyInjection/config/main_config_with_only_imports.php

This file was deleted.

17 changes: 0 additions & 17 deletions tests/DependencyInjection/config/two_sets_with_own_rename.php

This file was deleted.

0 comments on commit 58f3e0c

Please sign in to comment.