Completed
Push — master ( 747e00...006ae1 )
by Changwan
03:04
created

Migration   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A up() 0 4 1
A down() 0 4 1
migrate() 0 1 ?
rollback() 0 1 ?
1
<?php
2
namespace Wandu\Database\Migrator;
3
4
use Wandu\Database\Contracts\ConnectionInterface;
5
use Wandu\Database\Manager;
6
7
abstract class Migration implements MigrationInterface
8
{
9
    /** @var string */
10
    protected $connection = 'default';
11
12
    /** @var \Wandu\Database\Manager */
13
    protected $manager;
14
15
    /**
16
     * @param \Wandu\Database\Manager $manager
17
     */
18
    public function __construct(Manager $manager)
19
    {
20
        $this->manager = $manager;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function up()
27
    {
28
        $this->migrate($this->manager->getConnection($this->connection));
0 ignored issues
show
Bug introduced by
It seems like $this->manager->getConnection($this->connection) can be null; however, migrate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function down()
35
    {
36
        $this->rollback($this->manager->getConnection($this->connection));
0 ignored issues
show
Bug introduced by
It seems like $this->manager->getConnection($this->connection) can be null; however, rollback() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
37
    }
38
39
    /**
40
     * @param \Wandu\Database\Contracts\ConnectionInterface $connection
41
     */
42
    abstract public function migrate(ConnectionInterface $connection);
43
44
    /**
45
     * @param \Wandu\Database\Contracts\ConnectionInterface $connection
46
     */
47
    abstract public function rollback(ConnectionInterface $connection);
48
}
49