Completed
Push — master ( b4d64e...1bc42b )
by recca
02:02
created

ViewMakeCommand::buildClass()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Recca0120\Generator\Console;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\Console\Input\InputOption;
7
8
class ViewMakeCommand extends GeneratorCommand
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'generate:view';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create a new view';
23
24
    /**
25
     * The type of class being generated.
26
     *
27
     * @var string
28
     */
29
    protected $type = 'View';
30
31
    /**
32
     * Get the stub file for the generator.
33
     *
34
     * @return string
35
     */
36 1
    protected function getStub()
37
    {
38 1
        return $this->getStubResource('resources/views/scaffold/'.$this->view().'.blade.stub');
39
    }
40
41
    /**
42
     * Get the default namespace for the class.
43
     *
44
     * @param string $rootNamespace
45
     *
46
     * @return string
47
     */
48 1
    protected function getDefaultNamespace($rootNamespace)
49
    {
50 1
        return $rootNamespace.'\Http\Controllers';
51
    }
52
53
    /**
54
     * Build the class with the given name.
55
     *
56
     * @param string $name
57
     *
58
     * @return string
59
     */
60 1
    protected function buildClass($name)
61
    {
62 1
        $fullBaseClass = $this->getNamespace($name).'\Controller';
63 1
        $fullBaseClass = class_exists($fullBaseClass) === true ?
64 1
            $fullBaseClass : 'App\Http\Controllers\Controller';
65
66 1
        $fullBaseClass = $this->option('extend') ?: $fullBaseClass;
67 1
        $rootNamespace = trim($this->rootNamespace(), '\\');
68 1
        $namespace = $this->getNamespace($name);
69 1
        $baseClass = ltrim(str_replace($namespace, '', $name), '\\');
70 1
        $repositoryContractInterface = $rootNamespace.'\Repositories\Contracts\\'.$baseClass.'Repository';
71 1
        $repositoryClass = $rootNamespace.'\Repositories\\'.$baseClass.'Repository';
0 ignored issues
show
Unused Code introduced by
$repositoryClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72 1
        $requestClass = $rootNamespace.'\Http\Requests\\'.$baseClass.'Request';
73
74 1
        return $this->generator->setFullControllerClass($name.'Controller')
75 1
            ->setFullBaseClass($fullBaseClass)
0 ignored issues
show
Bug introduced by
It seems like $fullBaseClass defined by $this->option('extend') ?: $fullBaseClass on line 66 can also be of type array; however, Recca0120\Generator\Generator::setFullBaseClass() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76 1
            ->setFullRepositoryInterface($repositoryContractInterface)
77 1
            ->setFullRequestClass($requestClass)
78 1
            ->render($this->getStub());
79
    }
80
81
    /**
82
     * Get the destination class path.
83
     *
84
     * @param string $name
85
     *
86
     * @return string
87
     */
88 1
    protected function getPath($name)
89
    {
90 1
        $name = Str::replaceFirst($this->rootNamespace().'Http\Controllers\\', '', $name);
91 1
        $path = $this->laravel->basePath().'/resources/views/';
92
93 1
        return $path .= implode('/', array_map(function ($path) {
94 1
            return Str::snake($path, '-');
95 1
        }, explode('\\', $name))).'/'.$this->view().'.blade.php';
96
    }
97
98
    /**
99
     * view.
100
     *
101
     * @return string
102
     */
103 1
    protected function view()
104
    {
105 1
        return $this->option('view') ?: 'index';
106
    }
107
108
    /**
109
     * Get the console command options.
110
     *
111
     * @return array
112
     */
113 1 View Code Duplication
    protected function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        return [
116 1
            ['extend', '', InputOption::VALUE_OPTIONAL, 'controller extend.'],
117 1
            ['view', '', InputOption::VALUE_OPTIONAL, 'view'],
118 1
        ];
119
    }
120
}
121