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

RecursiveRegexFinder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPattern() 0 6 1
A createIterator() 0 9 1
A findMigrations() 0 5 1
A getMatches() 0 8 2
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