diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-07-21 08:08:15 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-07-21 08:08:45 +0900 |
| commit | c899a4675ca90507a420d901ef7fa26d8b285f23 (patch) | |
| tree | 405b8049c3ac59c65d56792c96f4a8c316bf605b /scripts/linters/src/Linters/SortedDependencies.php | |
| parent | 6b16a2cd672edcbfeaf7988591db92ed4c594ddc (diff) | |
| download | php-shirabe-c899a4675ca90507a420d901ef7fa26d8b285f23.tar.gz php-shirabe-c899a4675ca90507a420d901ef7fa26d8b285f23.tar.zst php-shirabe-c899a4675ca90507a420d901ef7fa26d8b285f23.zip | |
refactor(lint): rewrite structural linters from Ruby to PHP
Fold scripts/lint and scripts/linters/*.rb into a standalone Composer
project under scripts/linters/, matching the scripts/plugin-class-classifier/
convention. Uses no external packages, only PHP + Composer autoloading.
Verified byte-for-byte identical output against the original Ruby
implementation, both on the current repo (all linters pass) and on a
synthetic fixture exercising every violation type.
Entry point moves from `scripts/lint` to `scripts/linters/lint`.
Diffstat (limited to 'scripts/linters/src/Linters/SortedDependencies.php')
| -rw-r--r-- | scripts/linters/src/Linters/SortedDependencies.php | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/scripts/linters/src/Linters/SortedDependencies.php b/scripts/linters/src/Linters/SortedDependencies.php new file mode 100644 index 00000000..78ea0eb2 --- /dev/null +++ b/scripts/linters/src/Linters/SortedDependencies.php @@ -0,0 +1,100 @@ +<?php + +declare(strict_types=1); + +namespace Shirabe\Lint\Linters; + +use Shirabe\Lint\Linter; +use Shirabe\Lint\Support\FileFinder; +use Shirabe\Lint\Support\Paths; + +final class SortedDependencies implements Linter +{ + public function name(): string + { + return 'sorted_dependencies'; + } + + public function failureIntro(): string + { + return "Found unsorted `[dependencies]` / `[dev-dependencies]` in Cargo.toml.\n" + . 'Entries must be alphabetical, with `shirabe-*` crates listed before others:'; + } + + public function check(string $rootDir, array $excludes): array + { + $errors = []; + + foreach (FileFinder::cargoTomls($rootDir) as $path) { + $relative = Paths::relativeTo($rootDir, $path); + if (in_array($relative, $excludes, true)) { + continue; + } + + $sections = $this->parseDepSections(file_get_contents($path)); + + foreach (['dependencies', 'dev-dependencies'] as $section) { + $deps = $sections[$section] ?? []; + if ($deps === []) { + continue; + } + + $expected = $this->sortDepNames($deps); + if ($deps === $expected) { + continue; + } + + $errors[] = "{$relative} [{$section}]\n" + . ' actual: ' . implode(', ', $deps) . "\n" + . ' expected: ' . implode(', ', $expected); + } + } + + return $errors; + } + + /** @return array<string, list<string>> */ + private function parseDepSections(string $content): array + { + $sections = []; + $current = null; + + foreach (explode("\n", $content) as $line) { + $stripped = rtrim($line, "\r\n"); + + if (preg_match('/\A\s*\[([^\]]+)\]\s*\z/', $stripped, $m)) { + $current = $m[1]; + $sections[$current] ??= []; + continue; + } + + if ($current !== null && preg_match('/\A([A-Za-z0-9_-]+)\s*[.=]/', $stripped, $m)) { + $sections[$current][] = $m[1]; + } + } + + return $sections; + } + + /** + * @param list<string> $deps + * @return list<string> + */ + private function sortDepNames(array $deps): array + { + $shirabe = []; + $other = []; + + foreach ($deps as $dep) { + if (str_starts_with($dep, 'shirabe-')) { + $shirabe[] = $dep; + } else { + $other[] = $dep; + } + } + sort($shirabe); + sort($other); + + return array_merge($shirabe, $other); + } +} |
