MigrationMakeCommand::getSchemaParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Migrations\NameParser;
8
use Rawilk\LaravelModules\Support\Migrations\SchemaParser;
9
use Rawilk\LaravelModules\Support\Stub;
10
use Rawilk\LaravelModules\Traits\ModuleCommands;
11
12
class MigrationMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommands;
15
16
    /** @var string */
17
    protected $argumentName = 'name';
18
19
    /** @var string */
20
    protected $signature = 'module:make-migration
21
                            {name : The name of the migration}
22
                            {module? : The name of the module to create the migration for}
23
                            {--fields= : The fields to migrate}
24
                            {--p|plain : Create a plain migration}';
25
26
    /** @var string */
27
    protected $description = 'Create a new migration for the specified module.';
28
29
    protected function getClass(): string
30
    {
31
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

31
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
32
    }
33
34
    protected function getDestinationFilePath(): string
35
    {
36
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
37
38
        $generatorPath = GenerateConfigReader::read('migration');
39
40
        return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php';
41
    }
42
43
    protected function getFileName(): string
44
    {
45
        return date('Y_m_d_His_') . $this->getSchemaName();
46
    }
47
48
    protected function getTemplateContents(): string
49
    {
50
        $parser = new NameParser($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type null and string[]; however, parameter $name of Rawilk\LaravelModules\Su...meParser::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        $parser = new NameParser(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
51
52
        if ($parser->isCreate()) {
53
            return Stub::create('/migration/create.stub', [
54
                'class'  => $this->getClass(),
55
                'table'  => $parser->getTableName(),
56
                'fields' => $this->getSchemaParser()->render()
57
            ]);
58
        }
59
60
        if ($parser->isAdd()) {
61
            return Stub::create('/migration/add.stub', [
62
                'class'       => $this->getClass(),
63
                'table'       => $parser->getTableName(),
64
                'fields_up'   => $this->getSchemaParser()->up(),
65
                'fields_down' => $this->getSchemaParser()->down()
66
            ]);
67
        }
68
69
        if ($parser->isDelete()) {
70
            return Stub::create('/migration/delete.stub', [
71
                'class'       => $this->getClass(),
72
                'table'       => $parser->getTableName(),
73
                'fields_down' => $this->getSchemaParser()->up(),
74
                'fields_up'   => $this->getSchemaParser()->down()
75
            ]);
76
        }
77
78
        if ($parser->isDrop()) {
79
            return Stub::create('/migration/drop.stub', [
80
                'class'  => $this->getClass(),
81
                'table'  => $parser->getTableName(),
82
                'fields' => $this->getSchemaParser()->render()
83
            ]);
84
        }
85
86
        return Stub::create('/migration/plain.stub', [
87
            'class' => $this->getClass()
88
        ]);
89
    }
90
91
    private function getClassName(): string
0 ignored issues
show
Unused Code introduced by
The method getClassName() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
92
    {
93
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

93
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
94
    }
95
96
    private function getSchemaName(): string
97
    {
98
        return $this->argument('name');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->argument('name') could return the type null|string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
99
    }
100
101
    private function getSchemaParser(): SchemaParser
102
    {
103
        return new SchemaParser($this->option('fields'));
104
    }
105
}
106