Failed Conditions
Push — master ( bfa2cf...3827b1 )
by Matthias
07:29 queued 27s
created

prepareBlacklistPatterns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace ComposerRequireChecker\FileLocator;
4
5
use Traversable;
6
7
final class LocateAllFilesByExtension
8
{
9 6
    public function __invoke(Traversable $directories, string $fileExtension, ?array $blacklist): Traversable
10
    {
11 6
        foreach ($directories as $directory) {
12 5
            yield from $this->filterFilesByExtension(
13 5
                new \RecursiveIteratorIterator(
14 5
                    new \RecursiveDirectoryIterator($directory),
15 5
                    \RecursiveIteratorIterator::LEAVES_ONLY
16
                ),
17 5
                $fileExtension,
18 5
                $blacklist
19
            );
20
        }
21 6
    }
22
23 5
    private function filterFilesByExtension(Traversable $files, string $fileExtension, ?array $blacklist): Traversable
24
    {
25 5
        $extensionMatcher = '/.*' . preg_quote($fileExtension) . '$/';
26
27 5
        $blacklist = $this->prepareBlacklistPatterns($blacklist);
28
29
        /* @var $file \SplFileInfo */
30 5
        foreach ($files as $file) {
31 5
            if ($blacklist && preg_match('{('.implode('|', $blacklist).')}', $file->getPathname())) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $blacklist of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
                continue;
33
            }
34
35 5
            if (!preg_match($extensionMatcher, $file->getBasename())) {
36 5
                continue;
37
            }
38
39 5
            yield $file->getPathname();
40
        }
41 5
    }
42
43 5
    private function prepareBlacklistPatterns(?array $blacklistPaths)
44
    {
45 5
        if ($blacklistPaths === null) {
0 ignored issues
show
introduced by
The condition $blacklistPaths === null is always false.
Loading history...
46 5
            return $blacklistPaths;
47
        }
48
49 4
        foreach ($blacklistPaths as &$path) {
50 4
            $path = preg_replace('{/+}', '/', preg_quote(trim(strtr($path, '\\', '/'), '/')));
51 4
            $path = str_replace('\\*\\*', '.+?', $path);
52 4
            $path = str_replace('\\*', '[^/]+?', $path);
53
        }
54
55 4
        return $blacklistPaths;
56
    }
57
}
58