Migrate   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 122
Duplicated Lines 11.48 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 14
loc 122
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 8 42 7
A migrateToVersion() 6 11 2
A getDbVersion() 0 5 2
A showVersions() 0 17 1
A getLatestVersion() 0 11 2
B listMigrationFiles() 0 28 6

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
 * Part of Cli for CodeIgniter
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/codeigniter-cli
9
 */
10
11
namespace Kenjis\CodeIgniter_Cli\Command;
12
13
use Aura\Cli\Status;
14
15
/**
16
 * @property \CI_Migration $migration
17
 * @property \CI_Loader    $load
18
 * @property \CI_Config    $config
19
 */
20
class Migrate extends Command
21
{
22
    public function __invoke($command = null)
23
    {
24
        $this->load->library('migration');
25
        $this->load->config('migration');
26
27
        if ($command === 'status') {
28
            $this->listMigrationFiles();
29
            return;
30
        }
31
32
        if ($command === 'version') {
33
            $this->showVersions();
34
            return;
35
        }
36
37
        // if argument is digits, migrate to the version
38
        if (ctype_digit($command)) {
39
            if ($this->migrateToVersion($command) === false) {
40
                return Status::FAILURE;
41
            } else {
42
                $this->showVersions();
43
                return;
44
            }
45
        }
46
47
        if ($command !== null) {
48
            $this->stdio->errln(
49
                '<<red>>No such command: ' . $command . '<<reset>>'
50
            );
51
            return Status::USAGE;
52
        }
53
54
        // if no argument, migrate to current
55 View Code Duplication
        if ($this->migration->current() === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            $this->stdio->errln(
57
                '<<red>>' . $this->migration->error_string() . '<<reset>>'
58
            );
59
            return Status::FAILURE;
60
        } else {
61
            $this->showVersions();
62
        }
63
    }
64
65
    private function migrateToVersion($version)
66
    {
67 View Code Duplication
        if ($this->migration->version($version) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
            $this->stdio->errln(
69
                '<<red>>' . $this->migration->error_string() . '<<reset>>'
70
            );
71
            return false;
72
        }
73
        
74
        return true;
75
    }
76
77
    private function getDbVersion()
78
    {
79
        $row = $this->db->select('version')->get($this->config->item('migration_table'))->row();
80
        return $row ? $row->version : '0';
81
    }
82
83
    private function showVersions()
84
    {
85
        $this->stdio->outln(
86
            ' current: <<green>>' . $this->config->item('migration_version') . '<<reset>>'
87
            . ' (in config/migration.php)'
88
        );
89
        $version = $this->getDbVersion();
90
        $this->stdio->outln(
91
            'database: <<bold>>' . $version . '<<reset>>'
92
            . ' (in database table)'
93
        );
94
        $version = $this->getLatestVersion();
95
        $this->stdio->outln(
96
            '  latest: ' . $version . ''
97
            . ' (in migration files)'
98
        );
99
    }
100
101
    private function getLatestVersion()
102
    {
103
        $files = $this->migration->find_migrations();
104
        
105
        if ($files === []) {
106
            return 'null';
107
        }
108
        
109
        end($files);
110
        return key($files);
111
    }
112
113
    private function listMigrationFiles()
114
    {
115
        $this->stdio->outln(
116
            $this->config->item('migration_path')
117
        );
118
119
        $current = $this->config->item('migration_version');
120
        $db = $this->getDbVersion();
121
122
        $files = $this->migration->find_migrations();
123
        foreach ($files as $v => $file) {
124
            if ($v == $current && $v == $db) {
125
                $this->stdio->outln(
126
                    '  <<green>>' . basename($file) .' (current/database)<<reset>>'
127
                );
128
            } elseif ($v == $current) {
129
                $this->stdio->outln(
130
                    '  <<green>>' . basename($file) .' (current)<<reset>>'
131
                );
132
            } elseif ($v == $db) {
133
                $this->stdio->outln(
134
                    '  <<bold>>' . basename($file) .' (database)<<reset>>'
135
                );
136
            } else {
137
                $this->stdio->outln('  ' . basename($file));
138
            }
139
        }
140
    }
141
}
142