Completed
Push — master ( 7a93ab...e5b266 )
by Michael
03:59
created

MigrateStatusCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 8
dl 7
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
B run() 0 33 5
A register() 0 10 1

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
namespace Schnittstabil\Dartisan\Commands;
4
5
use Garden\Cli\Cli;
6
use Garden\Cli\Args;
7
use Garden\Cli\Table;
8
use Illuminate\Database\Migrations\Migrator;
9
10
class MigrateStatusCommand extends Command
11
{
12
    use DatabaseAwareCommandTrait;
13
    use MigrationAwareCommandTrait;
14
15
    public static $name = 'migrate:status';
16
    protected $args;
17
    protected $migrator;
18
    protected $migrationsPath;
19
20 View Code Duplication
    public function __construct(callable $outputFormatter, Args $args, Migrator $migrator, $migrationsPath)
21
    {
22
        parent::__construct($outputFormatter);
23
        $this->args = $args;
24
        $this->migrator = $migrator;
25
        $this->migrationsPath = $migrationsPath;
26
    }
27
28
    public function run()
29
    {
30
        if (!$this->migrator->repositoryExists()) {
31
            $this->echoError('No migration table found.');
32
            return 1;
33
        }
34
35
        $path = $this->args->getOpt('path', $this->migrationsPath);
36
        $ran = $this->migrator->getRepository()->getRan();
37
        $migrationFiles = $this->migrator->getMigrationFiles($path);
38
39
        if (count($migrationFiles) === 0) {
40
            $this->echoInfo('No migration files found.');
41
            return 0;
42
        }
43
44
        $table = new Table();
45
        $table
46
            ->row()
47
            ->bold('Ran')
48
            ->bold('Migration');
49
50
        foreach ($migrationFiles as $migration) {
51
            $migrationName = $this->migrator->getMigrationName($migration);
52
            $table->row();
53
            in_array($migrationName, $ran) ? $table->green('Y') : $table->red('N');
54
            $table->cell($migrationName);
55
        }
56
57
        $table->write();
58
59
        return 0;
60
    }
61
62
    public static function register(Cli $cli)
63
    {
64
        $cli = static::registerDatabaseOpts($cli);
65
        $cli = static::registerMigrationOpts($cli);
66
67
        return $cli
68
            ->command(static::$name)
69
            ->description('Show the status of each migration.')
70
            ->opt('path', 'The path of migrations files to be executed.');
71
    }
72
}
73