Skip to content

Commit 50a37c7

Browse files
authored
Merge pull request #10 from smeghead/feature/error-code
fix: Fix command execution to properly return error codes when failur…
2 parents b7fee65 + 5b71c86 commit 50a37c7

File tree

8 files changed

+49
-18
lines changed

8 files changed

+49
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
### Bug fix
4+
5+
* Fixed command execution to properly return error codes when failures occur
6+
* Improved error handling in all command implementations
7+
38
## v0.0.4 (2025-03-20)
49

510
### Features

bin/php-variable-hard-usage

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
1313
use Smeghead\PhpVariableHardUsage\Command;
1414

1515
$command = new Command();
16-
$command->run($argv);
16+
$exitCode = $command->run($argv);
17+
exit($exitCode);

src/Command.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ final class Command
88
{
99
/**
1010
* @param list<string> $argv
11+
* @return int 終了コード
1112
*/
12-
public function run(array $argv): void
13+
public function run(array $argv): int
1314
{
1415
$factory = new CommandFactory();
1516
$command = $factory->createCommand($argv);
16-
$command->execute();
17+
return $command->execute();
1718
}
1819
}

src/Command/CommandInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66

77
interface CommandInterface
88
{
9-
public function execute(): void;
9+
/**
10+
* コマンドを実行する
11+
*
12+
* @return int 終了コード(成功時は0、失敗時は1以上)
13+
*/
14+
public function execute(): int;
1015
}

src/Command/HelpCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace Smeghead\PhpVariableHardUsage\Command;
66

7-
final class HelpCommand extends AbstractCommand
7+
class HelpCommand extends AbstractCommand
88
{
9-
public function execute(): void
9+
public function execute(): int
1010
{
1111
$this->printHelp();
12+
return 0;
1213
}
1314
}

src/Command/ScopesCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(array $paths)
2020
$this->paths = $paths;
2121
}
2222

23-
public function execute(): void
23+
public function execute(): int
2424
{
2525
$phpFiles = [];
2626

@@ -40,18 +40,21 @@ public function execute(): void
4040

4141
if (empty($phpFiles)) {
4242
fwrite(STDERR, "No PHP files found in specified paths\n");
43-
return;
43+
return 1;
4444
}
4545

4646
// 重複を削除
4747
$phpFiles = array_unique($phpFiles);
4848

4949
$results = [];
50+
$hasErrors = false;
51+
5052
foreach ($phpFiles as $file) {
5153
try {
5254
$content = file_get_contents($file);
5355
if ($content === false) {
5456
fwrite(STDERR, "Failed to read file: {$file}\n");
57+
$hasErrors = true;
5558
continue;
5659
}
5760

@@ -61,11 +64,19 @@ public function execute(): void
6164
$results[] = $analyzer->analyze();
6265
} catch (\Exception $e) {
6366
fwrite(STDERR, "Error analyzing {$file}: {$e->getMessage()}\n");
67+
$hasErrors = true;
6468
}
6569
}
70+
71+
if (empty($results)) {
72+
return 1;
73+
}
6674

6775
// 複数ファイルの結果をまとめて表示
6876
$this->printResults($results);
77+
78+
// エラーが一つでもあった場合は終了コードを1にする
79+
return $hasErrors ? 1 : 0;
6980
}
7081

7182
/**

src/Command/SingleCommand.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Smeghead\PhpVariableHardUsage\Analyze\VariableAnalyzer;
88
use Smeghead\PhpVariableHardUsage\Parse\VariableParser;
99

10-
final class SingleCommand extends AbstractCommand
10+
class SingleCommand extends AbstractCommand
1111
{
1212
private string $filePath;
1313

@@ -16,23 +16,29 @@ public function __construct(string $filePath)
1616
$this->filePath = $filePath;
1717
}
1818

19-
public function execute(): void
19+
public function execute(): int
2020
{
2121
if (!file_exists($this->filePath)) {
2222
fwrite(STDERR, "File not found: {$this->filePath}\n");
23-
return;
23+
return 1;
2424
}
2525

2626
$parser = new VariableParser();
2727
$content = file_get_contents($this->filePath);
2828
if ($content === false) {
2929
fwrite(STDERR, "Failed to read file: {$this->filePath}\n");
30-
return;
30+
return 1;
3131
}
3232

33-
$parseResult = $parser->parse($content);
34-
$analyzer = new VariableAnalyzer($this->filePath, $parseResult->functions);
35-
$result = $analyzer->analyze();
36-
echo $result->format();
33+
try {
34+
$parseResult = $parser->parse($content);
35+
$analyzer = new VariableAnalyzer($this->filePath, $parseResult->functions);
36+
$result = $analyzer->analyze();
37+
echo $result->format();
38+
return 0;
39+
} catch (\Exception $e) {
40+
fwrite(STDERR, "Error analyzing {$this->filePath}: {$e->getMessage()}\n");
41+
return 1;
42+
}
3743
}
3844
}

src/Command/VersionCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace Smeghead\PhpVariableHardUsage\Command;
66

7-
final class VersionCommand extends AbstractCommand
7+
class VersionCommand extends AbstractCommand
88
{
9-
public function execute(): void
9+
public function execute(): int
1010
{
1111
$this->printVersion();
12+
return 0;
1213
}
1314
}

0 commit comments

Comments
 (0)