Skip to content

Commit c225fe6

Browse files
committed
[Diffs] Handle --no-diffs usage to show file have been changed if any with --dry-run + --no-diffs
1 parent dfa1141 commit c225fe6

10 files changed

Lines changed: 52 additions & 11 deletions

File tree

src/Application/ApplicationFileProcessor.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function run(Configuration $configuration, InputInterface $input): Proces
6161

6262
// no files found
6363
if ($filePaths === []) {
64-
return new ProcessResult([], []);
64+
return new ProcessResult([], [], 0);
6565
}
6666

6767
$this->missConfigurationReporter->reportVendorInPaths($filePaths);
@@ -124,6 +124,7 @@ public function processFiles(
124124
/** @var FileDiff[] $fileDiffs */
125125
$fileDiffs = [];
126126

127+
$totalChanged = 0;
127128
foreach ($filePaths as $filePath) {
128129
if ($preFileCallback !== null) {
129130
$preFileCallback($filePath);
@@ -148,6 +149,10 @@ public function processFiles(
148149
if (is_callable($postFileCallback)) {
149150
$postFileCallback(1);
150151
}
152+
153+
if ($fileProcessResult->hasChanged()) {
154+
++$totalChanged;
155+
}
151156
} catch (Throwable $throwable) {
152157
$this->changedFilesDetector->invalidateFile($filePath);
153158

@@ -159,7 +164,7 @@ public function processFiles(
159164
}
160165
}
161166

162-
return new ProcessResult($systemErrors, $fileDiffs);
167+
return new ProcessResult($systemErrors, $fileDiffs, $totalChanged);
163168
}
164169

165170
private function processFile(File $file, Configuration $configuration): FileProcessResult

src/Application/FileProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function processFile(File $file, Configuration $configuration): FileProce
100100
$file->setFileDiff($currentFileDiff);
101101
}
102102

103-
return new FileProcessResult([], $file->getFileDiff());
103+
return new FileProcessResult([], $file->getFileDiff(), $file->hasChanged());
104104
}
105105

106106
private function parseFileAndDecorateNodes(File $file): ?SystemError

src/ChangesReporting/Output/ConsoleOutputFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ private function normalizePathsToRelativeWithLine(string $errorMessage): string
144144

145145
private function createSuccessMessage(ProcessResult $processResult, Configuration $configuration): string
146146
{
147-
$changeCount = count($processResult->getFileDiffs());
147+
$changeCount = $processResult->getTotalChanged();
148148

149-
if ($changeCount === 0) {
149+
if ($changeCount === 0 && ! $processResult->hasChanged()) {
150150
return 'Rector is done!';
151151
}
152152

src/Console/Command/ProcessCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ private function resolveReturnCode(ProcessResult $processResult, Configuration $
214214
return ExitCode::CHANGED_CODE;
215215
}
216216

217+
if ($processResult->hasChanged()) {
218+
return ExitCode::CHANGED_CODE;
219+
}
220+
217221
return ExitCode::SUCCESS;
218222
}
219223

src/Console/Command/WorkerCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private function runWorker(
158158
Bridge::FILES_COUNT => count($filePaths),
159159
Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(),
160160
Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()),
161+
Bridge::HAS_CHANGED => $processResult->hasChanged(),
161162
],
162163
]);
163164
});

src/Parallel/Application/ParallelFileProcessor.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function process(
119119

120120
$systemErrorsCount = 0;
121121
$reachedSystemErrorsCountLimit = false;
122+
$totalChanged = 0;
122123

123124
$handleErrorCallable = function (Throwable $throwable) use (
124125
&$systemErrors,
@@ -158,7 +159,8 @@ public function process(
158159
$timeoutInSeconds,
159160
$handleErrorCallable,
160161
&$fileChunksBudgetPerProcess,
161-
&$processSpawner
162+
&$processSpawner,
163+
&$totalChanged
162164
): void {
163165
$processIdentifier = Random::generate();
164166
$workerCommandLine = $this->workerCommandLineFactory->create(
@@ -185,8 +187,14 @@ function (array $json) use (
185187
&$reachedInternalErrorsCountLimit,
186188
$processIdentifier,
187189
&$fileChunksBudgetPerProcess,
188-
&$processSpawner
190+
&$processSpawner,
191+
&$totalChanged
189192
): void {
193+
194+
if ($json[Bridge::HAS_CHANGED]) {
195+
++$totalChanged;
196+
}
197+
190198
// decode arrays to objects
191199
foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) {
192200
if (is_string($jsonError)) {
@@ -269,6 +277,6 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
269277
));
270278
}
271279

272-
return new ProcessResult($systemErrors, $fileDiffs);
280+
return new ProcessResult($systemErrors, $fileDiffs, $totalChanged);
273281
}
274282
}

src/Parallel/ValueObject/Bridge.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ final class Bridge
3333
* @var string
3434
*/
3535
public const FILES_COUNT = 'files_count';
36+
37+
/**
38+
* @var string
39+
*/
40+
public const HAS_CHANGED = 'has_changed';
3641
}

src/ValueObject/FileProcessResult.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
public function __construct(
1717
private array $systemErrors,
18-
private ?FileDiff $fileDiff
18+
private ?FileDiff $fileDiff,
19+
private bool $hasChanged
1920
) {
2021
Assert::allIsInstanceOf($systemErrors, SystemError::class);
2122
}
@@ -32,4 +33,9 @@ public function getFileDiff(): ?FileDiff
3233
{
3334
return $this->fileDiff;
3435
}
36+
37+
public function hasChanged(): bool
38+
{
39+
return $this->hasChanged;
40+
}
3541
}

src/ValueObject/ProcessResult.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class ProcessResult
1717
public function __construct(
1818
private array $systemErrors,
1919
private readonly array $fileDiffs,
20+
private readonly int $totalChanged
2021
) {
2122
Assert::allIsInstanceOf($systemErrors, SystemError::class);
2223
Assert::allIsInstanceOf($fileDiffs, FileDiff::class);
@@ -51,4 +52,14 @@ public function addSystemErrors(array $systemErrors): void
5152

5253
$this->systemErrors = [...$this->systemErrors, ...$systemErrors];
5354
}
55+
56+
public function hasChanged(): bool
57+
{
58+
return $this->totalChanged > 0;
59+
}
60+
61+
public function getTotalChanged(): int
62+
{
63+
return $this->totalChanged;
64+
}
5465
}

tests/ChangesReporting/Output/GitHubOutputFormatterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function testReportShouldOutputErrorMessagesGrouped(): void
5353
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
5454
),
5555
],
56+
1
5657
),
5758
new Configuration()
5859
);
@@ -62,7 +63,7 @@ public function testReportShouldOutputErrorMessagesGroupedWithNoErrors(): void
6263
{
6364
$this->expectOsOutputString('::group::Rector report' . PHP_EOL . '::endgroup::' . PHP_EOL);
6465

65-
$this->gitHubOutputFormatter->report(new ProcessResult([], []), new Configuration());
66+
$this->gitHubOutputFormatter->report(new ProcessResult([], [], 1), new Configuration());
6667
}
6768

6869
public function testReportShouldOutputErrorMessagesGroupedWithMultipleDiffs(): void
@@ -95,7 +96,7 @@ public function testReportShouldOutputErrorMessagesGroupedWithMultipleDiffs(): v
9596
'diff console formatted',
9697
[new RectorWithLineChange(StrStartsWithRector::class, 54)]
9798
),
98-
], ),
99+
], 2),
99100
new Configuration()
100101
);
101102
}

0 commit comments

Comments
 (0)