FilesFromDir::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace FlexFqcnFinder\Repository;
5
6
use DirectoryIterator;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use RegexIterator;
10
use Traversable;
11
12
class FilesFromDir implements FileRepositoryInterface
13
{
14
    const PHP_FILES = '/\.php$/';
15
16
    /**
17
     * @var RegexIterator
18
     */
19
    protected $iterator;
20
21
    public function __construct(string $directory, bool $recursive = true, string $pattern = self::PHP_FILES)
22
    {
23
        $directoryIterator =
24
            $recursive ?
25
            new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) :
26
            new DirectoryIterator($directory);
27
28
        $this->iterator = new RegexIterator($directoryIterator, $pattern);
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function getFiles(): Traversable
35
    {
36
        foreach ($this->iterator as $path) {
37
            if ($path->isFile()) {
38
                yield realpath($path->getPathname());
39
            }
40
        }
41
    }
42
}
43