ExecutedMigrationsList   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 67
loc 67
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getItems() 4 4 1
A getFirst() 8 8 3
A getLast() 9 9 3
A count() 4 4 1
A hasMigration() 10 10 3
A getMigration() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Metadata;
6
7
use Countable;
8
use Doctrine\Migrations\Exception\MigrationNotExecuted;
9
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
10
use Doctrine\Migrations\Version\Version;
11
use function array_values;
12
use function count;
13
14
/**
15
 * Represents a sorted list of executed migrations.
16
 * The migrations in this set might be not available anymore.
17
 */
18 View Code Duplication
final class ExecutedMigrationsList implements Countable
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /** @var ExecutedMigration[] */
21
    private $items = [];
22
23
    /**
24
     * @param ExecutedMigration[] $items
25
     */
26 87
    public function __construct(array $items)
27
    {
28 87
        $this->items = array_values($items);
29 87
    }
30
31
    /**
32
     * @return ExecutedMigration[]
33
     */
34 31
    public function getItems() : array
35
    {
36 31
        return $this->items;
37
    }
38
39 2
    public function getFirst(int $offset = 0) : ExecutedMigration
40
    {
41 2
        if (! isset($this->items[$offset])) {
42 1
            throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : ''));
43
        }
44
45 1
        return $this->items[$offset];
46
    }
47
48 24
    public function getLast(int $offset = 0) : ExecutedMigration
49
    {
50 24
        $offset = count($this->items) - 1 - (-1 * $offset);
51 24
        if (! isset($this->items[$offset])) {
52 7
            throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : ''));
53
        }
54
55 17
        return $this->items[$offset];
56
    }
57
58 28
    public function count() : int
59
    {
60 28
        return count($this->items);
61
    }
62
63 46
    public function hasMigration(Version $version) : bool
64
    {
65 46
        foreach ($this->items as $migration) {
66 28
            if ($migration->getVersion()->equals($version)) {
67 28
                return true;
68
            }
69
        }
70
71 35
        return false;
72
    }
73
74 5
    public function getMigration(Version $version) : ExecutedMigration
75
    {
76 5
        foreach ($this->items as $migration) {
77 5
            if ($migration->getVersion()->equals($version)) {
78 4
                return $migration;
79
            }
80
        }
81
82 1
        throw MigrationNotExecuted::new((string) $version);
83
    }
84
}
85