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

LocateAllFilesByExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 23
c 1
b 0
f 0
dl 0
loc 49
ccs 26
cts 27
cp 0.963
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareBlacklistPatterns() 0 13 3
A __invoke() 0 10 2
A filterFilesByExtension() 0 17 5
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