Completed
Push — develop ( df9395...08456a )
by Abdelrahman
16:28
created

TransformerMakeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 118
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildClass() 0 8 2
A replaceModel() 0 22 2
A getStub() 0 6 2
A getDefaultNamespace() 0 4 1
A getArguments() 0 6 1
A getOptions() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Console\Commands;
6
7
use Illuminate\Support\Str;
8
use Illuminate\Console\GeneratorCommand;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Cortex\Foundation\Traits\ConsoleMakeModuleCommand;
12
13
class TransformerMakeCommand extends GeneratorCommand
14
{
15
    use ConsoleMakeModuleCommand;
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'make:transformer';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create a new transformer class';
30
31
    /**
32
     * The type of class being generated.
33
     *
34
     * @var string
35
     */
36
    protected $type = 'Transformer';
37
38
    /**
39
     * Build the class with the given name.
40
     *
41
     * @param  string  $name
42
     * @return string
43
     */
44
    protected function buildClass($name)
45
    {
46
        $stub = parent::buildClass($name);
47
48
        $model = $this->option('model');
49
50
        return $model ? $this->replaceModel($stub, $model) : $stub;
0 ignored issues
show
Bug introduced by
It seems like $model defined by $this->option('model') on line 48 can also be of type array; however, Cortex\Foundation\Consol...Command::replaceModel() 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...
51
    }
52
53
    /**
54
     * Replace the model for the given stub.
55
     *
56
     * @param  string  $stub
57
     * @param  string  $model
58
     * @return string
59
     */
60
    protected function replaceModel($stub, $model)
61
    {
62
        $model = str_replace('/', '\\', $model);
63
64
        $namespaceModel = $this->rootNamespace().'\Models\\'.$model;
65
66
        if (Str::startsWith($model, '\\')) {
67
            $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
68
        } else {
69
            $stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub);
70
        }
71
72
        $stub = str_replace(
73
            "use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub
74
        );
75
76
        $model = class_basename(trim($model, '\\'));
77
78
        $stub = str_replace('DummyModel', $model, $stub);
79
80
        return str_replace('dummyModel', Str::camel($model), $stub);
81
    }
82
83
    /**
84
     * Get the stub file for the generator.
85
     *
86
     * @return string
87
     */
88
    protected function getStub()
89
    {
90
        return $this->option('model')
91
            ? __DIR__.'/../../../resources/stubs/transformer.stub'
92
            : __DIR__.'/../../../resources/stubs/transformer.plain.stub';
93
    }
94
95
    /**
96
     * Get the default namespace for the class.
97
     *
98
     * @param  string  $rootNamespace
99
     * @return string
100
     */
101
    protected function getDefaultNamespace($rootNamespace)
102
    {
103
        return $rootNamespace.'\Transformers';
104
    }
105
106
    /**
107
     * Get the console command arguments.
108
     *
109
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
110
     */
111
    protected function getArguments()
112
    {
113
        return [
114
            ['name', InputArgument::REQUIRED, 'The name of the transformer.'],
115
        ];
116
    }
117
118
    /**
119
     * Get the console command arguments.
120
     *
121
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
122
     */
123
    protected function getOptions()
124
    {
125
        return [
126
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the transformer applies to.'],
127
            ['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'],
128
        ];
129
    }
130
}
131