mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 14:55:44 +01:00
Improves SPDX header linting to ensure consistent license metadata across the codebase. Key changes: - Enforce deterministic tag ordering (License-Identifier, FileCopyrightText, FileContributor) to ensure consistency, prevent merge conflicts, and simplify code reviews - Add contributor alias mapping to consolidate contributors with multiple emails or name variations (e.g., "nickl-" → "Nick Lombard") - Add --contributions-strategy option with "blame" (current code authors) and "log" (all historical contributors) to support different attribution philosophies - Add optional path argument to lint specific files or directories - Add --fix option to automatically correct header issues Assisted-by: Claude Code (claude-opus-4-5-20251101)
62 lines
1.7 KiB
PHP
62 lines
1.7 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\Spdx\HeaderRebuilder;
|
|
|
|
use Respect\Dev\Spdx\Contributor;
|
|
|
|
use function implode;
|
|
use function preg_match;
|
|
use function preg_replace;
|
|
use function sprintf;
|
|
|
|
use const PHP_EOL;
|
|
|
|
final class HtmlHeaderRebuilder implements HeaderRebuilder
|
|
{
|
|
private const string SPDX_HEADER_PATTERN = '/(<!--\n)(SPDX-[^\n]+\n)+(-->)/';
|
|
|
|
/** @param array<Contributor> $contributors */
|
|
public function rebuild(string $content, array $contributors): string
|
|
{
|
|
// REUSE-IgnoreStart
|
|
$spdxLines = [
|
|
'SPDX-License-Identifier: MIT',
|
|
'SPDX-FileCopyrightText: (c) Respect Project Contributors',
|
|
];
|
|
|
|
foreach ($contributors as $contributor) {
|
|
if ($contributor->email === null) {
|
|
$spdxLines[] = sprintf('SPDX-FileContributor: %s', $contributor->name);
|
|
continue;
|
|
}
|
|
|
|
$spdxLines[] = sprintf('SPDX-FileContributor: %s <%s>', $contributor->name, $contributor->email);
|
|
}
|
|
|
|
// REUSE-IgnoreEnd
|
|
|
|
$newSpdxBlock = implode(PHP_EOL, $spdxLines);
|
|
|
|
// If header exists, replace it
|
|
if (preg_match(self::SPDX_HEADER_PATTERN, $content) === 1) {
|
|
return preg_replace(
|
|
self::SPDX_HEADER_PATTERN,
|
|
'$1' . $newSpdxBlock . PHP_EOL . '$3',
|
|
$content,
|
|
);
|
|
}
|
|
|
|
// Otherwise, add header at the beginning
|
|
$header = '<!--' . PHP_EOL . $newSpdxBlock . PHP_EOL . '-->' . PHP_EOL . PHP_EOL;
|
|
|
|
return $header . $content;
|
|
}
|
|
}
|