AvailableMigrationsList   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\MigrationNotAvailable;
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 migrations that may or maybe not be already executed.
16
 */
17 View Code Duplication
final class AvailableMigrationsList 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...
18
{
19
    /** @var AvailableMigration[] */
20
    private $items = [];
21
22
    /**
23
     * @param AvailableMigration[] $items
24
     */
25 89
    public function __construct(array $items)
26
    {
27 89
        $this->items = array_values($items);
28 89
    }
29
30
    /**
31
     * @return AvailableMigration[]
32
     */
33 51
    public function getItems() : array
34
    {
35 51
        return $this->items;
36
    }
37
38 11
    public function getFirst(int $offset = 0) : AvailableMigration
39
    {
40 11
        if (! isset($this->items[$offset])) {
41 6
            throw NoMigrationsFoundWithCriteria::new('first' . ($offset > 0 ? '+' . $offset : ''));
42
        }
43
44 5
        return $this->items[$offset];
45
    }
46
47 18
    public function getLast(int $offset = 0) : AvailableMigration
48
    {
49 18
        $offset = count($this->items) - 1 - (-1 * $offset);
50 18
        if (! isset($this->items[$offset])) {
51 2
            throw NoMigrationsFoundWithCriteria::new('last' . ($offset > 0 ? '+' . $offset : ''));
52
        }
53
54 16
        return $this->items[$offset];
55
    }
56
57 13
    public function count() : int
58
    {
59 13
        return count($this->items);
60
    }
61
62 25
    public function hasMigration(Version $version) : bool
63
    {
64 25
        foreach ($this->items as $migration) {
65 22
            if ($migration->getVersion()->equals($version)) {
66 12
                return true;
67
            }
68
        }
69
70 17
        return false;
71
    }
72
73 7
    public function getMigration(Version $version) : AvailableMigration
74
    {
75 7
        foreach ($this->items as $migration) {
76 7
            if ($migration->getVersion()->equals($version)) {
77 6
                return $migration;
78
            }
79
        }
80
81 1
        throw MigrationNotAvailable::forVersion($version);
82
    }
83
}
84