respect-validation/src-dev/Spdx/ContributorExtractor/HeaderContributorExtractor.php
Henrique Moody 7db3bea8a6
Enhance LintSpdxCommand with contributor tracking and header normalization
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)
2026-02-03 15:23:20 +01:00

53 lines
1.3 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\ContributorExtractor;
use Respect\Dev\Spdx\Contributor;
use function file_get_contents;
use function preg_match;
use function preg_match_all;
final readonly class HeaderContributorExtractor implements ContributorExtractor
{
public function __construct(
private string $extensionPattern,
) {
}
/** @return array<Contributor> */
public function extract(string $filepath): array
{
$content = file_get_contents($filepath);
preg_match($this->extensionPattern, $content, $matches);
$header = $matches[1] ?? '';
preg_match_all('/SPDX-FileContributor:\s*(.+)$/m', $header, $matches);
$contributors = [];
if (isset($matches[1]) === false) {
return $contributors;
}
foreach ($matches[1] as $contributor) {
preg_match('/^(.+)( <(.+)>)?$/', $contributor, $parts);
$name = $parts[1] ?? '';
$email = $parts[3] ?? null;
if ($name === '') {
continue;
}
$contributors[] = Contributor::create($name, $email);
}
return $contributors;
}
}