DataTableMakeCommand::getArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Console\Commands;
6
7
use Illuminate\Console\ConfirmableTrait;
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 ConfirmableTrait;
16
    use ConsoleMakeModuleCommand;
17
18
    /**
19
     * The model name.
20
     *
21
     * @var string
22
     */
23
    protected $modelName;
24
25
    /**
26
     * The transformer name.
27
     *
28
     * @var string
29
     */
30
    protected $transformerName;
31
32
    /**
33
     * The console command name.
34
     *
35
     * @var string
36
     */
37
    protected $name = 'make:datatable';
38
39
    /**
40
     * The console command description.
41
     *
42
     * @var string
43
     */
44
    protected $description = 'Create a new datatable class';
45
46
    /**
47
     * The type of class being generated.
48
     *
49
     * @var string
50
     */
51
    protected $type = 'Datatable';
52
53
    /**
54
     * Build the class with the given name.
55
     *
56
     * @param string $name
57
     *
58
     * @return string
59
     */
60
    protected function buildClass($name): string
61
    {
62
        $stub = parent::buildClass($name);
63
64
        $model = $this->option('model') ?? $this->modelName = $this->ask('What is your model?');
65
66
        $transformer = $this->option('transformer') ?? $this->transformerName = $this->ask('What is your transformer?');
67
68
        return $this->replaceClasses($stub, $model, $transformer);
69
    }
70
71
    /**
72
     * Replace the model and transformer for the given stub.
73
     *
74
     * @param string $stub
75
     * @param string $model
76
     * @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...
77
     *
78
     * @return string
79
     */
80
    protected function replaceClasses($stub, $model, $transformer = null): string
81
    {
82
        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...
83
            $transformer = str_replace('/', '\\', $transformer);
84
85
            $namespaceTransformer = $this->rootNamespace().'\Transformers\\'.$transformer;
86
87
            if (starts_with($transformer, '\\')) {
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated with message: Str::startsWith() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
88
                $stub = str_replace('NamespacedDummyTransformer', trim($transformer, '\\'), $stub);
89
            } else {
90
                $stub = str_replace('NamespacedDummyTransformer', $namespaceTransformer, $stub);
91
            }
92
93
            $stub = str_replace(
94
                "use {$namespaceTransformer};\nuse {$namespaceTransformer};", "use {$namespaceTransformer};", $stub
95
            );
96
97
            $transformer = class_basename(trim($transformer, '\\'));
98
99
            $stub = str_replace('DummyTransformer', $transformer, $stub);
100
101
            $stub = str_replace('dummyTransformer', camel_case($transformer), $stub);
0 ignored issues
show
Deprecated Code introduced by
The function camel_case() has been deprecated with message: Str::camel() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
102
        }
103
104
        $model = str_replace('/', '\\', $model);
105
106
        $namespaceModel = $this->rootNamespace().'\Models\\'.$model;
107
108
        if (starts_with($model, '\\')) {
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated with message: Str::startsWith() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
109
            $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
110
        } else {
111
            $stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub);
112
        }
113
114
        $stub = str_replace(
115
            "use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub
116
        );
117
118
        $model = class_basename(trim($model, '\\'));
119
120
        $stub = str_replace('DummyModel', $model, $stub);
121
122
        return str_replace('dummyModel', camel_case($model), $stub);
0 ignored issues
show
Deprecated Code introduced by
The function camel_case() has been deprecated with message: Str::camel() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
123
    }
124
125
    /**
126
     * Get the stub file for the generator.
127
     *
128
     * @return string
129
     */
130
    protected function getStub(): string
131
    {
132
        return __DIR__.'/../../../resources/stubs/datatable.stub';
133
    }
134
135
    /**
136
     * Get the default namespace for the class.
137
     *
138
     * @param string $rootNamespace
139
     *
140
     * @return string
141
     */
142
    protected function getDefaultNamespace($rootNamespace): string
143
    {
144
        return $rootNamespace.'\Datatables';
145
    }
146
147
    /**
148
     * Get the console command arguments.
149
     *
150
     * @return array
151
     */
152
    protected function getArguments(): array
153
    {
154
        return [
155
            ['name', InputArgument::REQUIRED, 'The name of the datatable.'],
156
        ];
157
    }
158
159
    /**
160
     * Get the console command arguments.
161
     *
162
     * @return array
163
     */
164
    protected function getOptions(): array
165
    {
166
        return [
167
            ['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the datatable applies to.'],
168
            ['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'],
169
            ['transformer', 't', InputOption::VALUE_REQUIRED, 'The transformer that the datatable applies to.'],
170
        ];
171
    }
172
}
173