Skip to content

Commit e120e4c

Browse files
Add withImport() method to RectorConfigBuilder
- Add withImport() method to import configuration files - Import files are processed in __invoke() before other configuration - Add unit tests with single, multiple, and nested import scenarios - Fixture configs use RectorConfigBuilder pattern
1 parent 26f7bc1 commit e120e4c

5 files changed

Lines changed: 94 additions & 0 deletions

File tree

src/Configuration/RectorConfigBuilder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
/**
4949
* @api
50+
* @see \Rector\Tests\Configuration\RectorConfigBuilderTest
5051
*/
5152
final class RectorConfigBuilder
5253
{
@@ -65,6 +66,11 @@ final class RectorConfigBuilder
6566
*/
6667
private array $sets = [];
6768

69+
/**
70+
* @var string[]
71+
*/
72+
private array $imports = [];
73+
6874
/**
6975
* @var array<mixed>
7076
*/
@@ -195,6 +201,10 @@ final class RectorConfigBuilder
195201

196202
public function __invoke(RectorConfig $rectorConfig): void
197203
{
204+
foreach ($this->imports as $import) {
205+
$rectorConfig->import($import);
206+
}
207+
198208
if ($this->setGroups !== [] || $this->setProviders !== []) {
199209
$setProviderCollector = new SetProviderCollector(array_map(
200210
$rectorConfig->make(...),
@@ -407,6 +417,16 @@ public function withPaths(array $paths): self
407417
return $this;
408418
}
409419

420+
/**
421+
* @param string ...$files file paths to import
422+
*/
423+
public function withImport(string ...$files): self
424+
{
425+
$this->imports = array_merge($this->imports, $files);
426+
427+
return $this;
428+
}
429+
410430
/**
411431
* @param array<mixed> $skip
412432
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Configuration;
6+
7+
use PHPUnit\Framework\Attributes\RunClassInSeparateProcess;
8+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
9+
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
10+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
11+
12+
#[RunClassInSeparateProcess]
13+
final class RectorConfigBuilderTest extends AbstractLazyTestCase
14+
{
15+
public function testWithImport(): void
16+
{
17+
$rectorConfig = self::getContainer();
18+
19+
$rectorConfig->configure()
20+
->withImport(__DIR__ . '/config/imported_config.php')($rectorConfig);
21+
22+
$this->assertTrue($rectorConfig->has(ReturnTypeFromReturnNewRector::class));
23+
}
24+
25+
public function testWithMultipleImports(): void
26+
{
27+
$rectorConfig = self::getContainer();
28+
29+
$rectorConfig->configure()
30+
->withImport(
31+
__DIR__ . '/config/imported_config.php',
32+
__DIR__ . '/config/second_imported_config.php'
33+
)($rectorConfig);
34+
35+
$this->assertTrue($rectorConfig->has(ReturnTypeFromReturnNewRector::class));
36+
$this->assertTrue($rectorConfig->has(RemoveUnusedPrivateMethodRector::class));
37+
}
38+
39+
public function testWithNestedImport(): void
40+
{
41+
$rectorConfig = self::getContainer();
42+
43+
$rectorConfig->configure()
44+
->withImport(__DIR__ . '/config/nested_import_config.php')($rectorConfig);
45+
46+
$this->assertTrue($rectorConfig->has(ReturnTypeFromReturnNewRector::class));
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([ReturnTypeFromReturnNewRector::class]);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withImport(__DIR__ . '/imported_config.php');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([RemoveUnusedPrivateMethodRector::class]);

0 commit comments

Comments
 (0)