Completed
Push — master ( 961aa4...4462c2 )
by Matthias
03:43
created

LocateAllFilesByExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 2
A filterFilesByExtension() 0 13 3
1
<?php
2
3
namespace ComposerRequireChecker\FileLocator;
4
5
use Traversable;
6
7
final class LocateAllFilesByExtension
8
{
9
    public function __invoke(Traversable $directories, string $fileExtension) : Traversable
10
    {
11
        foreach ($directories as $directory) {
12
            yield from $this->filterFilesByExtension(
13
                new \RecursiveIteratorIterator(
14
                    new \RecursiveDirectoryIterator($directory),
15
                    \RecursiveIteratorIterator::LEAVES_ONLY
16
                ),
17
                $fileExtension
18
            );
19
        }
20
    }
21
22
    private function filterFilesByExtension(Traversable $files, string $fileExtension) : Traversable
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
23
    {
24
        $extensionMatcher = '/.*' . preg_quote($fileExtension) . '$/';
25
26
        /* @var $file \SplFileInfo */
27
        foreach ($files as $file) {
28
            if (! preg_match($extensionMatcher, $file->getBasename())) {
29
                continue;
30
            }
31
32
            yield $file->getPathname();
33
        }
34
    }
35
}
36