Skip to content

Commit 20db305

Browse files
authored
[Differ] Remove ConsoleDiffer, use DefaultDiffer that utilize ColorConsoleDiffFormatter instead to avoid double diffing process (#7309)
* [Differ] Remove ConsoleDiffer, use DefaultDiffer that utilize ColorConsoleDiffFormatter instead to avoid double diffing process fix cs * Fix e2e * final touch: register e2e test runner script to phpstan so it detected by phpstan if any issue * Final touch: fix phpstan * Final touch: clean up regex * final touch: clean up logic * final touch: fix variable * final touch: keep fg=cyan color * final touch: keep variable name for consistency * Final touch: stop loop when --- Original and +++ New already removed * reduce loop
1 parent ff296fa commit 20db305

File tree

6 files changed

+50
-51
lines changed

6 files changed

+50
-51
lines changed

e2e/e2eTestRunner.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\Console\Formatter\ColorConsoleDiffFormatter;
88
use Rector\Console\Formatter\ConsoleDiffer;
99
use Rector\Console\Style\SymfonyStyleFactory;
10+
use Rector\Differ\DefaultDiffer;
1011
use Rector\Util\Reflection\PrivatesAccessor;
1112
use Symfony\Component\Console\Command\Command;
1213

@@ -37,7 +38,7 @@
3738

3839
$cliOptions = 'cli-options.txt';
3940
if (file_exists($cliOptions)) {
40-
$e2eCommand .= ' ' . trim(file_get_contents($cliOptions));
41+
$e2eCommand .= ' ' . trim((string) file_get_contents($cliOptions));
4142
}
4243

4344

@@ -55,15 +56,16 @@
5556
$symfonyStyle = $symfonyStyleFactory->create();
5657

5758
$matchedExpectedOutput = false;
58-
$expectedOutput = trim(file_get_contents($expectedDiff));
59+
$expectedOutput = trim((string) file_get_contents($expectedDiff));
5960
if ($output === $expectedOutput) {
6061
$symfonyStyle->success('End-to-end test successfully completed');
6162
exit(Command::SUCCESS);
6263
}
6364

6465
// print color diff, to make easy find the differences
65-
$consoleDiffer = new ConsoleDiffer(new ColorConsoleDiffFormatter());
66-
$diff = $consoleDiffer->diff($output, $expectedOutput);
66+
$defaultDiffer = new DefaultDiffer();
67+
$colorConsoleDiffFormatter = new ColorConsoleDiffFormatter();
68+
$diff = $colorConsoleDiffFormatter->format($defaultDiffer->diff($output, $expectedOutput));
6769
$symfonyStyle->writeln($diff);
6870

6971
exit(Command::FAILURE);

e2e/e2eTestRunnerWithCache.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\Console\Formatter\ColorConsoleDiffFormatter;
88
use Rector\Console\Formatter\ConsoleDiffer;
99
use Rector\Console\Style\SymfonyStyleFactory;
10+
use Rector\Differ\DefaultDiffer;
1011
use Rector\Util\Reflection\PrivatesAccessor;
1112
use Symfony\Component\Console\Command\Command;
1213

@@ -33,15 +34,16 @@
3334
$symfonyStyle = $symfonyStyleFactory->create();
3435

3536
$matchedExpectedOutput = false;
36-
$expectedOutput = trim(file_get_contents($expectedDiff));
37+
$expectedOutput = trim((string) file_get_contents($expectedDiff));
3738
if ($output === $expectedOutput) {
3839
$symfonyStyle->success('End-to-end test successfully completed');
3940
exit(Command::SUCCESS);
4041
}
4142

4243
// print color diff, to make easy find the differences
43-
$consoleDiffer = new ConsoleDiffer(new ColorConsoleDiffFormatter());
44-
$diff = $consoleDiffer->diff($output, $expectedOutput);
44+
$defaultDiffer = new DefaultDiffer();
45+
$colorConsoleDiffFormatter = new ColorConsoleDiffFormatter();
46+
$diff = $colorConsoleDiffFormatter->format($defaultDiffer->diff($output, $expectedOutput));
4547
$symfonyStyle->writeln($diff);
4648

4749
exit(Command::FAILURE);

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ parameters:
2626
- tests
2727
- rules-tests
2828
- utils
29+
- e2e/e2eTestRunnerWithCache.php
30+
- e2e/e2eTestRunner.php
2931

3032
scanDirectories:
3133
- stubs

src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Rector\ChangesReporting\ValueObjectFactory;
66

77
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
8-
use Rector\Console\Formatter\ConsoleDiffer;
8+
use Rector\Console\Formatter\ColorConsoleDiffFormatter;
99
use Rector\Differ\DefaultDiffer;
1010
use Rector\FileSystem\FilePathHelper;
1111
use Rector\ValueObject\Application\File;
@@ -15,8 +15,8 @@
1515
{
1616
public function __construct(
1717
private DefaultDiffer $defaultDiffer,
18-
private ConsoleDiffer $consoleDiffer,
1918
private FilePathHelper $filePathHelper,
19+
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter
2020
) {
2121
}
2222

@@ -32,11 +32,14 @@ public function createFileDiffWithLineChanges(
3232
): FileDiff {
3333
$relativeFilePath = $this->filePathHelper->relativePath($file->getFilePath());
3434

35+
$diff = $shouldShowDiffs ? $this->defaultDiffer->diff($oldContent, $newContent) : '';
36+
$consoleDiff = $shouldShowDiffs ? $this->colorConsoleDiffFormatter->format($diff) : '';
37+
3538
// always keep the most recent diff
3639
return new FileDiff(
3740
$relativeFilePath,
38-
$shouldShowDiffs ? $this->defaultDiffer->diff($oldContent, $newContent) : '',
39-
$shouldShowDiffs ? $this->consoleDiffer->diff($oldContent, $newContent) : '',
41+
$diff,
42+
$consoleDiff,
4043
$rectorsWithLineChanges
4144
);
4245
}

src/Console/Formatter/ColorConsoleDiffFormatter.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
*/
3535
private const AT_START_REGEX = '#^(@.*)#';
3636

37+
/**
38+
* @var string
39+
* @see https://regex101.com/r/8MXnfa/2
40+
*/
41+
private const AT_DIFF_LINE_REGEX = '#^\<fg=cyan\>@@ \-\d+,\d+ \+\d+,\d+ @@\<\/fg=cyan\>$#';
42+
3743
private string $template;
3844

3945
public function __construct()
@@ -56,30 +62,42 @@ private function formatWithTemplate(string $diff, string $template): string
5662

5763
$escapedDiffLines = NewLineSplitter::split($escapedDiff);
5864

59-
// remove description of added + remove; obvious on diffs
65+
// remove description of added + remove, obvious on diffs
66+
// decorize lines
6067
foreach ($escapedDiffLines as $key => $escapedDiffLine) {
6168
if ($escapedDiffLine === '--- Original') {
6269
unset($escapedDiffLines[$key]);
70+
continue;
6371
}
6472

6573
if ($escapedDiffLine === '+++ New') {
6674
unset($escapedDiffLines[$key]);
75+
continue;
6776
}
68-
}
69-
70-
$coloredLines = array_map(function (string $string): string {
71-
$string = $this->makePlusLinesGreen($string);
72-
$string = $this->makeMinusLinesRed($string);
73-
$string = $this->makeAtNoteCyan($string);
7477

75-
if ($string === ' ') {
76-
return '';
78+
if ($escapedDiffLine === ' ') {
79+
$escapedDiffLines[$key] = '';
80+
continue;
7781
}
7882

79-
return $string;
80-
}, $escapedDiffLines);
83+
$escapedDiffLine = $this->makePlusLinesGreen($escapedDiffLine);
84+
$escapedDiffLine = $this->makeMinusLinesRed($escapedDiffLine);
85+
$escapedDiffLine = $this->makeAtNoteCyan($escapedDiffLine);
86+
$escapedDiffLine = $this->normalizeLineAtDiff($escapedDiffLine);
8187

82-
return sprintf($template, implode(PHP_EOL, $coloredLines));
88+
// final decorized line
89+
$escapedDiffLines[$key] = $escapedDiffLine;
90+
}
91+
92+
return sprintf($template, implode(PHP_EOL, $escapedDiffLines));
93+
}
94+
95+
/**
96+
* Remove number diff, eg; @@ -67,6 +67,8 @@ to become @@ @@
97+
*/
98+
private function normalizeLineAtDiff(string $string): string
99+
{
100+
return Strings::replace($string, self::AT_DIFF_LINE_REGEX, '<fg=cyan>@@ @@</fg=cyan>');
83101
}
84102

85103
private function makePlusLinesGreen(string $string): string

src/Console/Formatter/ConsoleDiffer.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)