Failed Conditions
Pull Request — master (#617)
by Jonathan
03:01
created

Finder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A requireOnce() 0 3 1
A getRealPath() 0 12 3
A loadMigrations() 0 23 3
A getFileSortCallback() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Migrations\Finder;
6
7
use InvalidArgumentException;
8
use const PHP_EOL;
9
use function basename;
10
use function is_dir;
11
use function realpath;
12
use function sprintf;
13
use function substr;
14
use function uasort;
15
16
abstract class Finder implements MigrationFinder
17
{
18 35
    protected static function requireOnce(string $path) : void
19
    {
20 35
        require_once $path;
21 35
    }
22
23 95
    protected function getRealPath(string $directory) : string
24
    {
25 95
        $dir = realpath($directory);
26
27 95
        if ($dir === false || ! is_dir($dir)) {
28 4
            throw new InvalidArgumentException(sprintf(
29 4
                'Cannot load migrations from "%s" because it is not a valid directory',
30 4
                $directory
31
            ));
32
        }
33
34 91
        return $dir;
35
    }
36
37
    /**
38
     * @param string[] $files
39
     *
40
     * @return string[]
41
     */
42 91
    protected function loadMigrations(array $files, ?string $namespace) : array
43
    {
44 91
        $migrations = [];
45
46 91
        uasort($files, $this->getFileSortCallback());
47
48 91
        foreach ($files as $file) {
49 35
            static::requireOnce($file);
50 35
            $className = basename($file, '.php');
51 35
            $version   = (string) substr($className, 7);
52
53 35
            if ($version === '0') {
54 1
                throw new InvalidArgumentException(sprintf(
55
                    'Cannot load a migrations with the name "%s" because it is a reserved number by doctrine migrations' . PHP_EOL .
56 1
                    'It\'s used to revert all migrations including the first one.',
57 1
                    $version
58
                ));
59
            }
60
61 34
            $migrations[$version] = sprintf('%s\\%s', $namespace, $className);
62
        }
63
64 90
        return $migrations;
65
    }
66
67 91
    protected function getFileSortCallback() : callable
68
    {
69
        return function (string $a, string $b) {
70 26
            return (basename($a) < basename($b)) ? -1 : 1;
71 91
        };
72
    }
73
}
74