Skip to content

Commit

Permalink
add --file-extension option
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 21, 2023
1 parent 9b724fb commit e2b2782
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
"require": {
"php": ">=8.1",
"illuminate/container": "^10.20",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.17",
"symfony/console": "^6.3",
"webmozart/assert": "^1.11",
"nette/utils": "^3.2"
"symfony/finder": "^6.3",
"webmozart/assert": "^1.11"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.10.25",
"phpunit/phpunit": "^10.3",
Expand Down
13 changes: 12 additions & 1 deletion src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ protected function configure(): void
'Class suffix that should be skipped'
);

$this->addOption(
'file-extension',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'File extensions to check',
['php']
);

$this->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON');
}

Expand All @@ -72,7 +80,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$isJson = (bool) $input->getOption('json');

$phpFilePaths = $this->phpFilesFinder->findPhpFiles($paths);
/** @var string[] $fileExtensions */
$fileExtensions = (array) $input->getOption('file-extension');

$phpFilePaths = $this->phpFilesFinder->findPhpFiles($paths, $fileExtensions);

if (! $isJson) {
$this->symfonyStyle->progressStart(count($phpFilePaths));
Expand Down
49 changes: 15 additions & 34 deletions src/Finder/PhpFilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace TomasVotruba\ClassLeak\Finder;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Webmozart\Assert\Assert;

/**
Expand All @@ -13,51 +15,30 @@ final class PhpFilesFinder
{
/**
* @param string[] $paths
* @param string[] $fileExtensions
* @return string[]
*/
public function findPhpFiles(array $paths): array
public function findPhpFiles(array $paths, array $fileExtensions): array
{
Assert::allFileExists($paths);
Assert::allString($fileExtensions);

// fallback to config paths
$filePaths = [];

foreach ($paths as $path) {
if (is_file($path)) {
$filePaths[] = $path;
} else {
$currentFilePaths = $this->findFilesUsingGlob($path);
$filePaths = [...$filePaths, ...$currentFilePaths];
}
}

sort($filePaths);

Assert::allString($filePaths);
Assert::allFileExists($filePaths);

return $filePaths;
}

/**
* @return string[]
*/
private function findFilesUsingGlob(string $directory): array
{
// Search for php files in the current directory
/** @var string[] $phpFiles */
$phpFiles = glob($directory . '/*.php');
$currentFileFinder = Finder::create()->files()
->in($paths)
->sortByName();

// recursively search in subdirectories

/** @var string[] $subdirectories */
$subdirectories = glob($directory . '/*', GLOB_ONLYDIR);
foreach ($fileExtensions as $fileExtension) {
$currentFileFinder->name('*.' . $fileExtension);
}

foreach ($subdirectories as $subdirectory) {
// Merge the results from subdirectories
$phpFiles = array_merge($phpFiles, $this->findFilesUsingGlob($subdirectory));
foreach ($currentFileFinder as $fileInfo) {
/** @var SplFileInfo $fileInfo */
$filePaths[] = $fileInfo->getRealPath();
}

return $phpFiles;
return $filePaths;
}
}
5 changes: 5 additions & 0 deletions tests/Finder/Fixture/core_file.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

// some content
5 changes: 4 additions & 1 deletion tests/Finder/PhpFilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ protected function setUp(): void

public function test(): void
{
$phpFiles = $this->phpFilesFinder->findPhpFiles([__DIR__ . '/Fixture']);
$phpFiles = $this->phpFilesFinder->findPhpFiles([__DIR__ . '/Fixture'], ['php', 'phtml']);
$this->assertCount(4, $phpFiles);

$phpFiles = $this->phpFilesFinder->findPhpFiles([__DIR__ . '/Fixture'], ['php']);
$this->assertCount(3, $phpFiles);
}
}

0 comments on commit e2b2782

Please sign in to comment.