Skip to content
Merged
10 changes: 6 additions & 4 deletions e2e/e2eTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Console\Formatter\ColorConsoleDiffFormatter;
use Rector\Console\Formatter\ConsoleDiffer;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\Differ\DefaultDiffer;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Command\Command;

Expand Down Expand Up @@ -37,7 +38,7 @@

$cliOptions = 'cli-options.txt';
if (file_exists($cliOptions)) {
$e2eCommand .= ' ' . trim(file_get_contents($cliOptions));
$e2eCommand .= ' ' . trim((string) file_get_contents($cliOptions));
}


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

$matchedExpectedOutput = false;
$expectedOutput = trim(file_get_contents($expectedDiff));
$expectedOutput = trim((string) file_get_contents($expectedDiff));
if ($output === $expectedOutput) {
$symfonyStyle->success('End-to-end test successfully completed');
exit(Command::SUCCESS);
}

// print color diff, to make easy find the differences
$consoleDiffer = new ConsoleDiffer(new ColorConsoleDiffFormatter());
$diff = $consoleDiffer->diff($output, $expectedOutput);
$defaultDiffer = new DefaultDiffer();
$colorConsoleDiffFormatter = new ColorConsoleDiffFormatter();
$diff = $colorConsoleDiffFormatter->format($defaultDiffer->diff($output, $expectedOutput));
$symfonyStyle->writeln($diff);

exit(Command::FAILURE);
8 changes: 5 additions & 3 deletions e2e/e2eTestRunnerWithCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Console\Formatter\ColorConsoleDiffFormatter;
use Rector\Console\Formatter\ConsoleDiffer;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\Differ\DefaultDiffer;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Command\Command;

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

$matchedExpectedOutput = false;
$expectedOutput = trim(file_get_contents($expectedDiff));
$expectedOutput = trim((string) file_get_contents($expectedDiff));
if ($output === $expectedOutput) {
$symfonyStyle->success('End-to-end test successfully completed');
exit(Command::SUCCESS);
}

// print color diff, to make easy find the differences
$consoleDiffer = new ConsoleDiffer(new ColorConsoleDiffFormatter());
$diff = $consoleDiffer->diff($output, $expectedOutput);
$defaultDiffer = new DefaultDiffer();
$colorConsoleDiffFormatter = new ColorConsoleDiffFormatter();
$diff = $colorConsoleDiffFormatter->format($defaultDiffer->diff($output, $expectedOutput));
$symfonyStyle->writeln($diff);

exit(Command::FAILURE);
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ parameters:
- tests
- rules-tests
- utils
- e2e/e2eTestRunnerWithCache.php
- e2e/e2eTestRunner.php

scanDirectories:
- stubs
Expand Down
11 changes: 7 additions & 4 deletions src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\ChangesReporting\ValueObjectFactory;

use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Console\Formatter\ConsoleDiffer;
use Rector\Console\Formatter\ColorConsoleDiffFormatter;
use Rector\Differ\DefaultDiffer;
use Rector\FileSystem\FilePathHelper;
use Rector\ValueObject\Application\File;
Expand All @@ -15,8 +15,8 @@
{
public function __construct(
private DefaultDiffer $defaultDiffer,
private ConsoleDiffer $consoleDiffer,
private FilePathHelper $filePathHelper,
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter
) {
}

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

$diff = $shouldShowDiffs ? $this->defaultDiffer->diff($oldContent, $newContent) : '';
$consoleDiff = $shouldShowDiffs ? $this->colorConsoleDiffFormatter->format($diff) : '';

// always keep the most recent diff
return new FileDiff(
$relativeFilePath,
$shouldShowDiffs ? $this->defaultDiffer->diff($oldContent, $newContent) : '',
$shouldShowDiffs ? $this->consoleDiffer->diff($oldContent, $newContent) : '',
$diff,
$consoleDiff,
$rectorsWithLineChanges
);
}
Expand Down
42 changes: 30 additions & 12 deletions src/Console/Formatter/ColorConsoleDiffFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
*/
private const AT_START_REGEX = '#^(@.*)#';

/**
* @var string
* @see https://regex101.com/r/8MXnfa/2
*/
private const AT_DIFF_LINE_REGEX = '#^\<fg=cyan\>@@ \-\d+,\d+ \+\d+,\d+ @@\<\/fg=cyan\>$#';

private string $template;

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

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

// remove description of added + remove; obvious on diffs
// remove description of added + remove, obvious on diffs
// decorize lines
foreach ($escapedDiffLines as $key => $escapedDiffLine) {
if ($escapedDiffLine === '--- Original') {
unset($escapedDiffLines[$key]);
continue;
}

if ($escapedDiffLine === '+++ New') {
unset($escapedDiffLines[$key]);
continue;
}
}

$coloredLines = array_map(function (string $string): string {
$string = $this->makePlusLinesGreen($string);
$string = $this->makeMinusLinesRed($string);
$string = $this->makeAtNoteCyan($string);

if ($string === ' ') {
return '';
if ($escapedDiffLine === ' ') {
$escapedDiffLines[$key] = '';
continue;
}

return $string;
}, $escapedDiffLines);
$escapedDiffLine = $this->makePlusLinesGreen($escapedDiffLine);
$escapedDiffLine = $this->makeMinusLinesRed($escapedDiffLine);
$escapedDiffLine = $this->makeAtNoteCyan($escapedDiffLine);
$escapedDiffLine = $this->normalizeLineAtDiff($escapedDiffLine);

return sprintf($template, implode(PHP_EOL, $coloredLines));
// final decorized line
$escapedDiffLines[$key] = $escapedDiffLine;
}

return sprintf($template, implode(PHP_EOL, $escapedDiffLines));
}

/**
* Remove number diff, eg; @@ -67,6 +67,8 @@ to become @@ @@
*/
private function normalizeLineAtDiff(string $string): string
{
return Strings::replace($string, self::AT_DIFF_LINE_REGEX, '<fg=cyan>@@ @@</fg=cyan>');
}

private function makePlusLinesGreen(string $string): string
Expand Down
28 changes: 0 additions & 28 deletions src/Console/Formatter/ConsoleDiffer.php

This file was deleted.

Loading