diff --git a/src/CLI/Application.php b/src/CLI/Application.php index b5a550eb..1482fcef 100644 --- a/src/CLI/Application.php +++ b/src/CLI/Application.php @@ -5,11 +5,11 @@ namespace TomasVotruba\Lines\CLI; use Throwable; -use SebastianBergmann\FileIterator\Facade; use TomasVotruba\Lines\Analyser; use TomasVotruba\Lines\Enum\StatusCode; use TomasVotruba\Lines\Log\Json as JsonPrinter; use TomasVotruba\Lines\Log\Text as TextPrinter; +use TomasVotruba\Lines\PhpFilesFinder; final class Application { @@ -31,26 +31,22 @@ public function run(array $argv): int print PHP_EOL; - if ($arguments->help()) { + if ($arguments->displayHelp()) { $this->help(); return StatusCode::SUCCESS; } - $files = []; + $phpFilesFinder = new PhpFilesFinder(); + $filePaths = $phpFilesFinder->findInDirectories($arguments->getDirectories(), $arguments->getSuffixes(), $arguments->getExclude()); - foreach ($arguments->directories() as $directory) { - $currentFiles = (new Facade())->getFilesAsArray($directory, $arguments->suffixes(), '', $arguments->exclude()); - $files = [...$files, ...$currentFiles]; - } - - if ($files === []) { + if ($filePaths === []) { print 'No files found to scan' . PHP_EOL; return StatusCode::ERROR; } $analyser = new Analyser(); - $result = $analyser->countFiles($files); + $result = $analyser->countFiles($filePaths); $textPrinter = new TextPrinter(); $textPrinter->printResult($result); diff --git a/src/CLI/Arguments.php b/src/CLI/Arguments.php index 09367631..c81deb61 100644 --- a/src/CLI/Arguments.php +++ b/src/CLI/Arguments.php @@ -23,7 +23,7 @@ public function __construct( /** * @return string[] */ - public function directories(): array + public function getDirectories(): array { return $this->directories; } @@ -31,7 +31,7 @@ public function directories(): array /** * @return string[] */ - public function suffixes(): array + public function getSuffixes(): array { return $this->suffixes; } @@ -39,7 +39,7 @@ public function suffixes(): array /** * @return string[] */ - public function exclude(): array + public function getExclude(): array { return $this->exclude; } @@ -49,7 +49,7 @@ public function jsonLogfile(): ?string return $this->jsonLogfile; } - public function help(): bool + public function displayHelp(): bool { return $this->help; } diff --git a/src/CLI/ArgumentsBuilder.php b/src/CLI/ArgumentsBuilder.php index f9fdfc35..e70e7d25 100644 --- a/src/CLI/ArgumentsBuilder.php +++ b/src/CLI/ArgumentsBuilder.php @@ -15,17 +15,15 @@ final class ArgumentsBuilder */ public function build(array $argv): Arguments { + $cliParser = new CliParser(); + try { - $options = (new CliParser())->parse( - $argv, - '', - [ - 'suffix=', - 'exclude=', - 'log-json=', - 'help', - ] - ); + $options = $cliParser->parse($argv, '', [ + 'suffix=', + 'exclude=', + 'log-json=', + 'help', + ]); } catch (CliParserException $cliParserException) { throw new ShouldNotHappenException( $cliParserException->getMessage(), @@ -35,6 +33,7 @@ public function build(array $argv): Arguments } $directories = $options[1]; + $exclude = []; $suffixes = ['.php']; $jsonLogfile = null; @@ -44,23 +43,18 @@ public function build(array $argv): Arguments switch ($option[0]) { case '--suffix': $suffixes[] = $option[1]; - break; case '--exclude': $exclude[] = $option[1]; - break; case '--log-json': $jsonLogfile = $option[1]; - break; - case 'h': case '--help': $help = true; - break; } } @@ -69,12 +63,6 @@ public function build(array $argv): Arguments throw new ShouldNotHappenException('No directory specified'); } - return new Arguments( - $directories, - $suffixes, - $exclude, - $jsonLogfile, - $help, - ); + return new Arguments($directories, $suffixes, $exclude, $jsonLogfile, $help); } } diff --git a/src/PhpFilesFinder.php b/src/PhpFilesFinder.php new file mode 100644 index 00000000..933b58c2 --- /dev/null +++ b/src/PhpFilesFinder.php @@ -0,0 +1,29 @@ +getFilesAsArray($directory, $suffixes, '', $exclude); + $filePaths = [...$filePaths, ...$currentFilePaths]; + } + + return $filePaths; + } +}