Passed
Push — master ( 9e7eff...49ce35 )
by Mr
02:03
created

MigrationList::getPendingMigrations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/dbal project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Dbal\Migration;
10
11
use Daikon\DataStructure\TypedList;
12
use Daikon\Interop\ToNativeInterface;
13
14
final class MigrationList extends TypedList implements ToNativeInterface
15
{
16
    public function __construct(iterable $migrations = [])
17
    {
18
        $this->init($migrations, [MigrationInterface::class]);
19
    }
20
21
    public function exclude(self $migrationList): self
22
    {
23
        return $this->filter(
24
            fn(MigrationInterface $migration): bool => !$migrationList->contains($migration)
25
        );
26
    }
27
28
    public function getPendingMigrations(): self
29
    {
30
        return $this->filter(
31
            fn(MigrationInterface $migration): bool => !$migration->hasExecuted()
32
        );
33
    }
34
35
    public function getExecutedMigrations(): self
36
    {
37
        return $this->filter(
38
            fn(MigrationInterface $migration): bool => $migration->hasExecuted()
39
        );
40
    }
41
42
    public function contains(MigrationInterface $migration): bool
43
    {
44
        return $this->reduce(
45
            function (bool $carry, MigrationInterface $item) use ($migration): bool {
46
                return $carry
47
                    || $item->getName() === $migration->getName()
48
                    && $item->getVersion() === $migration->getVersion();
49
            },
50
            false
51
        );
52
    }
53
54
    public function sortByVersion(): self
55
    {
56
        return $this->sort(
57
            fn(MigrationInterface $a, MigrationInterface $b): int => $a->getVersion() - $b->getVersion()
58
        );
59
    }
60
61
    public function findBeforeVersion(int $version = null): self
62
    {
63
        return $this->filter(
64
            fn(MigrationInterface $migration): bool => !$version || $migration->getVersion() <= $version
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. 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...
65
        );
66
    }
67
68
    public function findAfterVersion(int $version = null): self
69
    {
70
        return $this->filter(
71
            fn(MigrationInterface $migration): bool => !$version || $migration->getVersion() > $version
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. 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...
72
        );
73
    }
74
75
    public function toNative(): array
76
    {
77
        return $this->map(
78
            fn(MigrationInterface $migration): array => $migration->toNative()
79
        )->unwrap();
80
    }
81
}
82