Skip to content
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

Add early file filtering to FileProvider::getFilesInDir() #7201

Merged
merged 3 commits into from Dec 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -316,9 +316,7 @@ public function __construct(
);

foreach ($file_paths as $file_path) {
if ($this->config->isInProjectDirs($file_path)) {
$this->addProjectFile($file_path);
}
$this->addProjectFile($file_path);
}
}

Expand All @@ -330,9 +328,7 @@ public function __construct(
);

foreach ($file_paths as $file_path) {
if ($this->config->isInExtraDirs($file_path)) {
$this->addExtraFile($file_path);
}
$this->addExtraFile($file_path);
}
}

Expand Down Expand Up @@ -1057,20 +1053,18 @@ public function checkDir(string $dir_name): void
private function checkDirWithConfig(string $dir_name, Config $config, bool $allow_non_project_files = false): void
{
$file_extensions = $config->getFileExtensions();
$directory_filter = $allow_non_project_files ? null : [$this->config, 'isInProjectDirs'];
$filter = $allow_non_project_files ? null : [$this->config, 'isInProjectDirs'];

$file_paths = $this->file_provider->getFilesInDir(
$dir_name,
$file_extensions,
$directory_filter
$filter
);

$files_to_scan = [];

foreach ($file_paths as $file_path) {
if ($allow_non_project_files || $config->isInProjectDirs($file_path)) {
$files_to_scan[$file_path] = $file_path;
}
$files_to_scan[$file_path] = $file_path;
}

$this->codebase->addFilesToAnalyze($files_to_scan);
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Provider/FakeFileProvider.php
Expand Up @@ -57,13 +57,13 @@ public function registerFile(string $file_path, string $file_contents): void

/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $directory_filter
* @param null|callable(string):bool $filter
*
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions, callable $directory_filter = null): array
public function getFilesInDir(string $dir_path, array $file_extensions, callable $filter = null): array
{
$file_paths = parent::getFilesInDir($dir_path, $file_extensions, $directory_filter);
$file_paths = parent::getFilesInDir($dir_path, $file_extensions, $filter);

foreach ($this->fake_files as $file_path => $_) {
if (strpos(strtolower($file_path), strtolower($dir_path)) === 0) {
Expand Down
16 changes: 11 additions & 5 deletions src/Psalm/Internal/Provider/FileProvider.php
Expand Up @@ -118,11 +118,11 @@ public function fileExists(string $file_path): bool

/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $directory_filter
* @param null|callable(string):bool $filter
*
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions, callable $directory_filter = null): array
public function getFilesInDir(string $dir_path, array $file_extensions, callable $filter = null): array
{
$file_paths = [];

Expand All @@ -131,12 +131,18 @@ public function getFilesInDir(string $dir_path, array $file_extensions, callable
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
);

if ($directory_filter !== null) {
if ($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);
function (string $current, $_, RecursiveIterator $iterator) use ($filter): bool {
if ($iterator->hasChildren()) {
$path = $current . DIRECTORY_SEPARATOR;
} else {
$path = $current;
}

return $filter($path);
}
);
}
Expand Down