respect-validation/src-dev/Markdown/Linters/ValidatorRelatedLinter.php
Henrique Moody 7aef3763f2
Add doc linter to check related validators
This commit ensures that if validator A has a direct link to validator
B, validator B will have a direct link to validator A too.
2026-01-13 23:37:06 -07:00

83 lines
2.3 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Dev\Markdown\Linters;
use Respect\Dev\Markdown\Content;
use Respect\Dev\Markdown\File;
use Respect\Dev\Markdown\Linter;
use function array_keys;
use function array_unique;
use function basename;
use function dirname;
use function file_get_contents;
use function in_array;
use function preg_match_all;
use function sort;
use function sprintf;
use function str_contains;
use function str_ends_with;
final readonly class ValidatorRelatedLinter implements Linter
{
public function lint(File $file): File
{
if (!str_contains($file->filename, '/validators/') || str_ends_with($file->filename, '/validators/index.md')) {
return $file;
}
$validator = basename($file->filename, '.md');
$relatedValidators = $this->getRelatedValidators($validator);
if ($relatedValidators === []) {
return $file;
}
$content = new Content();
$content->paragraph('See also:');
$content->emptyLine();
foreach ($relatedValidators as $relatedValidator) {
$content->anchorListItem($relatedValidator, $relatedValidator . '.md');
}
$content->emptyLine();
return $file->withContent($file->content->withSection($content));
}
/** @return array<string> */
private function getRelatedValidators(string $validator): array
{
$docsDirectory = dirname(__DIR__, 3) . '/docs';
$filename = sprintf('%s/validators/%s.md', $docsDirectory, $validator);
$content = file_get_contents($filename);
if ($content === false) {
return [];
}
$relatedValidators = [];
preg_match_all('/\[([^\]]+)\]\(([^\)]+\.md)\)/', $content, $matches);
foreach (array_keys($matches[0]) as $key) {
$related = $matches[1][$key];
$document = $matches[2][$key];
if (str_contains($document, '/') || in_array($related, $relatedValidators)) {
continue;
}
$relatedValidators[] = $related;
}
$relatedValidators = array_unique($relatedValidators);
sort($relatedValidators);
return $relatedValidators;
}
}