Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

/**
* @api
* @see \Rector\Tests\Configuration\RectorConfigBuilderTest
*/
final class RectorConfigBuilder
{
Expand All @@ -65,6 +66,11 @@ final class RectorConfigBuilder
*/
private array $sets = [];

/**
* @var string[]
*/
private array $imports = [];

/**
* @var array<mixed>
*/
Expand Down Expand Up @@ -195,6 +201,10 @@ final class RectorConfigBuilder

public function __invoke(RectorConfig $rectorConfig): void
{
foreach ($this->imports as $import) {
$rectorConfig->import($import);
}

if ($this->setGroups !== [] || $this->setProviders !== []) {
$setProviderCollector = new SetProviderCollector(array_map(
$rectorConfig->make(...),
Expand Down Expand Up @@ -407,6 +417,16 @@ public function withPaths(array $paths): self
return $this;
}

/**
* @param string ...$files file paths to import
*/
public function withImport(string ...$files): self
{
$this->imports = array_merge($this->imports, $files);

return $this;
}

/**
* @param array<mixed> $skip
*/
Expand Down
48 changes: 48 additions & 0 deletions tests/Configuration/RectorConfigBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Configuration;

use PHPUnit\Framework\Attributes\RunClassInSeparateProcess;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;

#[RunClassInSeparateProcess]
final class RectorConfigBuilderTest extends AbstractLazyTestCase
{
public function testWithImport(): void
{
$rectorConfig = self::getContainer();

$rectorConfig->configure()
->withImport(__DIR__ . '/config/imported_config.php')($rectorConfig);

$this->assertTrue($rectorConfig->has(ReturnTypeFromReturnNewRector::class));
}

public function testWithMultipleImports(): void
{
$rectorConfig = self::getContainer();

$rectorConfig->configure()
->withImport(
__DIR__ . '/config/imported_config.php',
__DIR__ . '/config/second_imported_config.php'
)($rectorConfig);

$this->assertTrue($rectorConfig->has(ReturnTypeFromReturnNewRector::class));
$this->assertTrue($rectorConfig->has(RemoveUnusedPrivateMethodRector::class));
}

public function testWithNestedImport(): void
{
$rectorConfig = self::getContainer();

$rectorConfig->configure()
->withImport(__DIR__ . '/config/nested_import_config.php')($rectorConfig);

$this->assertTrue($rectorConfig->has(ReturnTypeFromReturnNewRector::class));
}
}
9 changes: 9 additions & 0 deletions tests/Configuration/config/imported_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;

return RectorConfig::configure()
->withRules([ReturnTypeFromReturnNewRector::class]);
8 changes: 8 additions & 0 deletions tests/Configuration/config/nested_import_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withImport(__DIR__ . '/imported_config.php');
9 changes: 9 additions & 0 deletions tests/Configuration/config/second_imported_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;

return RectorConfig::configure()
->withRules([RemoveUnusedPrivateMethodRector::class]);
Loading