Completed
Push — master ( 1f054f...39d367 )
by Grégoire
02:27
created

DefaultAliasResolver::resolveVersionAlias()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 63
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 42
nc 13
nop 1
dl 0
loc 63
ccs 32
cts 32
cp 1
crap 13
rs 6.6166
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Doctrine\Migrations\MigrationRepository;
12
use function substr;
13
14
/**
15
 * The DefaultAliasResolver class is responsible for resolving aliases like first, current, etc. to the actual version number.
16
 *
17
 * @internal
18
 */
19
final class DefaultAliasResolver implements AliasResolver
20
{
21
    private const ALIAS_FIRST   = 'first';
22
    private const ALIAS_CURRENT = 'current';
23
    private const ALIAS_PREV    = 'prev';
24
    private const ALIAS_NEXT    = 'next';
25
    private const ALIAS_LATEST  = 'latest';
26
27
    /** @var MigrationRepository */
28
    private $migrationRepository;
29
30
    /** @var MetadataStorage */
31
    private $metadataStorage;
32
33
    /** @var MigrationStatusCalculator */
34
    private $migrationStatusCalculator;
35
36 34
    public function __construct(
37
        MigrationRepository $migrationRepository,
38
        MetadataStorage $metadataStorage,
39
        MigrationStatusCalculator $migrationStatusCalculator
40
    ) {
41 34
        $this->migrationRepository       = $migrationRepository;
42 34
        $this->metadataStorage           = $metadataStorage;
43 34
        $this->migrationStatusCalculator = $migrationStatusCalculator;
44 34
    }
45
46
    /**
47
     * Returns the version number from an alias.
48
     *
49
     * Supported aliases are:
50
     *
51
     * - first: The very first version before any migrations have been run.
52
     * - current: The current version.
53
     * - prev: The version prior to the current version.
54
     * - next: The version following the current version.
55
     * - latest: The latest available version.
56
     *
57
     * If an existing version number is specified, it is returned verbatimly.
58
     */
59 32
    public function resolveVersionAlias(string $alias) : Version
60
    {
61 32
        $availableMigrations = $this->migrationRepository->getMigrations();
62 32
        $executedMigrations  = $this->metadataStorage->getExecutedMigrations();
63
64
        switch ($alias) {
65 32
            case self::ALIAS_FIRST:
66 3
                return new Version('0');
67
68
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69 29
            case self::ALIAS_CURRENT:
70
                try {
71 3
                    return $executedMigrations->getLast()->getVersion();
72 1
                } catch (NoMigrationsFoundWithCriteria $e) {
73 1
                    return new Version('0');
74
                }
75
76
                break;
77 27
            case self::ALIAS_PREV:
78
                try {
79 4
                    return $executedMigrations->getLast(-1)->getVersion();
80 2
                } catch (NoMigrationsFoundWithCriteria $e) {
81 2
                    return new Version('0');
82
                }
83
84
                break;
85 24
            case self::ALIAS_NEXT:
86 3
                $newMigrations = $this->migrationStatusCalculator->getNewMigrations();
87
88
                try {
89 3
                    return $newMigrations->getFirst()->getVersion();
90 1
                } catch (NoMigrationsFoundWithCriteria $e) {
91 1
                    throw NoMigrationsToExecute::new($e);
92
                }
93
94
                break;
95 22
            case self::ALIAS_LATEST:
96
                try {
97 11
                    return $availableMigrations->getLast()->getVersion();
98 1
                } catch (NoMigrationsFoundWithCriteria $e) {
99 1
                    throw NoMigrationsToExecute::new($e);
100
                }
101
102
                break;
103
            default:
104 11
                if ($availableMigrations->hasMigration(new Version($alias))) {
105 3
                    return $availableMigrations->getMigration(new Version($alias))->getVersion();
106
                }
107
108 8
                if (substr($alias, 0, 7) === self::ALIAS_CURRENT) {
109 5
                    $val             = (int) substr($alias, 7);
110 5
                    $targetMigration = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $targetMigration is dead and can be removed.
Loading history...
111 5
                    if ($val > 0) {
112 3
                        $newMigrations = $this->migrationStatusCalculator->getNewMigrations();
113
114 3
                        return $newMigrations->getFirst($val - 1)->getVersion();
115
                    }
116
117 2
                    return $executedMigrations->getLast($val)->getVersion();
118
                }
119
        }
120
121 3
        throw UnknownMigrationVersion::new($alias);
122
    }
123
}
124