Skip to content

Added support for multiple files #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Short | Long | Description
-h | --help | Display help message.
-x | --exclude=EXCLUDE | Files and directories to exclude.
-d | --directory=DIRECTORY | Directory to scan. [default: "./"]
-f | --files=FILES | Files to scan.
none | --skip-classes | Don't check classes for docblocks.
none | --skip-methods | Don't check methods for docblocks.
none | --skip-signatures | Don't check docblocks against method signatures.
Expand Down
37 changes: 36 additions & 1 deletion src/CheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CheckerCommand extends Command
protected array $warnings = [];

protected array $exclude = [];

protected array $files = [];

protected OutputInterface $output;

Expand All @@ -46,6 +48,7 @@ protected function configure(): void
->setDescription('Check PHP files within a directory for appropriate use of Docblocks.')
->addOption('exclude', 'x', InputOption::VALUE_REQUIRED, 'Files and directories to exclude.', null)
->addOption('directory', 'd', InputOption::VALUE_REQUIRED, 'Directory to scan.', './')
->addOption('files', 'f', InputOption::VALUE_REQUIRED, 'Files to scan.', null)
->addOption('skip-classes', null, InputOption::VALUE_NONE, 'Don\'t check classes for docblocks.')
->addOption('skip-methods', null, InputOption::VALUE_NONE, 'Don\'t check methods for docblocks.')
->addOption('skip-signatures', null, InputOption::VALUE_NONE, 'Don\'t check docblocks against method signatures.')
Expand All @@ -68,6 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$exclude = $input->getOption('exclude');
$json = $input->getOption('json');
$this->basePath = $input->getOption('directory');
$files = $input->getOption('files');
$this->verbose = !$json;
$this->output = $output;
$failOnWarnings = $input->getOption('fail-on-warnings');
Expand All @@ -88,6 +92,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!\is_null($exclude)) {
$this->exclude = \array_map('trim', \explode(',', $exclude));
}

// Set up files:
if (!\is_null($files)) {
$this->files = \array_map('trim', \explode(',', $files));
}

// Check base path ends with a slash:
if (\substr($this->basePath, -1) != '/') {
Expand All @@ -96,7 +105,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Get files to check:
$files = [];
$this->processDirectory('', $files);
if (count($this->files) > 0) {
$this->processFiles('', $this->files, $files);
} else {
$this->processDirectory('', $files);
}

// Check files:
$filesPerLine = (int)$input->getOption('files-per-line');
Expand Down Expand Up @@ -232,6 +245,28 @@ protected function processDirectory(string $path = '', array &$workList = []): v
}
}
}

/**
* Iterate through the files and check them out
*
* @param string $path
* @param string[] $files
* @param string[] $workList
*/
protected function processFiles(string $path = '', array $files = [], array &$workList = []): void
{
foreach ($files as $item) {
$itemPath = $path . $item;

if (\in_array($itemPath, $this->exclude)) {
continue;
}

if (is_file($itemPath) && pathinfo($itemPath)["extension"] == 'php') {
$workList[] = $itemPath;
}
}
}

/**
* Check a specific PHP file for errors.
Expand Down