RecursiveRegexFinder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 57
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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