Skip to content

Commit

Permalink
Result caching: minor optimization tweak [2]
Browse files Browse the repository at this point in the history
Optimize use of the iterators:
1. Skip dot files at the `RecursiveDirectoryIterator` level by setting the `FilesystemIterator::SKIP_DOTS` flag and remove the code which was doing the same in the callback.
    Note: the other two flags are the default flags used by the `RecursiveDirectoryIterator` constructor, so are needed to maintain the existing behaviour.
2. No need for the `$file->getPathname()` function call. The `$key` already contains that information, as per the default flags.
3. No need for a file-system `is_dir()` call. If it's a directory, the iterator will have children.
  • Loading branch information
jrfnl committed Jun 24, 2020
1 parent 5ee7531 commit 15ddd84
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/Util/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,25 @@ public static function load(Ruleset $ruleset, Config $config)
// hash. This ensures that core PHPCS changes will also invalidate the cache.
// Note that we ignore sniffs here, and any files that don't affect
// the outcome of the run.
$di = new \RecursiveDirectoryIterator($installDir);
$di = new \RecursiveDirectoryIterator(
$installDir,
(\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS)
);
$filter = new \RecursiveCallbackFilterIterator(
$di,
function ($file, $key, $iterator) {
// Skip hidden files.
$filename = $file->getFilename();
if (substr($filename, 0, 1) === '.') {
return false;
}

// Skip non-php files.
$filename = $file->getFilename();
if ($file->isFile() === true && substr($filename, -4) !== '.php') {
return false;
}

$filePath = Common::realpath($file->getPathname());
$filePath = Common::realpath($key);
if ($filePath === false) {
return false;
}

if (is_dir($filePath) === true
if ($iterator->hasChildren() === true
&& ($filename === 'Standards'
|| $filename === 'Exceptions'
|| $filename === 'Reports'
Expand Down

0 comments on commit 15ddd84

Please sign in to comment.