Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/only-option/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions e2e/rules-summary/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion e2e/typo3-extension/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
1) typo3-extension/result/my_theme/Configuration/Sets/BaseSet/setup.typoscript:1

---------- begin diff ----------
@@ @@
@@ Line 1 @@
-<INCLUDE_TYPOSCRIPT: source="DIR:EXT:my_theme/Configuration/PageTsConfig/Includes/" extensions="tsconfig.typoscript">
+@import 'EXT:my_theme/Configuration/PageTsConfig/Includes/*.tsconfig.typoscript'
----------- end diff -----------
Expand Down
2 changes: 1 addition & 1 deletion e2e/typo3-typoscript/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion e2e/typo3-xml/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
1) typo3-xml/result/Fixture.xml:9

---------- begin diff ----------
@@ @@
@@ Line 9 @@
<settings.nullable_field_trimmed>
<label>Nullable field -> trimmed</label>
<config>
Expand Down
4 changes: 2 additions & 2 deletions e2e/typo3-yaml/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
1) typo3-yaml/result/my_form.form.yaml:2

---------- begin diff ----------
@@ @@
@@ Line 2 @@
-
options:
recipients:
Expand Down Expand Up @@ -54,7 +54,7 @@
identifier: EmailToSender
variants:
-
@@ @@
@@ Line 38 @@
identifier: EmailToReceiver
options:
subject: 'Example Subject'
Expand Down
3 changes: 0 additions & 3 deletions packages/fractor/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
Expand Down
29 changes: 0 additions & 29 deletions packages/fractor/src/Differ/ConsoleDiffer.php

This file was deleted.

12 changes: 0 additions & 12 deletions packages/fractor/src/Differ/Contract/Differ.php

This file was deleted.

16 changes: 7 additions & 9 deletions packages/fractor/src/Differ/DefaultDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@

namespace a9f\Fractor\Differ;

use a9f\Fractor\Differ\Contract\Differ;
use a9f\Fractor\Differ\ValueObject\Diff;
use SebastianBergmann\Diff\Differ as CoreDiffer;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;

final readonly class DefaultDiffer implements Differ
final readonly class DefaultDiffer
{
private CoreDiffer $differ;
private Differ $differ;

public function __construct()
{
$strictUnifiedDiffOutputBuilder = new StrictUnifiedDiffOutputBuilder([
'fromFile' => '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);
}
}
43 changes: 28 additions & 15 deletions packages/fractor/src/Differ/Formatter/ColorConsoleDiffFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace a9f\Fractor\Differ\Formatter;

use a9f\Fractor\Util\NewLineSplitter;
use Nette\Utils\Strings;
use Symfony\Component\Console\Formatter\OutputFormatter;

Expand All @@ -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 = '#^\<fg=cyan\>@@ \-(\d+)(,\d+)? \+\d+(,\d+)? @@\<\/fg=cyan\>$#';

private string $template;

Expand All @@ -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
Expand All @@ -95,4 +100,12 @@ private function makeAtNoteCyan(string $string): string
{
return Strings::replace($string, self::AT_START_REGEX, '<fg=cyan>$1</fg=cyan>');
}

/**
* 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, '<fg=cyan>@@ Line $1 @@</fg=cyan>');
}
}
2 changes: 2 additions & 0 deletions packages/fractor/src/Differ/ValueObject/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function getFractorClasses(): array
foreach ($this->appliedRules as $appliedRule) {
$fractorClasses[] = $appliedRule->getFractorClass();
}
$fractorClasses = array_unique($fractorClasses);
sort($fractorClasses);
return $fractorClasses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,8 +15,8 @@
{
public function __construct(
private DefaultDiffer $defaultDiffer,
private ConsoleDiffer $consoleDiffer,
private FilePathHelper $filePathHelper,
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter,
private FractorsChangelogLinesResolver $fractorsChangelogLinesResolver,
) {
}
Expand All @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions packages/fractor/src/Util/NewLineSplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace a9f\Fractor\Util;

use Nette\Utils\Strings;

final class NewLineSplitter
{
/**
* @see https://regex101.com/r/qduj2O/4
*/
private const NEWLINES_REGEX = "#\r?\n#";

/**
* @return string[]
*/
public static function split(string $content): array
{
return Strings::split($content, self::NEWLINES_REGEX);
}
}