Completed
Pull Request — develop (#47)
by Marcus
01:39
created

DataTableMakeCommand::getStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 DataTableMakeCommand extends GeneratorCommand
14
{
15
    use ConsoleMakeModuleCommand;
16
17
    /**
18
     * The model name.
19
     *
20
     * @var string
21
     */
22
    protected $modelName;
23
24
    /**
25
     * The transformer name.
26
     *
27
     * @var string
28
     */
29
    protected $transformerName;
30
31
    /**
32
     * The console command name.
33
     *
34
     * @var string
35
     */
36
    protected $name = 'make:datatable';
37
38
    /**
39
     * The console command description.
40
     *
41
     * @var string
42
     */
43
    protected $description = 'Create a new datatable class';
44
45
    /**
46
     * The type of class being generated.
47
     *
48
     * @var string
49
     */
50
    protected $type = 'Datatable';
51
52
    /**
53
     * Build the class with the given name.
54
     *
55
     * @param string $name
56
     *
57
     * @return string
58
     */
59
    protected function buildClass($name)
60
    {
61
        $stub = parent::buildClass($name);
62
63
        $model = $this->option('model') ?? $this->modelName = $this->ask('What is your model?');
64
65
        $transformer = $this->option('transformer') ?? $this->transformerName = $this->ask('What is your transformer?');
66
67
        return $this->replaceClasses($stub, $model, $transformer);
0 ignored issues
show
Bug introduced by
It seems like $model defined by $this->option('model') ?...'What is your model?')) on line 63 can also be of type array; however, Cortex\Foundation\Consol...mmand::replaceClasses() 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...
Bug introduced by
It seems like $transformer defined by $this->option('transform...is your transformer?')) on line 65 can also be of type array; however, Cortex\Foundation\Consol...mmand::replaceClasses() does only seem to accept string|null, 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...
68
    }
69
70
    /**
71
     * Replace the model and transformer for the given stub.
72
     *
73
     * @param string $stub
74
     * @param string $model
75
     * @param string $transformer
0 ignored issues
show
Documentation introduced by
Should the type for parameter $transformer not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
76
     *
77
     * @return string
78
     */
79
    protected function replaceClasses($stub, $model, $transformer = null)
80
    {
81
        if ($transformer) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $transformer of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
82
            $transformer = str_replace('/', '\\', $transformer);
83
84
            $namespaceTransformer = $this->rootNamespace().'\Transformers\\'.$transformer;
85
86
            if (Str::startsWith($transformer, '\\')) {
87
                $stub = str_replace('NamespacedDummyTransformer', trim($transformer, '\\'), $stub);
88
            } else {
89
                $stub = str_replace('NamespacedDummyTransformer', $namespaceTransformer, $stub);
90
            }
91
92
            $stub = str_replace(
93
                "use {$namespaceTransformer};\nuse {$namespaceTransformer};", "use {$namespaceTransformer};", $stub
94
            );
95
96
            $transformer = class_basename(trim($transformer, '\\'));
97
98
            $stub = str_replace('DummyTransformer', $transformer, $stub);
99
100
            $stub = str_replace('dummyTransformer', Str::camel($transformer), $stub);
101
        }
102
103
        $model = str_replace('/', '\\', $model);
104
105
        $namespaceModel = $this->rootNamespace().'\Models\\'.$model;
106
107
        if (Str::startsWith($model, '\\')) {
108
            $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
109
        } else {
110
            $stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub);
111
        }
112
113
        $stub = str_replace(
114
            "use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub
115
        );
116
117
        $model = class_basename(trim($model, '\\'));
118
119
        $stub = str_replace('DummyModel', $model, $stub);
120
121
        return str_replace('dummyModel', Str::camel($model), $stub);
122
    }
123
124
    /**
125
     * Get the stub file for the generator.
126
     *
127
     * @return string
128
     */
129
    protected function getStub()
130
    {
131
        return __DIR__.'/../../../resources/stubs/datatable.stub';
132
    }
133
134
    /**
135
     * Get the default namespace for the class.
136
     *
137
     * @param string $rootNamespace
138
     *
139
     * @return string
140
     */
141
    protected function getDefaultNamespace($rootNamespace)
142
    {
143
        return $rootNamespace.'\Datatables';
144
    }
145
146
    /**
147
     * Get the console command arguments.
148
     *
149
     * @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...
150
     */
151
    protected function getArguments()
152
    {
153
        return [
154
            ['name', InputArgument::REQUIRED, 'The name of the datatable.'],
155
        ];
156
    }
157
158
    /**
159
     * Get the console command arguments.
160
     *
161
     * @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...
162
     */
163
    protected function getOptions()
164
    {
165
        return [
166
            ['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the datatable applies to.'],
167
            ['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'],
168
            ['transformer', 't', InputOption::VALUE_REQUIRED, 'The transformer that the datatable applies to.'],
169
        ];
170
    }
171
}
172