Completed
Push — master ( 13dce9...b41dd5 )
by Dmitry
03:01
created

Migrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 45.45%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 35
wmc 6
lcom 1
cbo 4
ccs 10
cts 22
cp 0.4545
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMigration() 0 8 2
A migrate() 0 21 4
1
<?php
2
3
namespace Tarantool\Mapper\Migrations;
4
5
use Tarantool\Mapper\Contracts;
6
use InvalidArgumentException;
7
use ReflectionClass;
8
9
class Migrator implements Contracts\Migration
10
{
11
    protected $migrations = [];
12
13
    public function registerMigration($class)
14
    {
15
        $reflection = new ReflectionClass($class);
16
        if (!$reflection->implementsInterface(Contracts\Migration::class)) {
17
            throw new InvalidArgumentException("Register only Migration classes");
18
        }
19
        $this->migrations = [];
20
    }
21
22 2
    public function migrate(Contracts\Manager $manager)
23
    {
24 2
        if (!$manager->getSchema()->hasSpace('migration')) {
25 2
            $instance = new Tracker();
26 2
            $instance->migrate($manager);
27 2
            $manager->save($manager->get('migration')->make(['name' => Migration::class]));
0 ignored issues
show
Unused Code introduced by
The call to Repository::make() has too many arguments starting with array('name' => \Taranto...tions\Migration::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28 2
        }
29
30 2
        $repository = $manager->get('migration');
31
32 2
        foreach ($this->migrations as $migration) {
33
            $row = [
34
                'name' => $migration
35
            ];
36
            if (!$repository->find($row, true)) {
37
                $instance = new $migration;
38
                $instance->migrate($this->manager);
0 ignored issues
show
Bug introduced by
The property manager 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...
39
                $manager->save($repository->make($row));
0 ignored issues
show
Unused Code introduced by
The call to Repository::make() has too many arguments starting with $row.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40
            }
41 2
        }
42 2
    }
43
}
44