respect-validation/src-dev/Differ/ConsoleDiffer.php
Henrique Moody 7c681fec66
Fix SPDX headers in all files
I ran the `bin/console spdx --fix` with different strategies for
different files. For most of the core classes, since they've been
drastically rebuilt, I've run it with the `git-blame` strategy, for for
the `src/Validators`, in which the API changed completely but the logic
remains the same, I use the `git-log` strategy.
2026-02-03 15:23:23 +01:00

59 lines
1.5 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Dev\Differ;
use SebastianBergmann\Diff\Differ as SebastianBergmannDiffer;
use function getcwd;
use function preg_replace_callback;
use function sprintf;
use function str_replace;
use const PHP_EOL;
final readonly class ConsoleDiffer
{
public function __construct(
private SebastianBergmannDiffer $differ,
) {
}
public function diff(Item $from, Item $to): string|null
{
$diff = $this->differ->diff($from->content, $to->content);
if ($diff === '') {
return null;
}
$content = sprintf('<options=bold>--- a/%s</>' . PHP_EOL, $this->getRelativePath($from->headline));
$content .= sprintf('<options=bold>+++ b/%s</>' . PHP_EOL, $this->getRelativePath($to->headline));
return $content . preg_replace_callback(
'/^([+-]|@{2})(.*)$/m',
static fn($matches) => sprintf(
'<fg=%s>%s</>',
match ($matches[1]) {
'+' => 'green',
'-' => 'red',
'@@' => 'cyan',
},
$matches[0],
),
$diff,
);
}
private function getRelativePath(string $filename): string
{
return str_replace(getcwd() . '/', '', $filename);
}
}