Skip to content

Filter VCS directories and non PHP files. #171

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RuntimeException;

class Filesystem implements FilesystemInterface
Expand All @@ -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
*/
Expand All @@ -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);
}

Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down