diff --git a/e2e/e2eTestRunner.php b/e2e/e2eTestRunner.php index 3ce312202f2..e99630b161f 100644 --- a/e2e/e2eTestRunner.php +++ b/e2e/e2eTestRunner.php @@ -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; @@ -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)); } @@ -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); diff --git a/e2e/e2eTestRunnerWithCache.php b/e2e/e2eTestRunnerWithCache.php index c45533534e8..033b7fd2440 100644 --- a/e2e/e2eTestRunnerWithCache.php +++ b/e2e/e2eTestRunnerWithCache.php @@ -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; @@ -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); diff --git a/phpstan.neon b/phpstan.neon index c1b8e3aa3b2..756eba937d9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -26,6 +26,8 @@ parameters: - tests - rules-tests - utils + - e2e/e2eTestRunnerWithCache.php + - e2e/e2eTestRunner.php scanDirectories: - stubs diff --git a/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php b/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php index babbe6bd860..c816c615cec 100644 --- a/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php +++ b/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php @@ -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; @@ -15,8 +15,8 @@ { public function __construct( private DefaultDiffer $defaultDiffer, - private ConsoleDiffer $consoleDiffer, private FilePathHelper $filePathHelper, + private ColorConsoleDiffFormatter $colorConsoleDiffFormatter ) { } @@ -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 ); } diff --git a/src/Console/Formatter/ColorConsoleDiffFormatter.php b/src/Console/Formatter/ColorConsoleDiffFormatter.php index 73d235dc726..dfc6c7cb4ab 100644 --- a/src/Console/Formatter/ColorConsoleDiffFormatter.php +++ b/src/Console/Formatter/ColorConsoleDiffFormatter.php @@ -34,6 +34,12 @@ */ private const AT_START_REGEX = '#^(@.*)#'; + /** + * @var string + * @see https://regex101.com/r/8MXnfa/2 + */ + private const AT_DIFF_LINE_REGEX = '#^\@@ \-\d+,\d+ \+\d+,\d+ @@\<\/fg=cyan\>$#'; + private string $template; public function __construct() @@ -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, '@@ @@'); } private function makePlusLinesGreen(string $string): string diff --git a/src/Console/Formatter/ConsoleDiffer.php b/src/Console/Formatter/ConsoleDiffer.php deleted file mode 100644 index a50c1d4e91f..00000000000 --- a/src/Console/Formatter/ConsoleDiffer.php +++ /dev/null @@ -1,28 +0,0 @@ -differ = new Differ($unifiedDiffOutputBuilder); - } - - public function diff(string $old, string $new): string - { - $diff = $this->differ->diff($old, $new); - return $this->colorConsoleDiffFormatter->format($diff); - } -}