respect-validation/src-dev/Markdown/Differ.php
Henrique Moody 098c973a2a
Add GitHub action to lint documentation files
When we make changes to the category of a validator, it's easy to forget
to update overall list of validators. This commit a GitHub actions that
will run a console command to check if the documentation it up-to-date.

The job will fail when we need to change the document, but the console
command will fix the issues, so there isn't a lot of friction there.
2026-01-13 23:37:05 -07:00

57 lines
1.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
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);
}
}