44
55namespace Smeghead \PhpVariableHardUsage \Command ;
66
7+ use Smeghead \PhpVariableHardUsage \Analyze \AnalysisResult ;
78use Smeghead \PhpVariableHardUsage \Analyze \VariableAnalyzer ;
9+ use Smeghead \PhpVariableHardUsage \Parse \Exception \ParseFailedException ;
810use Smeghead \PhpVariableHardUsage \Parse \VariableParser ;
911
1012final class ScopesCommand extends AbstractCommand
@@ -20,12 +22,16 @@ public function __construct(array $paths)
2022 $ this ->paths = $ paths ;
2123 }
2224
23- public function execute (): int
25+ /**
26+ * @param list<string> $paths
27+ * @return list<string>
28+ */
29+ private function pickupPhpFiles (array $ paths ): array
2430 {
2531 $ phpFiles = [];
2632
2733 // 各パスを処理
28- foreach ($ this -> paths as $ path ) {
34+ foreach ($ paths as $ path ) {
2935 if (is_dir ($ path )) {
3036 // ディレクトリの場合は再帰的にPHPファイルを収集
3137 $ dirFiles = $ this ->findPhpFiles ($ path );
@@ -38,6 +44,26 @@ public function execute(): int
3844 }
3945 }
4046
47+ return $ phpFiles ;
48+ }
49+
50+ private function analyzeFile (string $ file ): AnalysisResult
51+ {
52+ $ parser = new VariableParser ();
53+ $ content = file_get_contents ($ file );
54+ if ($ content === false ) {
55+ throw new ParseFailedException ("Failed to read file: {$ file }" );
56+ }
57+
58+ $ parseResult = $ parser ->parse ($ content );
59+ $ analyzer = new VariableAnalyzer ($ file , $ parseResult ->functions );
60+ return $ analyzer ->analyze ();
61+ }
62+
63+ public function execute (): int
64+ {
65+ $ phpFiles = $ this ->pickupPhpFiles ($ this ->paths );
66+
4167 if (empty ($ phpFiles )) {
4268 fwrite (STDERR , "No PHP files found in specified paths \n" );
4369 return 1 ;
@@ -51,17 +77,7 @@ public function execute(): int
5177
5278 foreach ($ phpFiles as $ file ) {
5379 try {
54- $ content = file_get_contents ($ file );
55- if ($ content === false ) {
56- fwrite (STDERR , "Failed to read file: {$ file }\n" );
57- $ hasErrors = true ;
58- continue ;
59- }
60-
61- $ parser = new VariableParser ();
62- $ parseResult = $ parser ->parse ($ content );
63- $ analyzer = new VariableAnalyzer ($ file , $ parseResult ->functions );
64- $ results [] = $ analyzer ->analyze ();
80+ $ results [] = $ this ->analyzeFile ($ file );
6581 } catch (\Exception $ e ) {
6682 fwrite (STDERR , "Error analyzing {$ file }: {$ e ->getMessage ()}\n" );
6783 $ hasErrors = true ;
0 commit comments