Skip to content

Commit 54f866d

Browse files
committed
[Differ] Remove ConsoleDiffer, use DefaultDiffer that utilize ColorConsoleDiffFormatter instead to avoid double diffing process
fix cs
1 parent ead2e71 commit 54f866d

5 files changed

Lines changed: 29 additions & 36 deletions

File tree

e2e/e2eTestRunner.php

Lines changed: 4 additions & 2 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

@@ -62,8 +63,9 @@
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($consoleDiffer->diff($output, $expectedOutput));
6769
$symfonyStyle->writeln($diff);
6870

6971
exit(Command::FAILURE);

e2e/e2eTestRunnerWithCache.php

Lines changed: 4 additions & 2 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

@@ -40,8 +41,9 @@
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($consoleDiffer->diff($output, $expectedOutput));
4547
$symfonyStyle->writeln($diff);
4648

4749
exit(Command::FAILURE);

src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php

Lines changed: 6 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,13 @@ public function createFileDiffWithLineChanges(
3232
): FileDiff {
3333
$relativeFilePath = $this->filePathHelper->relativePath($file->getFilePath());
3434

35+
$diff = $this->defaultDiffer->diff($oldContent, $newContent);
36+
3537
// always keep the most recent diff
3638
return new FileDiff(
3739
$relativeFilePath,
38-
$shouldShowDiffs ? $this->defaultDiffer->diff($oldContent, $newContent) : '',
39-
$shouldShowDiffs ? $this->consoleDiffer->diff($oldContent, $newContent) : '',
40+
$shouldShowDiffs ? $diff : '',
41+
$shouldShowDiffs ? $this->colorConsoleDiffFormatter->format($diff) : '',
4042
$rectorsWithLineChanges
4143
);
4244
}

src/Console/Formatter/ColorConsoleDiffFormatter.php

Lines changed: 15 additions & 0 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/1
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()
@@ -71,6 +77,7 @@ private function formatWithTemplate(string $diff, string $template): string
7177
$string = $this->makePlusLinesGreen($string);
7278
$string = $this->makeMinusLinesRed($string);
7379
$string = $this->makeAtNoteCyan($string);
80+
$string = $this->normalizeLineAtDiff($string);
7481

7582
if ($string === ' ') {
7683
return '';
@@ -82,6 +89,14 @@ private function formatWithTemplate(string $diff, string $template): string
8289
return sprintf($template, implode(PHP_EOL, $coloredLines));
8390
}
8491

92+
/**
93+
* Remove number diff, eg; @@ -67,6 +67,8 @@ to become @@ @@
94+
*/
95+
private function normalizeLineAtDiff(string $line): string
96+
{
97+
return Strings::replace($line, self::AT_DIFF_LINE_REGEX, '@@ @@');
98+
}
99+
85100
private function makePlusLinesGreen(string $string): string
86101
{
87102
return Strings::replace($string, self::PLUS_START_REGEX, '<fg=green>$1</fg=green>');

src/Console/Formatter/ConsoleDiffer.php

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

0 commit comments

Comments
 (0)