mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 06:45:44 +01:00
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.
57 lines
1.4 KiB
PHP
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);
|
|
}
|
|
}
|