From 09be00fdb630696ca53436d83844520410363a74 Mon Sep 17 00:00:00 2001 From: Alessandro Morelli Date: Mon, 4 May 2020 15:33:43 +0200 Subject: [PATCH] Filter VCS directories and non PHP files. --- src/Filesystem.php | 34 +++++++++++++++++++++++++--------- tests/unit/FilesystemTest.php | 6 +++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Filesystem.php b/src/Filesystem.php index c1e2233..d5f94e3 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -6,7 +6,6 @@ use FilesystemIterator; use RecursiveDirectoryIterator; -use RecursiveIteratorIterator; use RuntimeException; class Filesystem implements FilesystemInterface @@ -19,17 +18,34 @@ class Filesystem implements FilesystemInterface */ public function allFiles(string $path): array { - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) + $iterator = new \RecursiveIteratorIterator( + new \RecursiveCallbackFilterIterator( + new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), + function ($current, $key, $iterator) { + if ($iterator->hasChildren()) { + return preg_match( + '/[\._](svn|CVS|arch-params|monotone|bzr|git|hg)|CVS|_darcs/', + $current->getFilename() + ) === 0; + } + return preg_match('/\.php$/i', $current->getFileName()) === 1; + } + ) ); return iterator_to_array($iterator); } + public function filter($current, $key, $iterator) + { + + return $current->isFile() && preg_match('/.php$/i', $current->getFilename()); + } + /** * Extract the parent directory from a file path. * - * @param string $path + * @param string $path * * @return string */ @@ -41,14 +57,14 @@ public function dirname(string $path): string /** * Get the contents of a file. * - * @param string $path + * @param string $path * * @return string * @throws \RuntimeException */ public function get(string $path): string { - if (! $this->isFile($path)) { + if (!$this->isFile($path)) { throw new RuntimeException('File does not exist at path ' . $path); } @@ -58,7 +74,7 @@ public function get(string $path): string /** * Determine if the given path is a file. * - * @param string $file + * @param string $file * * @return bool */ @@ -70,8 +86,8 @@ public function isFile(string $file): bool /** * Write the contents of a file. * - * @param string $path - * @param string $contents + * @param string $path + * @param string $contents * * @return int|false */ diff --git a/tests/unit/FilesystemTest.php b/tests/unit/FilesystemTest.php index 9b9922e..a5940ca 100644 --- a/tests/unit/FilesystemTest.php +++ b/tests/unit/FilesystemTest.php @@ -117,8 +117,12 @@ public function testsAllFiles() return $file->getRealPath(); }, $actual); - $expects = [ + $other = [ codecept_data_dir('tmp-vendor/dummy/dummy-psr4/composer.json'), + codecept_data_dir('tmp-vendor/dummy/dummy-psr4/.git/dummy'), + ]; + + $expects = [ codecept_data_dir('tmp-vendor/dummy/dummy-psr4/src/DummyOne.php'), codecept_data_dir('tmp-vendor/dummy/dummy-psr4/src/DummyTwo.php'), codecept_data_dir('tmp-vendor/dummy/dummy-psr4/src/Sub/DummyOne.php'),