Failed Conditions
Pull Request — master (#632)
by Michael
02:44
created

RecursiveRegexFinder::getMatches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 const DIRECTORY_SEPARATOR;
12
use function sprintf;
13
14
final class RecursiveRegexFinder extends Finder implements MigrationDeepFinder
15
{
16
    /**
17
     * @return string[]
18
     */
19 92
    public function findMigrations(string $directory, ?string $namespace = null) : array
20
    {
21 92
        $dir = $this->getRealPath($directory);
22
23 90
        return $this->loadMigrations($this->getMatches($this->createIterator($dir)), $namespace);
24
    }
25
26 90
    private function createIterator(string $dir) : RegexIterator
27
    {
28 90
        return new RegexIterator(
29 90
            new RecursiveIteratorIterator(
30 90
                new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
31 90
                RecursiveIteratorIterator::LEAVES_ONLY
32
            ),
33 90
            $this->getPattern(),
34 90
            RegexIterator::GET_MATCH
35
        );
36
    }
37
38 90
    private function getPattern() : string
39
    {
40 90
        return sprintf(
41 90
            '#^.+\\%sVersion[^\\%s]{1,255}\\.php$#i',
42 90
            DIRECTORY_SEPARATOR,
43 90
            DIRECTORY_SEPARATOR
44
        );
45
    }
46
47
    /**
48
     * @return string[]
49
     */
50 90
    private function getMatches(RegexIterator $iteratorFilesMatch) : array
51
    {
52 90
        $files = [];
53 90
        foreach ($iteratorFilesMatch as $file) {
54 34
            $files[] = $file[0];
55
        }
56
57 90
        return $files;
58
    }
59
}
60