From ebdd056d9a9e57d8faf9c616493edd3a1ed3c456 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Tue, 30 Jun 2026 00:16:06 +0200 Subject: [PATCH] [TASK] Remove ConsoleDiffer, use DefaultDiffer that utilize ColorConsoleDiffFormatter instead to avoid double diffing process See https://github.com/rectorphp/rector-src/pull/7309 --- e2e/only-option/expected-output.txt | 2 +- e2e/rules-summary/expected-output.txt | 6 +-- e2e/typo3-extension/expected-output.txt | 2 +- e2e/typo3-typoscript/expected-output.txt | 2 +- e2e/typo3-xml/expected-output.txt | 2 +- e2e/typo3-yaml/expected-output.txt | 4 +- packages/fractor/config/application.php | 3 -- packages/fractor/src/Differ/ConsoleDiffer.php | 29 ------------- .../fractor/src/Differ/Contract/Differ.php | 12 ------ packages/fractor/src/Differ/DefaultDiffer.php | 16 +++---- .../Formatter/ColorConsoleDiffFormatter.php | 43 ++++++++++++------- .../src/Differ/ValueObject/FileDiff.php | 2 + .../ValueObjectFactory/FileDiffFactory.php | 8 ++-- packages/fractor/src/Util/NewLineSplitter.php | 23 ++++++++++ 14 files changed, 73 insertions(+), 81 deletions(-) delete mode 100644 packages/fractor/src/Differ/ConsoleDiffer.php delete mode 100644 packages/fractor/src/Differ/Contract/Differ.php create mode 100644 packages/fractor/src/Util/NewLineSplitter.php diff --git a/e2e/only-option/expected-output.txt b/e2e/only-option/expected-output.txt index 78422772..107eaa7f 100644 --- a/e2e/only-option/expected-output.txt +++ b/e2e/only-option/expected-output.txt @@ -5,7 +5,7 @@ 1) only-option/result/fixture.typoscript:1 ---------- begin diff ---------- -@@ @@ +@@ Line 1 @@ -config.xhtmlDoctype = 1 +config.doctype = 1 config.metaCharset = utf-8 diff --git a/e2e/rules-summary/expected-output.txt b/e2e/rules-summary/expected-output.txt index f148a58c..0a0f901d 100644 --- a/e2e/rules-summary/expected-output.txt +++ b/e2e/rules-summary/expected-output.txt @@ -5,22 +5,22 @@ 1) rules-summary/result/fixture.typoscript:1 ---------- begin diff ---------- -@@ @@ +@@ Line 1 @@ -config.xhtmlDoctype = 1 -config.metaCharset = utf-8 +config.doctype = 1 ----------- end diff ----------- Applied rules: - * RenameConfigXhtmlDoctypeToDoctypeFractor * RemoveConfigMetaCharsetFractor + * RenameConfigXhtmlDoctypeToDoctypeFractor Rules Summary ------------- - * RenameConfigXhtmlDoctypeToDoctypeFractor was applied 1 time * RemoveConfigMetaCharsetFractor was applied 1 time + * RenameConfigXhtmlDoctypeToDoctypeFractor was applied 1 time [OK] 1 file has been changed by Fractor diff --git a/e2e/typo3-extension/expected-output.txt b/e2e/typo3-extension/expected-output.txt index 206977ea..f570f376 100644 --- a/e2e/typo3-extension/expected-output.txt +++ b/e2e/typo3-extension/expected-output.txt @@ -5,7 +5,7 @@ 1) typo3-extension/result/my_theme/Configuration/Sets/BaseSet/setup.typoscript:1 ---------- begin diff ---------- -@@ @@ +@@ Line 1 @@ - +@import 'EXT:my_theme/Configuration/PageTsConfig/Includes/*.tsconfig.typoscript' ----------- end diff ----------- diff --git a/e2e/typo3-typoscript/expected-output.txt b/e2e/typo3-typoscript/expected-output.txt index 3fd98c78..12f34e40 100644 --- a/e2e/typo3-typoscript/expected-output.txt +++ b/e2e/typo3-typoscript/expected-output.txt @@ -5,7 +5,7 @@ 1) typo3-typoscript/result/cache-hash.typoscript:3 ---------- begin diff ---------- -@@ @@ +@@ Line 3 @@ page.10.value = Link to page 23 page.10.typolink { parameter = 23 diff --git a/e2e/typo3-xml/expected-output.txt b/e2e/typo3-xml/expected-output.txt index eece6132..80a5bdee 100644 --- a/e2e/typo3-xml/expected-output.txt +++ b/e2e/typo3-xml/expected-output.txt @@ -5,7 +5,7 @@ 1) typo3-xml/result/Fixture.xml:9 ---------- begin diff ---------- -@@ @@ +@@ Line 9 @@ diff --git a/e2e/typo3-yaml/expected-output.txt b/e2e/typo3-yaml/expected-output.txt index 6b91f5b9..2a80bcb8 100644 --- a/e2e/typo3-yaml/expected-output.txt +++ b/e2e/typo3-yaml/expected-output.txt @@ -5,7 +5,7 @@ 1) typo3-yaml/result/my_form.form.yaml:2 ---------- begin diff ---------- -@@ @@ +@@ Line 2 @@ - options: recipients: @@ -54,7 +54,7 @@ identifier: EmailToSender variants: - -@@ @@ +@@ Line 38 @@ identifier: EmailToReceiver options: subject: 'Example Subject' diff --git a/packages/fractor/config/application.php b/packages/fractor/config/application.php index 2020e976..7d26e75e 100644 --- a/packages/fractor/config/application.php +++ b/packages/fractor/config/application.php @@ -20,8 +20,6 @@ use a9f\Fractor\Console\Style\SymfonyStyleFactory; use a9f\Fractor\Contract\FilesystemInterface; use a9f\Fractor\Contract\LocalFilesystemInterface; -use a9f\Fractor\Differ\ConsoleDiffer; -use a9f\Fractor\Differ\Contract\Differ; use a9f\Fractor\FileSystem\FilesystemFactory; use a9f\Fractor\FileSystem\FlysystemFilesystem; use League\Flysystem\FilesystemAdapter; @@ -111,7 +109,6 @@ static function (ChildDefinition $definition, AsCommand $attribute): void { ->alias(ContainerBagInterface::class, 'parameter_bag') ->alias(ParameterBagInterface::class, 'parameter_bag'); - $services->alias(Differ::class, ConsoleDiffer::class); $services->set(FractorApplication::class)->call('setCommandLoader', [service('console.command_loader')]); $services->set(SkipConfiguration::class)->factory([service(SkipConfigurationFactory::class), 'create']); $services->set(FractorRunner::class)->arg('$processors', tagged_iterator('fractor.file_processor')); diff --git a/packages/fractor/src/Differ/ConsoleDiffer.php b/packages/fractor/src/Differ/ConsoleDiffer.php deleted file mode 100644 index d5763fc9..00000000 --- a/packages/fractor/src/Differ/ConsoleDiffer.php +++ /dev/null @@ -1,29 +0,0 @@ -differ = new CoreDiffer($unifiedDiffOutputBuilder); - } - - public function diff(Diff $diff): string - { - $diff = $this->differ->diff($diff->getOldContent(), $diff->getNewContent()); - return $this->colorConsoleDiffFormatter->format($diff); - } -} diff --git a/packages/fractor/src/Differ/Contract/Differ.php b/packages/fractor/src/Differ/Contract/Differ.php deleted file mode 100644 index ae79f3c1..00000000 --- a/packages/fractor/src/Differ/Contract/Differ.php +++ /dev/null @@ -1,12 +0,0 @@ - 'Original', 'toFile' => 'New', ]); - $this->differ = new CoreDiffer($strictUnifiedDiffOutputBuilder); + $this->differ = new Differ($strictUnifiedDiffOutputBuilder); } - public function diff(Diff $diff): string + public function diff(string $old, string $new): string { - if (! $diff->isDifferent()) { + if ($old === $new) { return ''; } - return $this->differ->diff($diff->getOldContent(), $diff->getNewContent()); + return $this->differ->diff($old, $new); } } diff --git a/packages/fractor/src/Differ/Formatter/ColorConsoleDiffFormatter.php b/packages/fractor/src/Differ/Formatter/ColorConsoleDiffFormatter.php index 31b1d9b2..198fc2d9 100644 --- a/packages/fractor/src/Differ/Formatter/ColorConsoleDiffFormatter.php +++ b/packages/fractor/src/Differ/Formatter/ColorConsoleDiffFormatter.php @@ -4,6 +4,7 @@ namespace a9f\Fractor\Differ\Formatter; +use a9f\Fractor\Util\NewLineSplitter; use Nette\Utils\Strings; use Symfony\Component\Console\Formatter\OutputFormatter; @@ -29,9 +30,9 @@ /** * @var string - * @see https://regex101.com/r/qduj2O/1 + * @see https://regex101.com/r/8MXnfa/2 */ - private const NEWLINES_REGEX = "#\n\r|\n#"; + private const AT_DIFF_LINE_REGEX = '#^\@@ \-(\d+)(,\d+)? \+\d+(,\d+)? @@\<\/fg=cyan\>$#'; private string $template; @@ -53,32 +54,36 @@ private function formatWithTemplate(string $diff, string $template): string { $escapedDiff = OutputFormatter::escape(rtrim($diff)); - $escapedDiffLines = Strings::split($escapedDiff, self::NEWLINES_REGEX); + $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)); } private function makePlusLinesGreen(string $string): string @@ -95,4 +100,12 @@ private function makeAtNoteCyan(string $string): string { return Strings::replace($string, self::AT_START_REGEX, '$1'); } + + /** + * Simplify diff line info, eg; @@ -67,6 +67,8 @@ to become @@ Line 67 @@ + */ + private function normalizeLineAtDiff(string $string): string + { + return Strings::replace($string, self::AT_DIFF_LINE_REGEX, '@@ Line $1 @@'); + } } diff --git a/packages/fractor/src/Differ/ValueObject/FileDiff.php b/packages/fractor/src/Differ/ValueObject/FileDiff.php index aabe957b..03f25db6 100644 --- a/packages/fractor/src/Differ/ValueObject/FileDiff.php +++ b/packages/fractor/src/Differ/ValueObject/FileDiff.php @@ -93,6 +93,8 @@ public function getFractorClasses(): array foreach ($this->appliedRules as $appliedRule) { $fractorClasses[] = $appliedRule->getFractorClass(); } + $fractorClasses = array_unique($fractorClasses); + sort($fractorClasses); return $fractorClasses; } diff --git a/packages/fractor/src/Differ/ValueObjectFactory/FileDiffFactory.php b/packages/fractor/src/Differ/ValueObjectFactory/FileDiffFactory.php index 0d8f10e7..d7d90ba6 100644 --- a/packages/fractor/src/Differ/ValueObjectFactory/FileDiffFactory.php +++ b/packages/fractor/src/Differ/ValueObjectFactory/FileDiffFactory.php @@ -5,8 +5,8 @@ namespace a9f\Fractor\Differ\ValueObjectFactory; use a9f\Fractor\Application\ValueObject\File; -use a9f\Fractor\Differ\ConsoleDiffer; use a9f\Fractor\Differ\DefaultDiffer; +use a9f\Fractor\Differ\Formatter\ColorConsoleDiffFormatter; use a9f\Fractor\Differ\ValueObject\FileDiff; use a9f\Fractor\FileSystem\FilePathHelper; use a9f\Fractor\Reporting\FractorsChangelogLinesResolver; @@ -15,8 +15,8 @@ { public function __construct( private DefaultDiffer $defaultDiffer, - private ConsoleDiffer $consoleDiffer, private FilePathHelper $filePathHelper, + private ColorConsoleDiffFormatter $colorConsoleDiffFormatter, private FractorsChangelogLinesResolver $fractorsChangelogLinesResolver, ) { } @@ -25,8 +25,8 @@ public function createFileDiff(bool $shouldShowDiffs, File $file): FileDiff { $relativeFilePath = $this->filePathHelper->relativePath($file->getFilePath()); - $diff = $shouldShowDiffs ? $this->defaultDiffer->diff($file->getDiff()) : ''; - $consoleDiff = $shouldShowDiffs ? $this->consoleDiffer->diff($file->getDiff()) : ''; + $diff = $shouldShowDiffs ? $this->defaultDiffer->diff($file->getOriginalContent(), $file->getContent()) : ''; + $consoleDiff = $shouldShowDiffs ? $this->colorConsoleDiffFormatter->format($diff) : ''; $fractorsChangelogsLines = $this->fractorsChangelogLinesResolver->createFractorChangelogLines( $file->getAppliedRules() diff --git a/packages/fractor/src/Util/NewLineSplitter.php b/packages/fractor/src/Util/NewLineSplitter.php new file mode 100644 index 00000000..6e96cf28 --- /dev/null +++ b/packages/fractor/src/Util/NewLineSplitter.php @@ -0,0 +1,23 @@ +