File tree Expand file tree Collapse file tree 5 files changed +41
-15
lines changed Expand file tree Collapse file tree 5 files changed +41
-15
lines changed Original file line number Diff line number Diff line change 1
1
# CHANGELOG
2
2
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
+
3
10
## v0.0.3 (2025-03-20)
4
11
5
12
### Features
Original file line number Diff line number Diff line change @@ -34,7 +34,8 @@ private function analyzeFunction(Func $function): Scope
34
34
$ analyzedVars = [];
35
35
36
36
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 ));
38
39
$ variableHardUsage = $ this ->calcVariableHardUsage ($ vars );
39
40
$ analyzedVars [] = new AnalyzedVariable ($ variableName , $ variableHardUsage );
40
41
}
Original file line number Diff line number Diff line change @@ -17,10 +17,10 @@ protected function printHelp(): void
17
17
{
18
18
echo "Usage: php bin/php-variable-hard-usage [command] [options] \n" ;
19
19
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" ;
22
22
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" ;
25
25
}
26
26
}
Original file line number Diff line number Diff line change 9
9
10
10
final class ScopesCommand extends AbstractCommand
11
11
{
12
- private string $ directory ;
12
+ /** @var list<string> */
13
+ private array $ paths ;
13
14
14
- public function __construct (string $ directory )
15
+ /**
16
+ * @param list<string> $paths ディレクトリまたはファイルのパスリスト
17
+ */
18
+ public function __construct (array $ paths )
15
19
{
16
- $ this ->directory = $ directory ;
20
+ $ this ->paths = $ paths ;
17
21
}
18
22
19
23
public function execute (): void
20
24
{
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
+ }
24
39
}
25
40
26
- $ phpFiles = $ this ->findPhpFiles ($ this ->directory );
27
41
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" );
29
43
return ;
30
44
}
31
45
46
+ // 重複を削除
47
+ $ phpFiles = array_unique ($ phpFiles );
48
+
32
49
$ results = [];
33
50
foreach ($ phpFiles as $ file ) {
34
51
try {
Original file line number Diff line number Diff line change @@ -41,10 +41,11 @@ public function createCommand(array $argv): CommandInterface
41
41
42
42
if ($ arg === 'scopes ' ) {
43
43
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" );
45
45
return new HelpCommand ();
46
46
}
47
- return new ScopesCommand ($ argv [2 ]);
47
+ // 複数のパスを渡す
48
+ return new ScopesCommand (array_slice ($ argv , 2 ));
48
49
}
49
50
50
51
// 後方互換性のため、コマンドが指定されていない場合は単一ファイルモードとして扱う
You can’t perform that action at this time.
0 commit comments