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/ContiguousUseBlock.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/ContiguousUseBlock.php')
| -rw-r--r-- | scripts/linters/src/Linters/ContiguousUseBlock.php | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/scripts/linters/src/Linters/ContiguousUseBlock.php b/scripts/linters/src/Linters/ContiguousUseBlock.php new file mode 100644 index 00000000..c73ea6f8 --- /dev/null +++ b/scripts/linters/src/Linters/ContiguousUseBlock.php @@ -0,0 +1,124 @@ +<?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 ContiguousUseBlock implements Linter +{ + private const USE_START_RE = '/\A(?:pub(?:\([^)]*\))?\s+)?use\b/'; + + public function name(): string + { + return 'contiguous_use_block'; + } + + public function failureIntro(): string + { + return "Found blank lines splitting the leading `use` block into sections.\n" + . 'All `use` statements at the top of the file must be contiguous (no blank lines between them):'; + } + + public function check(string $rootDir, array $excludes): array + { + $errors = []; + + foreach (FileFinder::rustFiles($rootDir) as $path) { + $relative = Paths::relativeTo($rootDir, $path); + if (in_array($relative, $excludes, true)) { + continue; + } + + array_push($errors, ...$this->findSplitUseBlock($path, $relative)); + } + + return $errors; + } + + /** @return list<string> */ + private function findSplitUseBlock(string $path, string $relative): array + { + $lines = file($path); + $errors = []; + $count = count($lines); + + $i = $this->skipPreamble($lines); + if ($i === null) { + return []; + } + + while (true) { + $i = $this->consumeUseStatement($lines, $i); + if ($i >= $count) { + break; + } + + $blanks = []; + $j = $i; + while ($j < $count) { + $stripped = trim($lines[$j]); + if ($stripped === '') { + $blanks[] = $j; + $j++; + } elseif (str_starts_with($stripped, '//') || str_starts_with($stripped, '#[')) { + $j++; + } else { + break; + } + } + + if ($j < $count && preg_match(self::USE_START_RE, trim($lines[$j]))) { + foreach ($blanks as $bi) { + $errors[] = "{$relative}:" . ($bi + 1) . ': blank line splits the leading `use` block'; + } + $i = $j; + } else { + break; + } + } + + return $errors; + } + + /** @param list<string> $lines */ + private function skipPreamble(array $lines): ?int + { + foreach ($lines as $idx => $raw) { + $stripped = trim($raw); + if (preg_match(self::USE_START_RE, $stripped)) { + return $idx; + } + if ($stripped === '' || str_starts_with($stripped, '//') || str_starts_with($stripped, '#![') || str_starts_with($stripped, '#[')) { + continue; + } + + return null; + } + + return null; + } + + /** @param list<string> $lines */ + private function consumeUseStatement(array $lines, int $startIdx): int + { + $braceDepth = 0; + $i = $startIdx; + $count = count($lines); + + while ($i < $count) { + $line = $lines[$i]; + $braceDepth += substr_count($line, '{') - substr_count($line, '}'); + $done = $braceDepth <= 0 && str_ends_with(rtrim($line), ';'); + $i++; + if ($done) { + return $i; + } + } + + return $i; + } +} |
