1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Add callable $directory_filter to FileProvider::getFilesInDir()

This commit is contained in:
Tomasz Mlynski 2021-12-14 18:22:47 +01:00
parent 837075a378
commit d00c733c1f
No known key found for this signature in database
GPG Key ID: 665B4A17698A1954
2 changed files with 19 additions and 3 deletions

View File

@ -57,12 +57,13 @@ class FakeFileProvider extends FileProvider
/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $directory_filter
*
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions): array
public function getFilesInDir(string $dir_path, array $file_extensions, callable $directory_filter = null): array
{
$file_paths = parent::getFilesInDir($dir_path, $file_extensions);
$file_paths = parent::getFilesInDir($dir_path, $file_extensions, $directory_filter);
foreach ($this->fake_files as $file_path => $_) {
if (strpos(strtolower($file_path), strtolower($dir_path)) === 0) {

View File

@ -2,7 +2,9 @@
namespace Psalm\Internal\Provider;
use FilesystemIterator;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIterator;
use RecursiveIteratorIterator;
use UnexpectedValueException;
@ -14,6 +16,8 @@ use function in_array;
use function is_dir;
use function strtolower;
use const DIRECTORY_SEPARATOR;
class FileProvider
{
/**
@ -113,10 +117,11 @@ class FileProvider
/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $directory_filter
*
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions): array
public function getFilesInDir(string $dir_path, array $file_extensions, callable $directory_filter = null): array
{
$file_paths = [];
@ -125,6 +130,16 @@ class FileProvider
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
);
if ($directory_filter !== null) {
$iterator = new RecursiveCallbackFilterIterator(
$iterator,
/** @param mixed $_ */
function (string $current, $_, RecursiveIterator $iterator) use ($directory_filter): bool {
return !$iterator->hasChildren() || $directory_filter($current . DIRECTORY_SEPARATOR);
}
);
}
/** @var RecursiveDirectoryIterator */
$iterator = new RecursiveIteratorIterator($iterator);
$iterator->rewind();