Skip to content

Commit

Permalink
add --allow-vendor and exclude vendor by default
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 2, 2024
1 parent 1f3653a commit 6c3f26c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ Or in a json format:
}
}
```


## Vendor file scanning

This tool use case is to measure your code, not the 3rd party libraries. That's why it ignores `/vendor` directory by default to avoid huge false positives.

If you want to measure vendor files too, use `--allow-vendor` option:

```bash
vendor/bin/lines measure vendor/rector/rector --allow-vendor
```
4 changes: 3 additions & 1 deletion src/Console/Command/MeasureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function configure(): void

$this->addOption('json', null, InputOption::VALUE_NONE, 'Output in JSON format');
$this->addOption('short', null, InputOption::VALUE_NONE, 'Print short metrics only');
$this->addOption('allow-vendor', null, InputOption::VALUE_NONE, 'Allow /vendor directory to be scanned');
}

/**
Expand All @@ -54,8 +55,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$excludes = (array) $input->getOption('exclude');
$isJson = (bool) $input->getOption('json');
$isShort = (bool) $input->getOption('short');
$allowVendor = (bool) $input->getOption('allow-vendor');

$filePaths = $this->phpFilesFinder->findInDirectories($paths, $excludes);
$filePaths = $this->phpFilesFinder->findInDirectories($paths, $excludes, $allowVendor);
if ($filePaths === []) {
$output->writeln('<error>No files found to scan</error>');
return Command::FAILURE;
Expand Down
7 changes: 6 additions & 1 deletion src/Finder/PhpFilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class PhpFilesFinder
* @param string[] $exclude
* @return string[]
*/
public function findInDirectories(array $paths, array $exclude = []): array
public function findInDirectories(array $paths, array $exclude = [], bool $allowVendor = false): array
{
Assert::allFileExists($paths);

Expand All @@ -38,6 +38,11 @@ public function findInDirectories(array $paths, array $exclude = []): array
->notPath('tomasvotruba/lines')
->exclude($exclude);

if ($allowVendor === false) {
// skip vendor directory, as we often need the full source code
$phpFilesFinder->notPath('vendor');
}

foreach ($phpFilesFinder->getIterator() as $fileInfo) {
$filePaths[] = $fileInfo->getRealPath();
}
Expand Down

0 comments on commit 6c3f26c

Please sign in to comment.