DefaultAliasResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Version;
6
7
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
8
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
9
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
10
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
11
use function substr;
12
13
/**
14
 * The DefaultAliasResolver class is responsible for resolving aliases like first, current, etc. to the actual version number.
15
 *
16
 * @internal
17
 */
18
final class DefaultAliasResolver implements AliasResolver
19
{
20
    private const ALIAS_FIRST   = 'first';
21
    private const ALIAS_CURRENT = 'current';
22
    private const ALIAS_PREV    = 'prev';
23
    private const ALIAS_NEXT    = 'next';
24
    private const ALIAS_LATEST  = 'latest';
25
26
    /** @var MigrationPlanCalculator */
27
    private $migrationPlanCalculator;
28
29
    /** @var MetadataStorage */
30
    private $metadataStorage;
31
32
    /** @var MigrationStatusCalculator */
33
    private $migrationStatusCalculator;
34
35 47
    public function __construct(
36
        MigrationPlanCalculator $migrationPlanCalculator,
37
        MetadataStorage $metadataStorage,
38
        MigrationStatusCalculator $migrationStatusCalculator
39
    ) {
40 47
        $this->migrationPlanCalculator   = $migrationPlanCalculator;
41 47
        $this->metadataStorage           = $metadataStorage;
42 47
        $this->migrationStatusCalculator = $migrationStatusCalculator;
43 47
    }
44
45
    /**
46
     * Returns the version number from an alias.
47
     *
48
     * Supported aliases are:
49
     *
50
     * - first: The very first version before any migrations have been run.
51
     * - current: The current version.
52
     * - prev: The version prior to the current version.
53
     * - next: The version following the current version.
54
     * - latest: The latest available version.
55
     *
56
     * If an existing version number is specified, it is returned verbatimly.
57
     *
58
     * @throws NoMigrationsToExecute
59
     * @throws UnknownMigrationVersion
60
     * @throws NoMigrationsFoundWithCriteria
61
     */
62 45
    public function resolveVersionAlias(string $alias) : Version
63
    {
64 45
        $availableMigrations = $this->migrationPlanCalculator->getMigrations();
65 45
        $executedMigrations  = $this->metadataStorage->getExecutedMigrations();
66
67
        switch ($alias) {
68 45
            case self::ALIAS_FIRST:
69 43
            case '0':
70 5
                return new Version('0');
71 42
            case self::ALIAS_CURRENT:
72
                try {
73 14
                    return $executedMigrations->getLast()->getVersion();
74 4
                } catch (NoMigrationsFoundWithCriteria $e) {
75 4
                    return new Version('0');
76
                }
77
78
                // no break because of return
79 35
            case self::ALIAS_PREV:
80
                try {
81 4
                    return $executedMigrations->getLast(-1)->getVersion();
82 1
                } catch (NoMigrationsFoundWithCriteria $e) {
83 1
                    return new Version('0');
84
                }
85
86
                // no break because of return
87 32
            case self::ALIAS_NEXT:
88 5
                $newMigrations = $this->migrationStatusCalculator->getNewMigrations();
89
90
                try {
91 5
                    return $newMigrations->getFirst()->getVersion();
92 3
                } catch (NoMigrationsFoundWithCriteria $e) {
93 3
                    throw NoMigrationsToExecute::new($e);
94
                }
95
96
                // no break because of return
97 28
            case self::ALIAS_LATEST:
98
                try {
99 16
                    return $availableMigrations->getLast()->getVersion();
100 1
                } catch (NoMigrationsFoundWithCriteria $e) {
101 1
                    return $this->resolveVersionAlias(self::ALIAS_CURRENT);
102
                }
103
104
                // no break because of return
105
            default:
106 12
                if ($availableMigrations->hasMigration(new Version($alias))) {
107 3
                    return $availableMigrations->getMigration(new Version($alias))->getVersion();
108
                }
109
110 9
                if (substr($alias, 0, 7) === self::ALIAS_CURRENT) {
111 6
                    $val             = (int) substr($alias, 7);
112 6
                    $targetMigration = null;
0 ignored issues
show
Unused Code introduced by
$targetMigration is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113 6
                    if ($val > 0) {
114 4
                        $newMigrations = $this->migrationStatusCalculator->getNewMigrations();
115
116 4
                        return $newMigrations->getFirst($val - 1)->getVersion();
117
                    }
118
119 2
                    return $executedMigrations->getLast($val)->getVersion();
120
                }
121
        }
122
123 3
        throw UnknownMigrationVersion::new($alias);
124
    }
125
}
126