Test Failed
Push — master ( 456c6d...06ba11 )
by Mr
02:08
created

MigrationTarget::getMigrationList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace Daikon\Dbal\Migration;
4
5
use Assert\Assertion;
6
use Daikon\Dbal\Exception\MigrationException;
7
8
final class MigrationTarget implements MigrationTargetInterface
9
{
10
    private $name;
11
12
    private $enabled;
13
14
    private $migrationAdapter;
15
16
    private $migrationLoader;
17
18
    private $migrationList;
19
20
    public function __construct(
21
        string $name,
22
        bool $enabled,
23
        MigrationAdapterInterface $migrationAdapter,
24
        MigrationLoaderInterface $migrationLoader
25
    ) {
26
        $this->name = $name;
27
        $this->enabled = $enabled;
28
        $this->migrationAdapter = $migrationAdapter;
29
        $this->migrationLoader = $migrationLoader;
30
    }
31
32
    public function getName(): string
33
    {
34
        return $this->name;
35
    }
36
37
    public function isEnabled(): bool
38
    {
39
        return $this->enabled === true;
40
    }
41
42
    public function getMigrationList(): MigrationList
43
    {
44
        if (!$this->migrationList) {
45
            $availableMigrations = $this->migrationLoader->load();
46
            $executedMigrations = $this->migrationAdapter->read($this->name);
47
            $pendingMigrations = $availableMigrations->diff($executedMigrations);
0 ignored issues
show
Documentation introduced by
$executedMigrations is of type object<Daikon\Dbal\Migration\MigrationList>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
            $this->migrationList = $executedMigrations->merge($pendingMigrations);
0 ignored issues
show
Documentation introduced by
$pendingMigrations is of type object<Daikon\Dbal\Migration\MigrationList>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
        }
50
        return $this->migrationList;
51
    }
52
53
    public function migrate(string $direction, string $version = null): MigrationList
54
    {
55
        Assertion::true($this->isEnabled());
56
57
        if ($direction === MigrationInterface::MIGRATE_DOWN) {
58
            $executedMigrations = $this->migrateDown($version);
59
        } else {
60
            $executedMigrations = $this->migrateUp($version);
61
        }
62
63
        return $executedMigrations;
64
    }
65
66 View Code Duplication
    private function migrateUp(int $version = null): MigrationList
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $migrationList = $this->getMigrationList();
69
        $pendingMigrations = $migrationList->getPendingMigrations();
70
        $executedMigrations = $migrationList->getExecutedMigrations();
71
72
        if ($version && !$pendingMigrations->containsVersion($version)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73
            throw new MigrationException(sprintf('Target version %s not found in pending migrations.', $version));
74
        }
75
76
        $completedMigrations = [];
77
        $connector = $this->migrationAdapter->getConnector();
78
        foreach ($pendingMigrations as $migration) {
79
            $migration->execute($connector, MigrationInterface::MIGRATE_UP);
80
            $executedMigrations = $executedMigrations->push($migration);
81
            $this->migrationAdapter->write($this->name, $executedMigrations);
82
            $completedMigrations[] = $migration;
83
            if ($version && $migration->getVersion() === $version) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
84
                break;
85
            }
86
        }
87
88
        return new MigrationList($completedMigrations);
89
    }
90
91 View Code Duplication
    private function migrateDown(int $version = null): MigrationList
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $migrationList = $this->getMigrationList();
94
        $executedMigrations = $migrationList->getExecutedMigrations();
95
96
        if ($version && !$executedMigrations->containsVersion($version)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
97
            throw new MigrationException(sprintf('Target version %s not found in executed migrations.', $version));
98
        }
99
100
        $completedMigrations = [];
101
        $connector = $this->migrationAdapter->getConnector();
102
        foreach ($executedMigrations->reverse() as $migration) {
103
            if ($version && $migration->getVersion() === $version) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
104
                break;
105
            }
106
            $migration->execute($connector, MigrationInterface::MIGRATE_DOWN);
107
            $executedMigrations = $executedMigrations->remove($migration);
108
            if ($executedMigrations->count() > 0) {
109
                /*
110
                 * Do not write version info after reversing initial migration since we have
111
                 * an assumption that the database was deleted!
112
                 */
113
                $this->migrationAdapter->write($this->name, $executedMigrations);
114
            }
115
            $completedMigrations[] = $migration;
116
        }
117
118
        return new MigrationList($completedMigrations);
119
    }
120
}
121