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

Migration::migrate()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
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