From 8614e83d2ca2bb04c7c0aacf28c5b722d67df51d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 23 Nov 2024 20:22:09 +0700 Subject: [PATCH 1/5] [Differ] Remove DefaultDiffer, use ConsoleDiffer instead to avoid double diffing process --- .../Output/ConsoleOutputFormatter.php | 2 +- .../ValueObjectFactory/FileDiffFactory.php | 3 -- .../Formatter/ColorConsoleDiffFormatter.php | 2 ++ src/Console/Formatter/ConsoleDiffer.php | 12 ++++--- .../Formatter/NumberLineDiffCleaner.php | 28 +++++++++++++++++ src/Differ/DefaultDiffer.php | 31 ------------------- src/ValueObject/Reporting/FileDiff.php | 13 ++------ 7 files changed, 41 insertions(+), 50 deletions(-) create mode 100644 src/Console/Formatter/NumberLineDiffCleaner.php delete mode 100644 src/Differ/DefaultDiffer.php diff --git a/src/ChangesReporting/Output/ConsoleOutputFormatter.php b/src/ChangesReporting/Output/ConsoleOutputFormatter.php index 6f8d911beb5..2af170b7b5b 100644 --- a/src/ChangesReporting/Output/ConsoleOutputFormatter.php +++ b/src/ChangesReporting/Output/ConsoleOutputFormatter.php @@ -97,7 +97,7 @@ private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath): void $this->symfonyStyle->writeln($message); $this->symfonyStyle->newLine(); - $this->symfonyStyle->writeln($fileDiff->getDiffConsoleFormatted()); + $this->symfonyStyle->writeln($fileDiff->getDiff()); if ($fileDiff->getRectorChanges() !== []) { $this->symfonyStyle->writeln('Applied rules:'); diff --git a/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php b/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php index 10277abe290..2e79f07ed20 100644 --- a/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php +++ b/src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php @@ -6,7 +6,6 @@ use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Console\Formatter\ConsoleDiffer; -use Rector\Differ\DefaultDiffer; use Rector\FileSystem\FilePathHelper; use Rector\ValueObject\Application\File; use Rector\ValueObject\Reporting\FileDiff; @@ -14,7 +13,6 @@ final readonly class FileDiffFactory { public function __construct( - private DefaultDiffer $defaultDiffer, private ConsoleDiffer $consoleDiffer, private FilePathHelper $filePathHelper, ) { @@ -34,7 +32,6 @@ public function createFileDiffWithLineChanges( // always keep the most recent diff return new FileDiff( $relativeFilePath, - $this->defaultDiffer->diff($oldContent, $newContent), $this->consoleDiffer->diff($oldContent, $newContent), $rectorsWithLineChanges ); diff --git a/src/Console/Formatter/ColorConsoleDiffFormatter.php b/src/Console/Formatter/ColorConsoleDiffFormatter.php index 79d975f8425..5ac7d2c7208 100644 --- a/src/Console/Formatter/ColorConsoleDiffFormatter.php +++ b/src/Console/Formatter/ColorConsoleDiffFormatter.php @@ -60,10 +60,12 @@ private function formatWithTemplate(string $diff, string $template): string foreach ($escapedDiffLines as $key => $escapedDiffLine) { if ($escapedDiffLine === '--- Original') { unset($escapedDiffLines[$key]); + continue; } if ($escapedDiffLine === '+++ New') { unset($escapedDiffLines[$key]); + continue; } } diff --git a/src/Console/Formatter/ConsoleDiffer.php b/src/Console/Formatter/ConsoleDiffer.php index a50c1d4e91f..891c975c7f3 100644 --- a/src/Console/Formatter/ConsoleDiffer.php +++ b/src/Console/Formatter/ConsoleDiffer.php @@ -5,7 +5,7 @@ namespace Rector\Console\Formatter; use SebastianBergmann\Diff\Differ; -use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; +use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder; final readonly class ConsoleDiffer { @@ -14,10 +14,12 @@ public function __construct( private ColorConsoleDiffFormatter $colorConsoleDiffFormatter ) { - // @see https://github.com/sebastianbergmann/diff#strictunifieddiffoutputbuilder - // @see https://github.com/sebastianbergmann/diff/compare/4.0.4...5.0.0#diff-251edf56a6344c03fa264a4926b06c2cee43c25f66192d5f39ebee912b7442dc for upgrade - $unifiedDiffOutputBuilder = new UnifiedDiffOutputBuilder(); - $this->differ = new Differ($unifiedDiffOutputBuilder); + $strictUnifiedDiffOutputBuilder = new StrictUnifiedDiffOutputBuilder([ + 'fromFile' => 'Original', + 'toFile' => 'New', + ]); + + $this->differ = new Differ($strictUnifiedDiffOutputBuilder); } public function diff(string $old, string $new): string diff --git a/src/Console/Formatter/NumberLineDiffCleaner.php b/src/Console/Formatter/NumberLineDiffCleaner.php new file mode 100644 index 00000000000..6ea1d24ae04 --- /dev/null +++ b/src/Console/Formatter/NumberLineDiffCleaner.php @@ -0,0 +1,28 @@ +\-\d+,\d+ \+\d+,\d+) \@\@#'; + + public static function clean(string $diff): string + { + $diffLines = NewLineSplitter::split($diff); + + foreach ($diffLines as $key => $diffLine) { + $diffLines[$key] = Strings::replace($diffLine, self::LINE_RANGE_REGEX, fn(array $match): string => '@@ @@'); + } + + return implode(PHP_EOL, $diffLines); + } +} diff --git a/src/Differ/DefaultDiffer.php b/src/Differ/DefaultDiffer.php deleted file mode 100644 index 893918fddc9..00000000000 --- a/src/Differ/DefaultDiffer.php +++ /dev/null @@ -1,31 +0,0 @@ - 'Original', - 'toFile' => 'New', - ]); - $this->differ = new Differ($strictUnifiedDiffOutputBuilder); - } - - public function diff(string $old, string $new): string - { - if ($old === $new) { - return ''; - } - - return $this->differ->diff($old, $new); - } -} diff --git a/src/ValueObject/Reporting/FileDiff.php b/src/ValueObject/Reporting/FileDiff.php index 9362f2b0b43..c370ef89e8c 100644 --- a/src/ValueObject/Reporting/FileDiff.php +++ b/src/ValueObject/Reporting/FileDiff.php @@ -6,6 +6,7 @@ use Nette\Utils\Strings; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; +use Rector\Console\Formatter\NumberLineDiffCleaner; use Rector\Contract\Rector\RectorInterface; use Rector\Parallel\ValueObject\BridgeItem; use Symplify\EasyParallel\Contract\SerializableInterface; @@ -30,7 +31,6 @@ public function __construct( private string $relativeFilePath, private string $diff, - private string $diffConsoleFormatted, private array $rectorsWithLineChanges = [] ) { Assert::allIsInstanceOf($rectorsWithLineChanges, RectorWithLineChange::class); @@ -38,12 +38,7 @@ public function __construct( public function getDiff(): string { - return $this->diff; - } - - public function getDiffConsoleFormatted(): string - { - return $this->diffConsoleFormatted; + return NumberLineDiffCleaner::clean($this->diff); } public function getRelativeFilePath(): string @@ -104,14 +99,13 @@ public function getFirstLineNumber(): ?int } /** - * @return array{relative_file_path: string, diff: string, diff_console_formatted: string, rectors_with_line_changes: RectorWithLineChange[]} + * @return array{relative_file_path: string, diff: string, rectors_with_line_changes: RectorWithLineChange[]} */ public function jsonSerialize(): array { return [ BridgeItem::RELATIVE_FILE_PATH => $this->relativeFilePath, BridgeItem::DIFF => $this->diff, - BridgeItem::DIFF_CONSOLE_FORMATTED => $this->diffConsoleFormatted, BridgeItem::RECTORS_WITH_LINE_CHANGES => $this->rectorsWithLineChanges, ]; } @@ -130,7 +124,6 @@ public static function decode(array $json): self return new self( $json[BridgeItem::RELATIVE_FILE_PATH], $json[BridgeItem::DIFF], - $json[BridgeItem::DIFF_CONSOLE_FORMATTED], $rectorWithLineChanges, ); } From 866332f4c86dfddb44b2708976f8752b32cb0c83 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 23 Nov 2024 20:42:39 +0700 Subject: [PATCH 2/5] no need diffing when equals content --- src/Console/Formatter/ConsoleDiffer.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Console/Formatter/ConsoleDiffer.php b/src/Console/Formatter/ConsoleDiffer.php index 891c975c7f3..69db7b51ded 100644 --- a/src/Console/Formatter/ConsoleDiffer.php +++ b/src/Console/Formatter/ConsoleDiffer.php @@ -24,6 +24,10 @@ public function __construct( public function diff(string $old, string $new): string { + if ($old === $new) { + return ''; + } + $diff = $this->differ->diff($old, $new); return $this->colorConsoleDiffFormatter->format($diff); } From d894ad4ae8b50178f2d351f580ee3916f6c5e10d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 23 Nov 2024 21:17:30 +0700 Subject: [PATCH 3/5] final touch: more precise regex line --- src/Console/Formatter/NumberLineDiffCleaner.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/Formatter/NumberLineDiffCleaner.php b/src/Console/Formatter/NumberLineDiffCleaner.php index 6ea1d24ae04..12c4a904b53 100644 --- a/src/Console/Formatter/NumberLineDiffCleaner.php +++ b/src/Console/Formatter/NumberLineDiffCleaner.php @@ -13,14 +13,14 @@ class NumberLineDiffCleaner * @var string * @see https://regex101.com/r/zHJEfJ/1 */ - private const LINE_RANGE_REGEX = '#\@\@ (?\-\d+,\d+ \+\d+,\d+) \@\@#'; + private const LINE_RANGE_REGEX = '#^\@@ (?\-\d+,\d+ \+\d+,\d+) @@\<\/fg=cyan\>$#'; public static function clean(string $diff): string { $diffLines = NewLineSplitter::split($diff); foreach ($diffLines as $key => $diffLine) { - $diffLines[$key] = Strings::replace($diffLine, self::LINE_RANGE_REGEX, fn(array $match): string => '@@ @@'); + $diffLines[$key] = Strings::replace($diffLine, self::LINE_RANGE_REGEX, '@@ @@'); } return implode(PHP_EOL, $diffLines); From 4659ee6cd3d42cd3e2db8459c43265abedb2b159 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 23 Nov 2024 21:17:53 +0700 Subject: [PATCH 4/5] final touch: more precise regex line --- src/Console/Formatter/NumberLineDiffCleaner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Formatter/NumberLineDiffCleaner.php b/src/Console/Formatter/NumberLineDiffCleaner.php index 12c4a904b53..f3e197df777 100644 --- a/src/Console/Formatter/NumberLineDiffCleaner.php +++ b/src/Console/Formatter/NumberLineDiffCleaner.php @@ -11,7 +11,7 @@ class NumberLineDiffCleaner { /** * @var string - * @see https://regex101.com/r/zHJEfJ/1 + * @see https://regex101.com/r/4Sr4Ua/1 */ private const LINE_RANGE_REGEX = '#^\@@ (?\-\d+,\d+ \+\d+,\d+) @@\<\/fg=cyan\>$#'; From 42fe769ba56ea7e3fa9c92d948fe5e8a2f04c881 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 23 Nov 2024 21:19:06 +0700 Subject: [PATCH 5/5] final touch: clean continue; --- src/Console/Formatter/ColorConsoleDiffFormatter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Console/Formatter/ColorConsoleDiffFormatter.php b/src/Console/Formatter/ColorConsoleDiffFormatter.php index 5ac7d2c7208..0d1551899a5 100644 --- a/src/Console/Formatter/ColorConsoleDiffFormatter.php +++ b/src/Console/Formatter/ColorConsoleDiffFormatter.php @@ -65,7 +65,6 @@ private function formatWithTemplate(string $diff, string $template): string if ($escapedDiffLine === '+++ New') { unset($escapedDiffLines[$key]); - continue; } }