RecursiveRegexFinder::findMigrations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Finder;
6
7
use FilesystemIterator;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use RegexIterator;
11
use function sprintf;
12
use const DIRECTORY_SEPARATOR;
13
14
/**
15
 * The RecursiveRegexFinder class recursively searches the given directory for migrations.
16
 */
17
final class RecursiveRegexFinder extends Finder
18
{
19
    /** @var string */
20
    private $pattern;
21
22 39
    public function __construct(?string $pattern = null)
23
    {
24 39
        $this->pattern = $pattern ?? sprintf(
25 10
            '#^.+\\%s[^\\%s]+\\.php$#i',
26 10
            DIRECTORY_SEPARATOR,
27 10
            DIRECTORY_SEPARATOR
28
        );
29 39
    }
30
31
    /**
32
     * @return string[]
33
     */
34 14
    public function findMigrations(string $directory, ?string $namespace = null) : array
35
    {
36 14
        $dir = $this->getRealPath($directory);
37
38 12
        return $this->loadMigrations(
39 12
            $this->getMatches($this->createIterator($dir)),
40 12
            $namespace
41
        );
42
    }
43
44 12
    private function createIterator(string $dir) : RegexIterator
45
    {
46 12
        return new RegexIterator(
47 12
            new RecursiveIteratorIterator(
48 12
                new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
49 12
                RecursiveIteratorIterator::LEAVES_ONLY
50
            ),
51 12
            $this->getPattern(),
52 12
            RegexIterator::GET_MATCH
53
        );
54
    }
55
56 12
    private function getPattern() : string
57
    {
58 12
        return $this->pattern;
59
    }
60
61
    /**
62
     * @return string[]
63
     */
64 12
    private function getMatches(RegexIterator $iteratorFilesMatch) : array
65
    {
66 12
        $files = [];
67 12
        foreach ($iteratorFilesMatch as $file) {
68 11
            $files[] = $file[0];
69
        }
70
71 12
        return $files;
72
    }
73
}
74