Skip to content

Commit 1a19a34

Browse files
authored
[Diffs] Handle --no-diffs usage to show file have been changed if any with --dry-run + --no-diffs (#7619)
* [Diffs] Handle --no-diffs usage to show file have been changed if any with --dry-run + --no-diffs * fix phpstan * Fix total changed * clean up * Fix phpstan * Add e2e test for show files changed without diff * Add e2e test for show files changed without diff * eol
1 parent 732ca2a commit 1a19a34

File tree

18 files changed

+149
-11
lines changed

18 files changed

+149
-11
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This workflow runs system tests: Use the Rector application from the source
2+
# checkout to process "fixture" projects in e2e/ directory
3+
# to see if those can be processed successfully
4+
name: End to End tests with no diffs
5+
6+
on:
7+
pull_request:
8+
branches:
9+
- main
10+
push:
11+
branches:
12+
- main
13+
14+
env:
15+
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361
16+
COMPOSER_ROOT_VERSION: "dev-main"
17+
18+
jobs:
19+
end_to_end:
20+
runs-on: ubuntu-latest
21+
timeout-minutes: 3
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
php_version: ['8.2']
26+
directory:
27+
- 'e2e/applied-rule-removed-node-no-diffs'
28+
29+
name: End to end test - ${{ matrix.directory }}
30+
31+
steps:
32+
- uses: actions/checkout@v4
33+
34+
- uses: shivammathur/setup-php@v2
35+
with:
36+
php-version: ${{ matrix.php_version }}
37+
coverage: none
38+
39+
# run in root rector-src
40+
- run: composer install --ansi
41+
42+
# run in e2e subdir
43+
-
44+
run: composer install --ansi
45+
working-directory: ${{ matrix.directory }}
46+
47+
# run e2e test
48+
- run: php ../e2eTestRunner.php --no-diffs
49+
working-directory: ${{ matrix.directory }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[OK] 2 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
final class AlwaysTrue
4+
{
5+
public function run()
6+
{
7+
if (1 === 1) {
8+
}
9+
10+
return 'no';
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+
}

e2e/e2eTestRunner.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
$e2eCommand .= ' -a ' . $argv[2];
3333
}
3434

35+
if (isset($argv[1]) && $argv[1] === '--no-diffs') {
36+
$e2eCommand .= ' --no-diffs';
37+
}
38+
3539
$cliOptions = 'cli-options.txt';
3640
if (file_exists($cliOptions)) {
3741
$e2eCommand .= ' ' . trim((string) file_get_contents($cliOptions));

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function processFile(File $file, Configuration $configuration): FileProce
4848
$parsingSystemError = $this->parseFileAndDecorateNodes($file);
4949
if ($parsingSystemError instanceof SystemError) {
5050
// we cannot process this file as the parsing and type resolving itself went wrong
51-
return new FileProcessResult([$parsingSystemError], null);
51+
return new FileProcessResult([$parsingSystemError], null, false);
5252
}
5353

5454
$fileHasChanged = false;
@@ -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

0 commit comments

Comments
 (0)