Skip to content

Commit

Permalink
[DX] Make use of Laravel container - step #12 (#4707)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 7, 2023
1 parent efdbf98 commit e2a6636
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 280 deletions.
12 changes: 4 additions & 8 deletions packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\Command\ProcessCommand;
use Rector\Core\Kernel\RectorKernel;
use Rector\Parallel\Command\WorkerCommandLineFactory;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;

final class WorkerCommandLineFactoryTest extends TestCase
final class WorkerCommandLineFactoryTest extends AbstractLazyTestCase
{
/**
* @var string
Expand All @@ -34,11 +33,8 @@ final class WorkerCommandLineFactoryTest extends TestCase

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->create();

$this->workerCommandLineFactory = $containerBuilder->get(WorkerCommandLineFactory::class);
$this->processCommand = $containerBuilder->get(ProcessCommand::class);
$this->workerCommandLineFactory = $this->make(WorkerCommandLineFactory::class);
$this->processCommand = $this->make(ProcessCommand::class);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions packages-tests/Skipper/FileSystem/FnMatchPathNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Skipper\FileSystem\FnMatchPathNormalizer;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class FnMatchPathNormalizerTest extends TestCase
final class FnMatchPathNormalizerTest extends AbstractLazyTestCase
{
private FnMatchPathNormalizer $fnMatchPathNormalizer;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->create();

$this->fnMatchPathNormalizer = $containerBuilder->get(FnMatchPathNormalizer::class);
$this->fnMatchPathNormalizer = $this->make(FnMatchPathNormalizer::class);
}

#[DataProvider('providePaths')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,39 @@

namespace Rector\Tests\Skipper\SkipCriteriaResolver\SkippedPathsResolver;

use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class SkippedPathsResolverTest extends TestCase
final class SkippedPathsResolverTest extends AbstractLazyTestCase
{
private SkippedPathsResolver $skippedPathsResolver;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/config.php']);
SimpleParameterProvider::setParameter(Option::SKIP, [
// windows slashes
__DIR__ . '\non-existing-path',
__DIR__ . '/Fixture',
'*\Mask\*',
]);

$this->skippedPathsResolver = $this->make(SkippedPathsResolver::class);
}

$this->skippedPathsResolver = $containerBuilder->get(SkippedPathsResolver::class);
protected function tearDown(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, []);
}

public function test(): void
{
$skippedPaths = $this->skippedPathsResolver->resolve();

$this->assertCount(2, $skippedPaths);

$this->assertSame(__DIR__ . '/Fixture', $skippedPaths[0]);
$this->assertSame('*/Mask/*', $skippedPaths[1]);
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,5 @@ parameters:
paths:
- packages/Config/RectorConfig.php
- 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>#'
10 changes: 7 additions & 3 deletions src/Configuration/ConfigInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ final class ConfigInitializer
private array $rectors = [];

/**
* @param RewindableGenerator<RectorInterface> $rectors
* @param RewindableGenerator<RectorInterface>|RectorInterface[] $rectors
*/
public function __construct(
RewindableGenerator $rectors,
iterable $rectors,
private readonly InitFilePathsResolver $initFilePathsResolver,
private readonly SymfonyStyle $symfonyStyle,
private readonly PhpVersionProvider $phpVersionProvider,
) {
$this->rectors = iterator_to_array($rectors);
if ($rectors instanceof RewindableGenerator) {
$this->rectors = iterator_to_array($rectors->getIterator());
} else {
$this->rectors = $rectors;
}
}

public function createConfig(string $projectDirectory): void
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/ListRulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ListRulesCommand extends Command
public function __construct(
private readonly RectorOutputStyle $rectorOutputStyle,
private readonly SkippedClassResolver $skippedClassResolver,
RewindableGenerator $rectors
iterable $rectors
) {
parent::__construct();
$this->rectors = iterator_to_array($rectors);
Expand Down
21 changes: 21 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Application\ChangedNodeScopeRefresher;
use Rector\Core\Application\FileProcessor\PhpFileProcessor;
use Rector\Core\Configuration\ConfigInitializer;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Console\Output\OutputFormatterCollector;
use Rector\Core\Console\Output\RectorOutputStyle;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Console\Style\RectorConsoleOutputStyleFactory;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\FileSystem\FilePathHelper;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\NodeDecorator\CreatedByRuleDecorator;
Expand Down Expand Up @@ -170,6 +174,7 @@
use Rector\StaticTypeMapper\PhpParser\UnionTypeNodeMapper;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Style\SymfonyStyle;
use Webmozart\Assert\Assert;

final class LazyContainerFactory
Expand Down Expand Up @@ -403,6 +408,10 @@ public function create(): Container
->needs('$phpRectors')
->giveTagged(PhpRectorInterface::class);

$lazyRectorConfig->when(ConfigInitializer::class)
->needs('$rectors')
->giveTagged(RectorInterface::class);

$lazyRectorConfig->singleton(
RectorConsoleOutputStyle::class,
static function (Container $container): RectorConsoleOutputStyle {
Expand Down Expand Up @@ -503,6 +512,14 @@ static function (AbstractRector $rector, Container $container): void {
ClassNameImportSkipVoterInterface::class
);

$lazyRectorConfig->singleton(
SymfonyStyle::class,
static function (Container $container): SymfonyStyle {
$symfonyStyleFactory = $container->make(SymfonyStyleFactory::class);
return $symfonyStyleFactory->create();
}
);

$this->registerTagged(
$lazyRectorConfig,
self::ANNOTATION_TO_ATTRIBUTE_MAPPER_CLASSES,
Expand All @@ -513,6 +530,10 @@ static function (AbstractRector $rector, Container $container): void {
->needs('$annotationToAttributeMappers')
->giveTagged(AnnotationToAttributeMapperInterface::class);

$lazyRectorConfig->when(OutputFormatterCollector::class)
->needs('$outputFormatters')
->giveTagged(OutputFormatterInterface::class);

// #[Required]-like setter
$lazyRectorConfig->afterResolving(
ArrayAnnotationToAttributeMapper::class,
Expand Down
7 changes: 4 additions & 3 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ final class RectorNodeTraverser extends NodeTraverser
private array $phpRectors = [];

/**
* @param RewindableGenerator<PhpRectorInterface> $phpRectors
* @param RewindableGenerator<PhpRectorInterface>|PhpRectorInterface[] $phpRectors
*/
public function __construct(
RewindableGenerator $phpRectors,
iterable $phpRectors,
private readonly PhpVersionedFilter $phpVersionedFilter
) {
$this->phpRectors = iterator_to_array($phpRectors);
$this->phpRectors = is_array($phpRectors) ? $phpRectors : iterator_to_array($phpRectors);

parent::__construct();
}

Expand Down
31 changes: 0 additions & 31 deletions tests/Configuration/ValueObjectInliner/ConfigFactoryNestedTest.php

This file was deleted.

42 changes: 0 additions & 42 deletions tests/Configuration/ValueObjectInliner/ConfigFactoryTest.php

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions tests/Configuration/ValueObjectInliner/Source/SomeValueObject.php

This file was deleted.

Loading

0 comments on commit e2a6636

Please sign in to comment.