From c899a4675ca90507a420d901ef7fa26d8b285f23 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 21 Jul 2026 08:08:15 +0900 Subject: 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`. --- scripts/linters/src/Linters/ContiguousUseBlock.php | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 scripts/linters/src/Linters/ContiguousUseBlock.php (limited to 'scripts/linters/src/Linters/ContiguousUseBlock.php') 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 @@ +findSplitUseBlock($path, $relative)); + } + + return $errors; + } + + /** @return list */ + 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 $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 $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; + } +} -- cgit v1.3.1