Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

src/Console/FormCommand.php (1 issue)

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 Encore\Admin\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
7
class FormCommand extends GeneratorCommand
8
{
9
    /**
10
     * The console command name.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'admin:form {name} 
15
        {--title=}
16
        {--step}
17
        {--namespace=}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Make admin form widget';
25
26
    /**
27
     * Replace the class name for the given stub.
28
     *
29
     * @param string $stub
30
     * @param string $name
31
     *
32
     * @return string
33
     */
34
    protected function replaceClass($stub, $name)
35
    {
36
        $stub = parent::replaceClass($stub, $name);
37
38
        return str_replace('DummyTitle', $this->option('title'), $stub);
39
    }
40
41
    /**
42
     * Get the stub file for the generator.
43
     *
44
     * @return string
45
     */
46
    protected function getStub()
47
    {
48
        if ($this->option('step')) {
49
            return __DIR__.'/stubs/step-form.stub';
50
        }
51
52
        return __DIR__.'/stubs/form.stub';
53
    }
54
55
    /**
56
     * Get the default namespace for the class.
57
     *
58
     * @param string $rootNamespace
59
     *
60
     * @return string
61
     */
62
    protected function getDefaultNamespace($rootNamespace)
63
    {
64
        if ($namespace = $this->option('namespace')) {
65
            return $namespace;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $namespace; (array|string|boolean) is incompatible with the return type documented by Encore\Admin\Console\For...nd::getDefaultNamespace 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...
66
        }
67
68
        return str_replace('Controllers', 'Forms', config('admin.route.namespace'));
69
    }
70
71
    /**
72
     * Get the desired class name from the input.
73
     *
74
     * @return string
75
     */
76
    protected function getNameInput()
77
    {
78
        $name = trim($this->argument('name'));
79
80
        $this->type = $this->qualifyClass($name);
81
82
        return $name;
83
    }
84
}
85