Skip to content

Commit 72afe52

Browse files
committed
feat: Added subcommand support with single and scopes modes
1 parent 59e9f88 commit 72afe52

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

CHANGELOG.md

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

3+
### Features
4+
5+
* Added subcommand support with `single` and `scopes` modes
6+
* The `single` mode analyzes a single PHP file
7+
* The `scopes` mode supports analyzing multiple files and directories
8+
* Enhanced command line interface with help and version options
9+
310
## v0.0.3 (2025-03-20)
411

512
### Features

src/Analyze/VariableAnalyzer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ private function analyzeFunction(Func $function): Scope
3434
$analyzedVars = [];
3535

3636
foreach ($variableNames as $variableName) {
37-
$vars = array_filter($variables, fn($variable) => $variable->name === $variableName);
37+
// array_values でインデックスを振り直す
38+
$vars = array_values(array_filter($variables, fn($variable) => $variable->name === $variableName));
3839
$variableHardUsage = $this->calcVariableHardUsage($vars);
3940
$analyzedVars[] = new AnalyzedVariable($variableName, $variableHardUsage);
4041
}

src/Command/AbstractCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ protected function printHelp(): void
1717
{
1818
echo "Usage: php bin/php-variable-hard-usage [command] [options]\n";
1919
echo "Commands:\n";
20-
echo " single <file> Analyze a single file\n";
21-
echo " scopes <directory> Analyze PHP files in a directory\n";
20+
echo " single <file> Analyze a single file\n";
21+
echo " scopes <path1> [<path2> ...] Analyze PHP files in directories or specific files\n";
2222
echo "Options:\n";
23-
echo " --help Display help information\n";
24-
echo " --version Show the version of the tool\n";
23+
echo " --help Display help information\n";
24+
echo " --version Show the version of the tool\n";
2525
}
2626
}

src/Command/ScopesCommand.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,43 @@
99

1010
final class ScopesCommand extends AbstractCommand
1111
{
12-
private string $directory;
12+
/** @var list<string> */
13+
private array $paths;
1314

14-
public function __construct(string $directory)
15+
/**
16+
* @param list<string> $paths ディレクトリまたはファイルのパスリスト
17+
*/
18+
public function __construct(array $paths)
1519
{
16-
$this->directory = $directory;
20+
$this->paths = $paths;
1721
}
1822

1923
public function execute(): void
2024
{
21-
if (!is_dir($this->directory)) {
22-
fwrite(STDERR, "Directory not found: {$this->directory}\n");
23-
return;
25+
$phpFiles = [];
26+
27+
// 各パスを処理
28+
foreach ($this->paths as $path) {
29+
if (is_dir($path)) {
30+
// ディレクトリの場合は再帰的にPHPファイルを収集
31+
$dirFiles = $this->findPhpFiles($path);
32+
$phpFiles = array_merge($phpFiles, $dirFiles);
33+
} elseif (is_file($path) && pathinfo($path, PATHINFO_EXTENSION) === 'php') {
34+
// 単一のPHPファイルの場合
35+
$phpFiles[] = $path;
36+
} else {
37+
fwrite(STDERR, "Invalid path: {$path}\n");
38+
}
2439
}
2540

26-
$phpFiles = $this->findPhpFiles($this->directory);
2741
if (empty($phpFiles)) {
28-
fwrite(STDERR, "No PHP files found in: {$this->directory}\n");
42+
fwrite(STDERR, "No PHP files found in specified paths\n");
2943
return;
3044
}
3145

46+
// 重複を削除
47+
$phpFiles = array_unique($phpFiles);
48+
3249
$results = [];
3350
foreach ($phpFiles as $file) {
3451
try {

src/CommandFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public function createCommand(array $argv): CommandInterface
4141

4242
if ($arg === 'scopes') {
4343
if (count($argv) < 3) {
44-
fwrite(STDERR, "Usage: php bin/php-variable-hard-usage scopes <directory>\n");
44+
fwrite(STDERR, "Usage: php bin/php-variable-hard-usage scopes <path1> [<path2> ...]\n");
4545
return new HelpCommand();
4646
}
47-
return new ScopesCommand($argv[2]);
47+
// 複数のパスを渡す
48+
return new ScopesCommand(array_slice($argv, 2));
4849
}
4950

5051
// 後方互換性のため、コマンドが指定されていない場合は単一ファイルモードとして扱う

0 commit comments

Comments
 (0)