Skip to content

Commit de69d05

Browse files
Add --rules-summary option to display applied rules summary with count (#7874)
1 parent f2be385 commit de69d05

File tree

14 files changed

+197
-0
lines changed

14 files changed

+197
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--rules-summary
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"require": {
3+
"php": "^8.1"
4+
},
5+
"minimum-stability": "dev",
6+
"prefer-stable": true
7+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
3 files with changes
2+
====================
3+
4+
1) src/AlwaysTrue.php:4
5+
6+
---------- begin diff ----------
7+
@@ @@
8+
{
9+
public function run()
10+
{
11+
- if (1 === 1) {
12+
- }
13+
-
14+
- if (2 === 2) {
15+
- }
16+
-
17+
return 'no';
18+
}
19+
}
20+
----------- end diff -----------
21+
22+
Applied rules:
23+
* RemoveAlwaysTrueIfConditionRector
24+
25+
26+
2) src/AnotherAlwaysTrue.php:4
27+
28+
---------- begin diff ----------
29+
@@ @@
30+
{
31+
public function run()
32+
{
33+
- if (3 === 3) {
34+
- }
35+
-
36+
return 'yes';
37+
}
38+
}
39+
----------- end diff -----------
40+
41+
Applied rules:
42+
* RemoveAlwaysTrueIfConditionRector
43+
44+
45+
3) src/DeadConstructor.php:2
46+
47+
---------- begin diff ----------
48+
@@ @@
49+
50+
final class DeadConstructor
51+
{
52+
- public function __construct()
53+
- {
54+
- }
55+
}
56+
----------- end diff -----------
57+
58+
Applied rules:
59+
* RemoveEmptyClassMethodRector
60+
61+
62+
Rules Summary
63+
-------------
64+
65+
* RemoveAlwaysTrueIfConditionRector would have been applied 2 times
66+
* RemoveEmptyClassMethodRector would have been applied 1 time
67+
68+
[OK] 3 files would have been changed (dry-run) by Rector
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
7+
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->paths([
11+
__DIR__ . '/src',
12+
]);
13+
14+
$rectorConfig->rule(RemoveEmptyClassMethodRector::class);
15+
$rectorConfig->rule(RemoveAlwaysTrueIfConditionRector::class);
16+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
final class AlwaysTrue
4+
{
5+
public function run()
6+
{
7+
if (1 === 1) {
8+
}
9+
10+
if (2 === 2) {
11+
}
12+
13+
return 'no';
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
final class AnotherAlwaysTrue
4+
{
5+
public function run()
6+
{
7+
if (3 === 3) {
8+
}
9+
10+
return 'yes';
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
final class DeadConstructor
4+
{
5+
public function __construct()
6+
{
7+
}
8+
}

src/ChangesReporting/Output/ConsoleOutputFormatter.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function report(ProcessResult $processResult, Configuration $configuratio
4646
$this->symfonyStyle->newLine();
4747
}
4848

49+
if ($configuration->shouldShowRulesSummary()) {
50+
$this->reportRulesSummary($processResult, $configuration);
51+
}
52+
4953
$message = $this->createSuccessMessage($processResult, $configuration);
5054
$this->symfonyStyle->success($message);
5155
}
@@ -131,6 +135,31 @@ private function reportErrors(array $errors, bool $absoluteFilePath): void
131135
}
132136
}
133137

138+
private function reportRulesSummary(ProcessResult $processResult, Configuration $configuration): void
139+
{
140+
$ruleApplicationCounts = $processResult->getRuleApplicationCounts();
141+
if ($ruleApplicationCounts === []) {
142+
return;
143+
}
144+
145+
$verb = $configuration->isDryRun() ? 'would have been applied' : 'was applied';
146+
147+
$this->symfonyStyle->section('Rules Summary');
148+
149+
foreach ($ruleApplicationCounts as $ruleClass => $count) {
150+
$ruleShortClass = (string) Strings::after($ruleClass, '\\', -1);
151+
$this->symfonyStyle->writeln(sprintf(
152+
' * <info>%s</info> %s <comment>%d</comment> time%s',
153+
$ruleShortClass,
154+
$verb,
155+
$count,
156+
$count > 1 ? 's' : ''
157+
));
158+
}
159+
160+
$this->symfonyStyle->newLine();
161+
}
162+
134163
private function normalizePathsToRelativeWithLine(string $errorMessage): string
135164
{
136165
$regex = '#' . preg_quote(getcwd(), '#') . '/#';

src/Configuration/ConfigurationFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function createFromInput(InputInterface $input): Configuration
8787

8888
$levelOverflows = SimpleParameterProvider::provideArrayParameter(Option::LEVEL_OVERFLOWS);
8989

90+
$showRulesSummary = (bool) $input->getOption(Option::RULES_SUMMARY);
91+
9092
return new Configuration(
9193
$isDryRun,
9294
$showProgressBar,
@@ -104,6 +106,7 @@ public function createFromInput(InputInterface $input): Configuration
104106
$onlyRule,
105107
$onlySuffix,
106108
$levelOverflows,
109+
$showRulesSummary,
107110
);
108111
}
109112

0 commit comments

Comments
 (0)