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

MigrationTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 16
loc 66
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 15 2
A getName() 8 8 2
A getVersion() 8 8 2
A hasExecuted() 0 4 1
A toArray() 0 15 2

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
namespace Daikon\Dbal\Migration;
4
5
use Assert\Assertion;
6
use Daikon\Dbal\Connector\ConnectorInterface;
7
use Daikon\Dbal\Exception\MigrationException;
8
9
trait MigrationTrait
10
{
11
    private $executedAt;
12
13
    private $connector;
14
15
    public function __construct(\DateTimeImmutable $executedAt = null)
16
    {
17
        $this->executedAt = $executedAt;
18
    }
19
20
    public function execute(ConnectorInterface $connector, string $direction = self::MIGRATE_UP): void
21
    {
22
        $this->connector = $connector;
23
24
        if ($direction === self::MIGRATE_DOWN) {
25
            Assertion::true($this->isReversible());
0 ignored issues
show
Bug introduced by
It seems like isReversible() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
            Assertion::true($this->hasExecuted());
27
            $this->down();
0 ignored issues
show
Bug introduced by
It seems like down() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
            $this->executedAt = null;
29
        } else {
30
            Assertion::false($this->hasExecuted());
31
            $this->up();
0 ignored issues
show
Bug introduced by
It seems like up() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
            $this->executedAt = new \DateTimeImmutable('now');
33
        }
34
    }
35
36 View Code Duplication
    public function getName(): string
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...
37
    {
38
        $shortName = (new \ReflectionClass(static::class))->getShortName();
39
        if (!preg_match('#^(?<name>.+?)\d+$#', $shortName, $matches)) {
40
            throw new MigrationException('Unexpected migration name in '.$shortName);
41
        }
42
        return $matches['name'];
43
    }
44
45 View Code Duplication
    public function getVersion(): int
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...
46
    {
47
        $shortName= (new \ReflectionClass(static::class))->getShortName();
48
        if (!preg_match('#(?<version>\d{14})$#', $shortName, $matches)) {
49
            throw new MigrationException('Unexpected migration version in '.$shortName);
50
        }
51
        return intval($matches['version']);
52
    }
53
54
    public function hasExecuted(): bool
55
    {
56
        return $this->executedAt instanceof \DateTimeImmutable;
57
    }
58
59
    public function toArray(): array
60
    {
61
        $arr = [
62
            '@type' => static::class,
63
            'name' => $this->getName(),
64
            'version' => $this->getVersion(),
65
            'description' => $this->getDescription()
0 ignored issues
show
Bug introduced by
It seems like getDescription() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
        ];
67
68
        if ($this->hasExecuted()) {
69
            $arr['executedAt']  = $this->executedAt->format('c');
70
        }
71
72
        return $arr;
73
    }
74
}
75