GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Migration::execute()   D
last analyzed

Complexity

Conditions 10
Paths 164

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
dl 0
loc 42
rs 4.606
c 8
b 0
f 0
cc 10
eloc 28
nc 164
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dami;
4
5
use Dami\Migration\MigrationFiles;
6
use Dami\Migration\SchemaTable;
7
use Dami\Migration\Api\MigrationApi;
8
use Rentgen\Schema\Info;
9
use Rentgen\Schema\Manipulation;
10
11
class Migration
12
{
13
    /**
14
     * @param SchemaTable    $schemaTable        description
15
     * @param MigrationFiles $migrationFiles     description
16
     * @param Manipulation   $schemaManipulation description
17
     * @param Info           $schemaInfo         description
18
     */
19
    public function __construct(SchemaTable $schemaTable, MigrationFiles $migrationFiles, Manipulation $schemaManipulation, Info $schemaInfo)
20
    {
21
        $this->schemaTable = $schemaTable;
0 ignored issues
show
Bug introduced by
The property schemaTable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        $this->migrationFiles = $migrationFiles;
0 ignored issues
show
Bug introduced by
The property migrationFiles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        $this->schemaManipulation = $schemaManipulation;
0 ignored issues
show
Bug introduced by
The property schemaManipulation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->schemaInfo = $schemaInfo;
0 ignored issues
show
Bug introduced by
The property schemaInfo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    /**
28
     * Migrate the schema to the given version.
29
     *
30
     * @return integer Number of migrations.
31
     */
32
    public function migrate($version = null, $message = null)
33
    {
34
        return $this->execute($version, $message);
35
    }
36
37
    /**
38
     * Migrate the schema to previous version.
39
     *
40
     * @return integer Number of migrations.
41
     */
42
    public function migrateToPreviousVersion($message)
43
    {
44
        return $this->execute($this->schemaTable->getPreviousVersion(), $message);
45
    }
46
47
    /**
48
     * Create an instance of migration class.
49
     *
50
     * @param $className Name of migration class.
51
     *
52
     * @return mixed
53
     */
54
    protected function createMigrationApiInstance($className)
55
    {
56
        return new $className($this->schemaManipulation, $this->schemaInfo);
57
    }
58
59
    /**
60
     * Execute migrate.
61
     *
62
     * @param string $version The version of migration to rollback or migrate.
63
     *
64
     * @return integer Number of executed migrations.
65
     */
66
    private function execute($version = null, $message = null)
67
    {
68
        $migrateUp = null === $version || $version > $this->schemaTable->getCurrentVersion();
69
        $files = $this->migrationFiles->get($version);
70
        if (null === $files) {
71
            return 0;
72
        }
73
        $this->schemaManipulation->execute('BEGIN');
74
        try {
75
            foreach ($files as $file) {
76
77
                require_once $file->getPath();
78
79
                $definition = $this->createMigrationApiInstance($file->getClassName());
80
81
                if ($message) {
82
                    $message($file->getName(), $file->getVersion());
83
                }
84
                if ($migrateUp) {
85
                    $definition->up();
86
                } else {
87
                    $definition->down();
88
                }
89
                foreach ($definition->getActions() as $action) {
90
                    if (!is_callable($action)) {
91
                        throw new \InvalidArgumentException('Migration must be callable');
92
                    }
93
                    $action = call_user_func_array($action, array());
94
                    if ($action instanceof MigrationApi) {
95
                        $action->execute();
0 ignored issues
show
Bug introduced by
The call to execute() misses a required argument $sql.

This check looks for function calls that miss required arguments.

Loading history...
96
                    }
97
                }
98
                $this->schemaTable->migrateToVersion($file->getVersion());
99
            }
100
            $this->schemaManipulation->execute('COMMIT');
101
        } catch (\Exception $e) {
102
            $this->schemaManipulation->execute('ROLLBACK');
103
            throw $e;
104
        }
105
106
        return count($files);
107
    }
108
}
109