respect-validation/src-dev/Markdown/Linters/ValidatorRelatedLinter.php
Alexandre Gomes Gaigalas bd48bdcda4 Lint Changelog format in validator docs
Introduces a Markdown linter for checking the Changelog format.

"See Also" was transformed into a section to make it easier to
handle it with the `Content` class. The "Related" linter was
simplified to reflect that change too.

An additional "alignment" parameter was added to markdown table
generators, allowing the padding and headers to be explicitly
marked with a specific left (-1), middle (0) or right(1)
alignment.

Existing files were fixed using the `fix` option after the
changes.
2026-01-26 19:11:00 +00:00

71 lines
1.8 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\Linters;
use Respect\Dev\Markdown\Content;
use Respect\Dev\Markdown\File;
use Respect\Dev\Markdown\Linter;
use UnexpectedValueException;
use function array_unique;
use function preg_match_all;
use function sort;
use function str_contains;
final readonly class ValidatorRelatedLinter implements Linter
{
public function lint(File $file): File
{
if (!str_contains($file->filename, '/validators/')) {
return $file;
}
$relatedValidators = $this->getRelatedValidators($file);
if ($relatedValidators === []) {
return $file;
}
$content = new Content();
$content->h2('See Also');
foreach ($relatedValidators as $relatedValidator) {
$content->anchorListItem($relatedValidator, $relatedValidator . '.md');
}
$content->emptyLine();
return $file->withContent($file->content->withSection($content));
}
/** @return array<string> */
private function getRelatedValidators(File $validator): array
{
try {
$seeAlso = $validator->content->getSection('## See Also');
} catch (UnexpectedValueException) {
return [];
}
$relatedValidators = [];
$lines = $seeAlso->toArray();
foreach ($lines as $line) {
preg_match_all('/\[(.+?)\]\((.+?)\.md\)/', $line, $matches);
foreach ($matches[1] as $match) {
$relatedValidators[] = $match;
}
}
$relatedValidators = array_unique($relatedValidators);
sort($relatedValidators);
return $relatedValidators;
}
}