Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Commands/MigrationMakeCommand.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Migrations\NameParser;
8
use Nwidart\Modules\Support\Migrations\SchemaParser;
9
use Nwidart\Modules\Support\Stub;
10
use Nwidart\Modules\Traits\ModuleCommandTrait;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputOption;
13
14
class MigrationMakeCommand extends GeneratorCommand
15
{
16
    use ModuleCommandTrait;
17
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'module:make-migration';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new migration for the specified module.';
31
32
    /**
33
     * Get the console command arguments.
34
     *
35
     * @return array
36
     */
37 120
    protected function getArguments()
38
    {
39
        return [
40 120
            ['name', InputArgument::REQUIRED, 'The migration name will be created.'],
41
            ['module', InputArgument::OPTIONAL, 'The name of module will be created.'],
42
        ];
43
    }
44
45
    /**
46
     * Get the console command options.
47
     *
48
     * @return array
49
     */
50 120 View Code Duplication
    protected function getOptions()
51
    {
52
        return [
53 120
            ['fields', null, InputOption::VALUE_OPTIONAL, 'The specified fields table.', null],
54
            ['plain', null, InputOption::VALUE_NONE, 'Create plain migration.'],
55
        ];
56
    }
57
58
    /**
59
     * Get schema parser.
60
     *
61
     * @return SchemaParser
62
     */
63 9
    public function getSchemaParser()
64
    {
65 9
        return new SchemaParser($this->option('fields'));
66
    }
67
68
    /**
69
     * @throws \InvalidArgumentException
70
     *
71
     * @return mixed
72
     */
73 10
    protected function getTemplateContents()
74
    {
75 10
        $parser = new NameParser($this->argument('name'));
0 ignored issues
show
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array or null; however, Nwidart\Modules\Support\...meParser::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
77 10
        if ($parser->isCreate()) {
78 6
            return Stub::create('/migration/create.stub', [
79 6
                'class' => $this->getClass(),
80 6
                'table' => $parser->getTableName(),
81 6
                'fields' => $this->getSchemaParser()->render(),
82
            ]);
83 4 View Code Duplication
        } elseif ($parser->isAdd()) {
84 1
            return Stub::create('/migration/add.stub', [
85 1
                'class' => $this->getClass(),
86 1
                'table' => $parser->getTableName(),
87 1
                'fields_up' => $this->getSchemaParser()->up(),
88 1
                'fields_down' => $this->getSchemaParser()->down(),
89
            ]);
90 3
        } elseif ($parser->isDelete()) {
91 1
            return Stub::create('/migration/delete.stub', [
92 1
                'class' => $this->getClass(),
93 1
                'table' => $parser->getTableName(),
94 1
                'fields_down' => $this->getSchemaParser()->up(),
95 1
                'fields_up' => $this->getSchemaParser()->down(),
96
            ]);
97 2 View Code Duplication
        } elseif ($parser->isDrop()) {
98 1
            return Stub::create('/migration/drop.stub', [
99 1
                'class' => $this->getClass(),
100 1
                'table' => $parser->getTableName(),
101 1
                'fields' => $this->getSchemaParser()->render(),
102
            ]);
103
        }
104
105 1
        return Stub::create('/migration/plain.stub', [
106 1
            'class' => $this->getClass(),
107
        ]);
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113 10
    protected function getDestinationFilePath()
114
    {
115 10
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
116
117 10
        $generatorPath = GenerateConfigReader::read('migration');
118
119 10
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
120
    }
121
122
    /**
123
     * @return string
124
     */
125 10
    private function getFileName()
126
    {
127 10
        return date('Y_m_d_His_') . $this->getSchemaName();
128
    }
129
130
    /**
131
     * @return array|string
132
     */
133 10
    private function getSchemaName()
134
    {
135 10
        return $this->argument('name');
136
    }
137
138
    /**
139
     * @return string
140
     */
141 10
    private function getClassName()
142
    {
143 10
        return Str::studly($this->argument('name'));
0 ignored issues
show
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array or null; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
144
    }
145
146 10
    public function getClass()
147
    {
148 10
        return $this->getClassName();
149
    }
150
151
    /**
152
     * Run the command.
153
     */
154 10
    public function handle()
155
    {
156 10
        parent::handle();
157
158 10
        if (app()->environment() === 'testing') {
159 10
            return;
160
        }
161
    }
162
}
163