From 15ddd84f3a16aadcd8927bc48c668b3e753b9414 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 21 Jun 2020 11:40:51 +0200 Subject: [PATCH] Result caching: minor optimization tweak [2] 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. --- src/Util/Cache.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Util/Cache.php b/src/Util/Cache.php index 48aa65aaa0..68abef59c4 100644 --- a/src/Util/Cache.php +++ b/src/Util/Cache.php @@ -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'