Completed
Push — master ( 86b833...956cdf )
by Jonathan
03:18
created

Finder::loadMigrationClasses()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Finder;
6
7
use InvalidArgumentException;
8
use ReflectionClass;
9
use const PHP_EOL;
10
use const SORT_STRING;
11
use function get_declared_classes;
12
use function in_array;
13
use function is_dir;
14
use function ksort;
15
use function realpath;
16
use function sprintf;
17
use function substr;
18
19
abstract class Finder implements MigrationFinder
20
{
21 40
    protected static function requireOnce(string $path) : void
22
    {
23 40
        require_once $path;
24 40
    }
25
26 109
    protected function getRealPath(string $directory) : string
27
    {
28 109
        $dir = realpath($directory);
29
30 109
        if ($dir === false || ! is_dir($dir)) {
31 4
            throw new InvalidArgumentException(sprintf(
32 4
                'Cannot load migrations from "%s" because it is not a valid directory',
33 4
                $directory
34
            ));
35
        }
36
37 105
        return $dir;
38
    }
39
40
    /**
41
     * @param string[] $files
42
     *
43
     * @return string[]
44
     */
45 105
    protected function loadMigrations(array $files, ?string $namespace) : array
46
    {
47 105
        $includedFiles = [];
48 105
        foreach ($files as $file) {
49 40
            static::requireOnce($file);
50 40
            $includedFiles[] = realpath($file);
51
        }
52
53 105
        $classes  = $this->loadMigrationClasses($includedFiles, $namespace);
54 105
        $versions = [];
55 105
        foreach ($classes as $class) {
56 40
            $version = substr($class->getShortName(), 7);
57 40
            if ($version === '0') {
58 1
                throw new InvalidArgumentException(sprintf(
59
                    'Cannot load a migrations with the name "%s" because it is a reserved number by doctrine migrations' . PHP_EOL .
60 1
                    'It\'s used to revert all migrations including the first one.',
61 1
                    $version
62
                ));
63
            }
64 39
            $versions[$version] = $class->getName();
65
        }
66
67 104
        ksort($versions, SORT_STRING);
68
69 104
        return $versions;
70
    }
71
72
    /**
73
     * Look up all declared classes and find those classes contained
74
     * in the given `$files` array.
75
     *
76
     * @param string[]    $files     The set of files that were `required`
77
     * @param string|null $namespace If not null only classes in this namespace will be returned
78
     * @return ReflectionClass[] the classes in `$files`
79
     */
80 105
    protected function loadMigrationClasses(array $files, ?string $namespace) : array
81
    {
82 105
        $classes = [];
83 105
        foreach (get_declared_classes() as $class) {
84 105
            $reflectionClass = new ReflectionClass($class);
85 105
            if (! in_array($reflectionClass->getFileName(), $files, true)) {
86 105
                continue;
87
            }
88
89 40
            if ($namespace !== null && $namespace !== $reflectionClass->getNamespaceName()) {
90 3
                continue;
91
            }
92
93 40
            $classes[] = $reflectionClass;
94
        }
95
96 105
        return $classes;
97
    }
98
}
99