Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion bin/php-variable-hard-usage
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
use Smeghead\PhpVariableHardUsage\Command;

$command = new Command();
$command->run($argv);
$exitCode = $command->run($argv);
exit($exitCode);
5 changes: 3 additions & 2 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ final class Command
{
/**
* @param list<string> $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();
}
}
7 changes: 6 additions & 1 deletion src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

interface CommandInterface
{
public function execute(): void;
/**
* コマンドを実行する
*
* @return int 終了コード(成功時は0、失敗時は1以上)
*/
public function execute(): int;
}
5 changes: 3 additions & 2 deletions src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
15 changes: 13 additions & 2 deletions src/Command/ScopesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(array $paths)
$this->paths = $paths;
}

public function execute(): void
public function execute(): int
{
$phpFiles = [];

Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand Down
22 changes: 14 additions & 8 deletions src/Command/SingleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
}
5 changes: 3 additions & 2 deletions src/Command/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}