Skip to content

Commit

Permalink
add php files finder to wrpa arround string API of php file iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 26, 2023
1 parent 2b697a6 commit 74116e2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
16 changes: 6 additions & 10 deletions src/CLI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/CLI/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ public function __construct(
/**
* @return string[]
*/
public function directories(): array
public function getDirectories(): array
{
return $this->directories;
}

/**
* @return string[]
*/
public function suffixes(): array
public function getSuffixes(): array
{
return $this->suffixes;
}

/**
* @return string[]
*/
public function exclude(): array
public function getExclude(): array
{
return $this->exclude;
}
Expand All @@ -49,7 +49,7 @@ public function jsonLogfile(): ?string
return $this->jsonLogfile;
}

public function help(): bool
public function displayHelp(): bool
{
return $this->help;
}
Expand Down
32 changes: 10 additions & 22 deletions src/CLI/ArgumentsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -35,6 +33,7 @@ public function build(array $argv): Arguments
}

$directories = $options[1];

$exclude = [];
$suffixes = ['.php'];
$jsonLogfile = null;
Expand All @@ -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;
}
}
Expand All @@ -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);
}
}
29 changes: 29 additions & 0 deletions src/PhpFilesFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\Lines;

use SebastianBergmann\FileIterator\Facade;

final class PhpFilesFinder
{
/**
* @param string[] $directories
* @param string[] $suffixes
* @param string[] $exclude
* @return string[]
*/
public function findInDirectories(array $directories, array $suffixes, array $exclude): array
{
$filePaths = [];
$facade = new Facade();

foreach ($directories as $directory) {
$currentFilePaths = $facade->getFilesAsArray($directory, $suffixes, '', $exclude);
$filePaths = [...$filePaths, ...$currentFilePaths];
}

return $filePaths;
}
}

0 comments on commit 74116e2

Please sign in to comment.