From 5b71c8668a690d5a3e063ad41e8f5b0b18a73fb3 Mon Sep 17 00:00:00 2001 From: smeghead Date: Fri, 21 Mar 2025 01:00:34 +0900 Subject: [PATCH] fix: Fix command execution to properly return error codes when failures occur --- CHANGELOG.md | 5 +++++ bin/php-variable-hard-usage | 3 ++- src/Command.php | 5 +++-- src/Command/CommandInterface.php | 7 ++++++- src/Command/HelpCommand.php | 5 +++-- src/Command/ScopesCommand.php | 15 +++++++++++++-- src/Command/SingleCommand.php | 22 ++++++++++++++-------- src/Command/VersionCommand.php | 5 +++-- 8 files changed, 49 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 996a95e..c88fa6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +### Bug fix + + * Fixed command execution to properly return error codes when failures occur + * Improved error handling in all command implementations + ## v0.0.4 (2025-03-20) ### Features diff --git a/bin/php-variable-hard-usage b/bin/php-variable-hard-usage index 79cb0a9..9101408 100755 --- a/bin/php-variable-hard-usage +++ b/bin/php-variable-hard-usage @@ -13,4 +13,5 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php use Smeghead\PhpVariableHardUsage\Command; $command = new Command(); -$command->run($argv); \ No newline at end of file +$exitCode = $command->run($argv); +exit($exitCode); \ No newline at end of file diff --git a/src/Command.php b/src/Command.php index 3d37016..0796281 100644 --- a/src/Command.php +++ b/src/Command.php @@ -8,11 +8,12 @@ final class Command { /** * @param list $argv + * @return int 終了コード */ - public function run(array $argv): void + public function run(array $argv): int { $factory = new CommandFactory(); $command = $factory->createCommand($argv); - $command->execute(); + return $command->execute(); } } \ No newline at end of file diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php index 3c908d8..413146c 100644 --- a/src/Command/CommandInterface.php +++ b/src/Command/CommandInterface.php @@ -6,5 +6,10 @@ interface CommandInterface { - public function execute(): void; + /** + * コマンドを実行する + * + * @return int 終了コード(成功時は0、失敗時は1以上) + */ + public function execute(): int; } \ No newline at end of file diff --git a/src/Command/HelpCommand.php b/src/Command/HelpCommand.php index 842aa15..5ad0b28 100644 --- a/src/Command/HelpCommand.php +++ b/src/Command/HelpCommand.php @@ -4,10 +4,11 @@ namespace Smeghead\PhpVariableHardUsage\Command; -final class HelpCommand extends AbstractCommand +class HelpCommand extends AbstractCommand { - public function execute(): void + public function execute(): int { $this->printHelp(); + return 0; } } \ No newline at end of file diff --git a/src/Command/ScopesCommand.php b/src/Command/ScopesCommand.php index 503acb4..8d285fe 100644 --- a/src/Command/ScopesCommand.php +++ b/src/Command/ScopesCommand.php @@ -20,7 +20,7 @@ public function __construct(array $paths) $this->paths = $paths; } - public function execute(): void + public function execute(): int { $phpFiles = []; @@ -40,18 +40,21 @@ public function execute(): void if (empty($phpFiles)) { fwrite(STDERR, "No PHP files found in specified paths\n"); - return; + return 1; } // 重複を削除 $phpFiles = array_unique($phpFiles); $results = []; + $hasErrors = false; + foreach ($phpFiles as $file) { try { $content = file_get_contents($file); if ($content === false) { fwrite(STDERR, "Failed to read file: {$file}\n"); + $hasErrors = true; continue; } @@ -61,11 +64,19 @@ public function execute(): void $results[] = $analyzer->analyze(); } catch (\Exception $e) { fwrite(STDERR, "Error analyzing {$file}: {$e->getMessage()}\n"); + $hasErrors = true; } } + + if (empty($results)) { + return 1; + } // 複数ファイルの結果をまとめて表示 $this->printResults($results); + + // エラーが一つでもあった場合は終了コードを1にする + return $hasErrors ? 1 : 0; } /** diff --git a/src/Command/SingleCommand.php b/src/Command/SingleCommand.php index 0219743..73f5fbe 100644 --- a/src/Command/SingleCommand.php +++ b/src/Command/SingleCommand.php @@ -7,7 +7,7 @@ use Smeghead\PhpVariableHardUsage\Analyze\VariableAnalyzer; use Smeghead\PhpVariableHardUsage\Parse\VariableParser; -final class SingleCommand extends AbstractCommand +class SingleCommand extends AbstractCommand { private string $filePath; @@ -16,23 +16,29 @@ public function __construct(string $filePath) $this->filePath = $filePath; } - public function execute(): void + public function execute(): int { if (!file_exists($this->filePath)) { fwrite(STDERR, "File not found: {$this->filePath}\n"); - return; + return 1; } $parser = new VariableParser(); $content = file_get_contents($this->filePath); if ($content === false) { fwrite(STDERR, "Failed to read file: {$this->filePath}\n"); - return; + return 1; } - $parseResult = $parser->parse($content); - $analyzer = new VariableAnalyzer($this->filePath, $parseResult->functions); - $result = $analyzer->analyze(); - echo $result->format(); + try { + $parseResult = $parser->parse($content); + $analyzer = new VariableAnalyzer($this->filePath, $parseResult->functions); + $result = $analyzer->analyze(); + echo $result->format(); + return 0; + } catch (\Exception $e) { + fwrite(STDERR, "Error analyzing {$this->filePath}: {$e->getMessage()}\n"); + return 1; + } } } \ No newline at end of file diff --git a/src/Command/VersionCommand.php b/src/Command/VersionCommand.php index 6a0643d..a68f04e 100644 --- a/src/Command/VersionCommand.php +++ b/src/Command/VersionCommand.php @@ -4,10 +4,11 @@ namespace Smeghead\PhpVariableHardUsage\Command; -final class VersionCommand extends AbstractCommand +class VersionCommand extends AbstractCommand { - public function execute(): void + public function execute(): int { $this->printVersion(); + return 0; } } \ No newline at end of file