respect-validation/src-dev/Markdown/Differ.php
Alexandre Gomes Gaigalas d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00

58 lines
1.5 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Dev\Markdown;
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 Differ
{
public function __construct(
private SebastianBergmannDiffer $differ,
) {
}
public function diff(File $from, File $to): string|null
{
$diff = $this->differ->diff($from->content->build(), $to->content->build());
if ($diff === '') {
return null;
}
$content = sprintf('<options=bold>--- a/%s</>' . PHP_EOL, $this->getRelativePath($from->filename));
$content .= sprintf('<options=bold>+++ b/%s</>' . PHP_EOL, $this->getRelativePath($to->filename));
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);
}
}