Completed
Push — master ( dcc042...9e46d5 )
by Nicolas
16:30
created

src/Commands/MigrationMakeCommand.php (5 issues)

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 118
    protected function getArguments()
38
    {
39
        return [
40 118
            ['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 118 View Code Duplication
    protected function getOptions()
51
    {
52
        return [
53 118
            ['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'));
76
77 10
        if ($parser->isCreate()) {
78 6
            return Stub::create('/migration/create.stub', [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Nwidart\Modules\...maParser()->render())); (Nwidart\Modules\Commands\MigrationMakeCommand) is incompatible with the return type declared by the abstract method Nwidart\Modules\Commands...nd::getTemplateContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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', [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Nwidart\Modules\...hemaParser()->down())); (Nwidart\Modules\Commands\MigrationMakeCommand) is incompatible with the return type declared by the abstract method Nwidart\Modules\Commands...nd::getTemplateContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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', [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Nwidart\Modules\...hemaParser()->down())); (Nwidart\Modules\Commands\MigrationMakeCommand) is incompatible with the return type declared by the abstract method Nwidart\Modules\Commands...nd::getTemplateContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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', [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Nwidart\Modules\...maParser()->render())); (Nwidart\Modules\Commands\MigrationMakeCommand) is incompatible with the return type declared by the abstract method Nwidart\Modules\Commands...nd::getTemplateContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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', [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Nwidart\Modules\...=> $this->getClass())); (Nwidart\Modules\Commands\MigrationMakeCommand) is incompatible with the return type declared by the abstract method Nwidart\Modules\Commands...nd::getTemplateContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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'));
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