MigrateMakeCommand::handle()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 51
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9.2733

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 20
nc 9
nop 0
dl 0
loc 51
ccs 17
cts 20
cp 0.85
crap 9.2733
rs 8.0555
c 1
b 0
f 0

How to fix   Long Method   

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
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Console\Migrations;
6
7
use Exception;
8
use Illuminate\Database\Console\Migrations\MigrateMakeCommand as IlluminateMigrateMakeCommand;
9
use Illuminate\Database\Console\Migrations\TableGuesser;
10
use Illuminate\Support\Composer;
11
use Illuminate\Support\Str;
12
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands;
13
use LaravelFreelancerNL\Aranguent\Migrations\MigrationCreator;
14
15
class MigrateMakeCommand extends IlluminateMigrateMakeCommand
16
{
17
    use ArangoCommands;
18
19
    /**
20
     * The console command signature.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'make:migration {name : The name of the migration}
25
        {--create= : The table to be created}
26
        {--edge= : The edge table to be created}
27
        {--table= : The table to alter}
28
        {--path= : The location where the migration file should be created}
29
        {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
30
        {--fullpath : Output the full path of the migration (Deprecated)}';
31
32
    /**
33
     * Create a new migration install command instance.
34
     */
35 498
    public function __construct(MigrationCreator $creator, Composer $composer)
36
    {
37 498
        $this->creator = $creator;
38 498
        $this->composer = $composer;
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Database\Cons...eMakeCommand::$composer has been deprecated: Will be removed in a future Laravel version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

38
        /** @scrutinizer ignore-deprecated */ $this->composer = $composer;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
39
40 498
        parent::__construct($creator, $composer);
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @return void
47
     *
48
     * @throws Exception
49
     */
50 4
    public function handle()
51
    {
52
        assert($this->creator instanceof MigrationCreator);
53
54 4
        if ($this->useFallback()) {
55
            $this->creator->setIlluminateCustomStubPath();
56
            parent::handle();
57
            return;
58
        }
59
60 4
        $this->creator->setArangoCustomStubPath();
61
62
        // It's possible for the developer to specify the tables to modify in this
63
        // schema operation. The developer may also specify if this table needs
64
        // to be freshly created so we can create the appropriate migrations.
65 4
        $name = Str::snake(trim((string) $this->input->getArgument('name')));
66
67 4
        $table = $this->input->getOption('table');
68
69 4
        $create = $this->input->getOption('create') ?: false;
70
71 4
        $edge = $this->input->getOption('edge') ?: false;
72
73
        // If no table was given as an option but a create option is given then we
74
        // will use the "create" option as the table name. This allows the devs
75
        // to pass a table name into this option as a short-cut for creating.
76 4
        if (!$table && is_string($create)) {
77 1
            $table = $create;
78
79 1
            $create = true;
80
        }
81
82 4
        if (!$table && is_string($edge)) {
83 2
            $table = $create;
84
85 2
            $edge = true;
86
        }
87
88
        // Next, we will attempt to guess the table name if this the migration has
89
        // "create" in the name. This will allow us to provide a convenient way
90
        // of creating migrations that create new tables for the application.
91 4
        if (!$table) {
92 3
            [$table, $create] = TableGuesser::guess($name);
93
        }
94
95
        // Now we are ready to write the migration out to disk. Once we've written
96
        // the migration out, we will dump-autoload for the entire framework to
97
        // make sure that the migrations are registered by the class loaders.
98 4
        $this->writeMigration($name, $table, $create, $edge);
99
100 4
        $this->composer->dumpAutoloads();
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Database\Cons...eMakeCommand::$composer has been deprecated: Will be removed in a future Laravel version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
        /** @scrutinizer ignore-deprecated */ $this->composer->dumpAutoloads();

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
101
    }
102
103
    /**
104
     * Write the migration file to disk.
105
     *
106
     * @param  string  $name
107
     * @param  string  $table
108
     * @param  bool  $create
109
     * @param  bool  $edge
110
     * @return void
111
     *
112
     * @throws Exception
113
     *
114
     * @SuppressWarnings("PHPMD.BooleanArgumentFlag")
115
     */
116 4
    protected function writeMigration($name, $table, $create, $edge = false)
117
    {
118 4
        if ($this->useFallback()) {
119
            parent::writeMigration($name, $table, $create);
120
            return;
121
        }
122
123
        assert($this->creator instanceof MigrationCreator);
124
125 4
        $file = pathinfo(
126 4
            $this->creator->create(
127 4
                $name,
128 4
                $this->getMigrationPath(),
129 4
                $table,
130 4
                $create,
131 4
                $edge,
132 4
            ),
133 4
            PATHINFO_FILENAME,
134 4
        );
135
136 4
        $this->line("<info>Created Migration:</info> {$file}");
137
    }
138
}
139