MigrateStatusCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A run() 0 15 2
A echoMigrationTable() 0 26 4
A register() 10 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
use Schnittstabil\Dartisan\OutputInterface;
10
11
class MigrateStatusCommand extends Command
12
{
13
    use DatabaseAwareCommandTrait;
14
    use MigrationAwareCommandTrait;
15
16
    public static $name = 'migrate:status';
17
18
    /**
19
     * @var Migrator
20
     */
21
    protected $migrator;
22
23
    /**
24
     * @var string
25
     */
26
    protected $migrationsPath;
27
28
    public function __construct(
29
        Args $args,
30
        OutputInterface $output,
31
        Migrator $migrator,
32
        string $migrationsPath
33
    ) {
34
        parent::__construct($args, $output);
35
        $this->migrator = $migrator;
36
        $this->migrationsPath = $migrationsPath;
37
    }
38
39
    public function run()
40
    {
41
        if (!$this->migrator->repositoryExists()) {
42
            $this->output->error('No migration table found.');
0 ignored issues
show
Bug introduced by
The method error cannot be called on $this->output (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
43
44
            return 1;
45
        }
46
47
        $path = $this->args->getOpt('path', $this->migrationsPath);
48
        $ran = $this->migrator->getRepository()->getRan();
49
        $migrationFiles = $this->migrator->getMigrationFiles($path);
50
        $this->echoMigrationTable($ran, $migrationFiles);
51
52
        return 0;
53
    }
54
55
    /**
56
     * @SuppressWarnings(PHPMD.ElseExpression)
57
     **/
58
    protected function echoMigrationTable($ran, $migrationFiles)
59
    {
60
        if (count($migrationFiles) === 0) {
61
            $this->output->info('No migration files found.');
0 ignored issues
show
Bug introduced by
The method info cannot be called on $this->output (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
62
63
            return;
64
        }
65
66
        $table = new Table();
67
        $table->row()->bold('Ran')->bold('Migration');
68
69
        foreach ($migrationFiles as $migration) {
70
            $migrationName = $this->migrator->getMigrationName($migration);
71
            $table->row();
72
73
            if (in_array($migrationName, $ran)) {
74
                $table->green('Y');
75
            } else {
76
                $table->red('N');
77
            }
78
79
            $table->cell($migrationName);
80
        }
81
82
        $table->write();
83
    }
84
85 View Code Duplication
    public static function register(Cli $cli)
86
    {
87
        $cli = static::registerDatabaseOpts($cli);
88
        $cli = static::registerMigrationOpts($cli);
89
90
        return $cli
91
            ->command(static::$name)
92
            ->description('Show the status of each migration.')
93
            ->opt('path', 'The path of migrations files to be executed.');
94
    }
95
}
96