Skip to content

Commit

Permalink
[FEATURE] #82
Browse files Browse the repository at this point in the history
Ignore coverage for the last line of the globstar function, since it's only used if the glob function returns an error.
Added a test to validate the behaviour of the globstar function when passing non existant glob pattern.
Fixed code using php-cs-fixer.
Fixed error reported by phpstan.
  • Loading branch information
Nicolas D'Amours authored and sebastianbergmann committed Aug 27, 2024
1 parent 320aff7 commit 6ac080a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
28 changes: 19 additions & 9 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
use function array_filter;
use function array_map;
use function array_merge;
use function array_unique;
use function array_values;
use function glob;
use function is_dir;
use function is_string;
use function realpath;
use function sort;
use function stripos;
use function substr;
use AppendIterator;
use FilesystemIterator;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -106,39 +110,45 @@ private function resolveWildcards(array $paths): array

/**
* @see https://gist.github.com/funkjedi/3feee27d873ae2297b8e2370a7082aad
* @param string $pattern
* @return list<non-empty-string>
*
* @return list<string>
*/
private function globstar(string $pattern) {
private function globstar(string $pattern)
{
if (stripos($pattern, '**') === false) {
$files = glob($pattern, GLOB_ONLYDIR);
} else {
$position = stripos($pattern, '**');
$position = stripos($pattern, '**');
$rootPattern = substr($pattern, 0, $position - 1);
$restPattern = substr($pattern, $position + 2);

$patterns = [$rootPattern.$restPattern];
$patterns = [$rootPattern . $restPattern];
$rootPattern .= '/*';

while($dirs = glob($rootPattern, GLOB_ONLYDIR)) {
while ($dirs = glob($rootPattern, GLOB_ONLYDIR)) {
$rootPattern .= '/*';
foreach($dirs as $dir) {

foreach ($dirs as $dir) {
$patterns[] = $dir . $restPattern;
}
}

$files = [];
foreach($patterns as $pat) {

foreach ($patterns as $pat) {
$files = array_merge($files, $this->globstar($pat));
}
}

if($files !== false) {
if ($files !== false) {
$files = array_unique($files);
sort($files);

return $files;
}

// @codeCoverageIgnoreStart
return [];
// @codeCoverageIgnoreEnd
}
}
10 changes: 9 additions & 1 deletion tests/fixture/a/c/d/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
10 changes: 9 additions & 1 deletion tests/fixture/b/e/g/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
10 changes: 9 additions & 1 deletion tests/fixture/b/e/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
10 changes: 9 additions & 1 deletion tests/fixture/b/f/h/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
10 changes: 8 additions & 2 deletions tests/unit/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianBergmann\FileIterator;

use function realpath;
Expand Down Expand Up @@ -148,7 +147,14 @@ public static function provider(): array
__DIR__ . '/../fixture/**/i',
'',
'',
[]
[],
],
'invalid path, filter prefix: no, filter suffix: no, excludes: none' => [
[],
__DIR__ . '/../fixture/**/this/path/does/not/exists',
'',
'',
[],
],
];
}
Expand Down

0 comments on commit 6ac080a

Please sign in to comment.