Skip to content

Commit 2138615

Browse files
committed
[DX] Make configurable include post rector classes in result
1 parent 1e75414 commit 2138615

9 files changed

Lines changed: 49 additions & 6 deletions

File tree

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@
4646
$rectorConfig->reportingRealPath(false);
4747

4848
$rectorConfig->treatClassesAsFinal(false);
49+
50+
$rectorConfig->includePostRectorsInReports(false);
4951
};

e2e/applied-auto-import/expected-output.diff

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
Applied rules:
2525
* RenameClassRector
26+
* DocblockNameImportingPostRector
27+
* NameImportingPostRector
28+
* UnusedImportRemovingPostRector
29+
* UseAddingPostRector
2630

2731

2832
[OK] 1 file would have been changed (dry-run) by Rector

e2e/applied-auto-import/rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
$rectorConfig->importNames();
1818
$rectorConfig->removeUnusedImports();
19+
$rectorConfig->includePostRectorsInReports(true);
1920
};

src/Config/RectorConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,9 @@ public function setOverflowLevels(array $levelOverflows): void
451451
{
452452
SimpleParameterProvider::addParameter(Option::LEVEL_OVERFLOWS, $levelOverflows);
453453
}
454+
455+
public function includePostRectorsInReports(bool $includePostRectorsInReports = true): void
456+
{
457+
SimpleParameterProvider::setParameter(Option::INCLUDE_POST_RECTORS_IN_REPORTS, $includePostRectorsInReports);
458+
}
454459
}

src/Configuration/Option.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,9 @@ final class Option
327327
* @var string
328328
*/
329329
public const KAIZEN = 'kaizen';
330+
331+
/**
332+
* @var string
333+
*/
334+
public const INCLUDE_POST_RECTORS_IN_REPORTS = 'include_post_rectors_in_reports';
330335
}

src/Configuration/RectorConfigBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ final class RectorConfigBuilder
177177

178178
private ?bool $isWithPhpLevelUsed = null;
179179

180+
private ?bool $includePostRectorsInReports = null;
181+
180182
/**
181183
* @var array<class-string<SetProviderInterface>,bool>
182184
*/
@@ -390,6 +392,10 @@ public function __invoke(RectorConfig $rectorConfig): void
390392
if ($this->levelOverflows !== []) {
391393
$rectorConfig->setOverflowLevels($this->levelOverflows);
392394
}
395+
396+
if ($this->includePostRectorsInReports !== null) {
397+
$rectorConfig->includePostRectorsInReports($this->includePostRectorsInReports);
398+
}
393399
}
394400

395401
/**
@@ -1291,4 +1297,10 @@ public function withSetProviders(string ...$setProviders): self
12911297

12921298
return $this;
12931299
}
1300+
1301+
public function withIncludePostRectorsInReports(bool $includePostRectorsInReports = true): self
1302+
{
1303+
$this->includePostRectorsInReports = $includePostRectorsInReports;
1304+
return $this;
1305+
}
12941306
}

src/Testing/PHPUnit/ValueObject/RectorTestResult.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Testing\PHPUnit\ValueObject;
66

77
use Rector\Contract\Rector\RectorInterface;
8+
use Rector\PostRector\Contract\Rector\PostRectorInterface;
89
use Rector\Util\RectorClassesSorter;
910
use Rector\ValueObject\ProcessResult;
1011

@@ -25,7 +26,7 @@ public function getChangedContents(): string
2526
}
2627

2728
/**
28-
* @return array<class-string<RectorInterface>>
29+
* @return array<class-string<RectorInterface|PostRectorInterface>>
2930
*/
3031
public function getAppliedRectorClasses(): array
3132
{
@@ -35,6 +36,6 @@ public function getAppliedRectorClasses(): array
3536
$rectorClasses = array_merge($rectorClasses, $fileDiff->getRectorClasses());
3637
}
3738

38-
return RectorClassesSorter::sortAndFilterOutPostRectors($rectorClasses);
39+
return RectorClassesSorter::sort($rectorClasses);
3940
}
4041
}

src/Util/RectorClassesSorter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
namespace Rector\Util;
66

7+
use Rector\Configuration\Option;
8+
use Rector\Configuration\Parameter\SimpleParameterProvider;
79
use Rector\Contract\Rector\RectorInterface;
810
use Rector\PostRector\Contract\Rector\PostRectorInterface;
911

1012
final class RectorClassesSorter
1113
{
1214
/**
1315
* @param array<class-string<RectorInterface|PostRectorInterface>> $rectorClasses
14-
* @return array<class-string<RectorInterface>>
16+
* @return array<class-string<RectorInterface|PostRectorInterface>>
1517
*/
16-
public static function sortAndFilterOutPostRectors(array $rectorClasses): array
18+
public static function sort(array $rectorClasses): array
1719
{
1820
$rectorClasses = array_unique($rectorClasses);
1921

@@ -23,6 +25,16 @@ public static function sortAndFilterOutPostRectors(array $rectorClasses): array
2325
);
2426
sort($mainRectorClasses);
2527

28+
if (SimpleParameterProvider::provideBoolParameter(Option::INCLUDE_POST_RECTORS_IN_REPORTS)) {
29+
$postRectorClasses = array_filter(
30+
$rectorClasses,
31+
fn (string $rectorClass): bool => is_a($rectorClass, PostRectorInterface::class, true)
32+
);
33+
sort($postRectorClasses);
34+
35+
return array_merge($mainRectorClasses, $postRectorClasses);
36+
}
37+
2638
return $mainRectorClasses;
2739
}
2840
}

src/ValueObject/Reporting/FileDiff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
99
use Rector\Contract\Rector\RectorInterface;
1010
use Rector\Parallel\ValueObject\BridgeItem;
11+
use Rector\PostRector\Contract\Rector\PostRectorInterface;
1112
use Rector\Util\RectorClassesSorter;
1213
use Symplify\EasyParallel\Contract\SerializableInterface;
1314
use Webmozart\Assert\Assert;
@@ -82,7 +83,7 @@ public function getRectorShortClasses(): array
8283
}
8384

8485
/**
85-
* @return array<class-string<RectorInterface>>
86+
* @return array<class-string<RectorInterface|PostRectorInterface>>
8687
*/
8788
public function getRectorClasses(): array
8889
{
@@ -92,7 +93,7 @@ public function getRectorClasses(): array
9293
$rectorClasses[] = $rectorWithLineChange->getRectorClass();
9394
}
9495

95-
return RectorClassesSorter::sortAndFilterOutPostRectors($rectorClasses);
96+
return RectorClassesSorter::sort($rectorClasses);
9697
}
9798

9899
public function getFirstLineNumber(): ?int

0 commit comments

Comments
 (0)