Completed
Pull Request — master (#99)
by Arjay
01:18
created

DataTablesHtmlCommand   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 281
Duplicated Lines 17.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 3
dl 49
loc 281
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A buildClass() 0 10 1
A replaceTableId() 0 8 1
A replaceDOM() 0 10 2
A replaceButtons() 0 8 1
A getButtons() 0 13 2
A parseButtons() 3 21 4
A replaceColumns() 0 8 1
A getColumns() 0 17 3
A parseColumns() 3 14 4
A replaceBuilder() 9 9 1
A qualifyClass() 18 18 4
A getDefaultNamespace() 0 4 1
A getStub() 8 8 2
A getOptions() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\DataTables\Generators;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\Schema;
7
use Illuminate\Console\GeneratorCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class DataTablesHtmlCommand extends GeneratorCommand
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'datatables:html
18
                            {name : The name of the datatable html.}
19
                            {--dom= : The dom of the datatable.}
20
                            {--buttons= : The buttons of the datatable.}
21
                            {--table= : Scaffold columns from the table.}
22
                            {--columns= : The columns of the datatable.}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create a new dataTable html class.';
30
31
    /**
32
     * The type of class being generated.
33
     *
34
     * @var string
35
     */
36
    protected $type = 'DataTableHtml';
37
38
    /**
39
     * Build the class with the given name.
40
     *
41
     * @param string $name
42
     * @return string
43
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
44
     */
45
    protected function buildClass($name)
46
    {
47
        $stub = parent::buildClass($name);
48
49
        return $this->replaceBuilder($stub)
50
                    ->replaceColumns($stub)
51
                    ->replaceButtons($stub)
52
                    ->replaceDOM($stub)
53
                    ->replaceTableId($stub);
54
    }
55
56
    /**
57
     * Replace columns.
58
     *
59
     * @param string $stub
60
     * @return string
61
     */
62
    protected function replaceTableId(&$stub)
63
    {
64
        $stub = str_replace(
65
            'DummyTableId', Str::lower($this->getNameInput()) . '-table', $stub
66
        );
67
68
        return $stub;
69
    }
70
71
    /**
72
     * Replace dom.
73
     *
74
     * @param string $stub
75
     * @return $this
76
     */
77
    protected function replaceDOM(&$stub)
78
    {
79
        $stub = str_replace(
80
            'DummyDOM',
81
            $this->option('dom') ?: $this->laravel['config']->get('datatables-buttons.generator.dom', 'Bfrtip'),
82
            $stub
83
        );
84
85
        return $this;
86
    }
87
88
    /**
89
     * Replace buttons.
90
     *
91
     * @param string $stub
92
     * @return $this
93
     */
94
    protected function replaceButtons(&$stub)
95
    {
96
        $stub = str_replace(
97
            'DummyButtons', $this->getButtons(), $stub
98
        );
99
100
        return $this;
101
    }
102
103
    /**
104
     * Get the columns to be used.
105
     *
106
     * @return string
107
     */
108
    protected function getButtons()
109
    {
110
        if ($this->option('buttons') != '') {
111
            return $this->parseButtons($this->option('buttons'));
112
        } else {
113
            return $this->parseButtons(
114
                $this->laravel['config']->get(
115
                    'datatables-buttons.generator.buttons',
116
                    'create,export,print,reset,reload'
117
                )
118
            );
119
        }
120
    }
121
122
    /**
123
     * Parse array from definition.
124
     *
125
     * @param string $definition
126
     * @param int $indentation
127
     * @return string
128
     */
129
    protected function parseButtons($definition, $indentation = 24)
130
    {
131
        $columns = explode(',', $definition);
132
        $stub    = '';
133
        foreach ($columns as $key => $column) {
134
            $indent    = '';
135
            $separator = ',';
136
137 View Code Duplication
            if ($key < count($columns) - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
138
                $indent = PHP_EOL . str_repeat(' ', $indentation);
139
            }
140
141
            if ($key == count($columns) - 1) {
142
                $separator = '';
143
            }
144
145
            $stub .= "Button::make('{$column}')" . $separator . $indent;
146
        }
147
148
        return $stub;
149
    }
150
151
    /**
152
     * Replace columns.
153
     *
154
     * @param string $stub
155
     * @return $this
156
     */
157
    protected function replaceColumns(&$stub)
158
    {
159
        $stub = str_replace(
160
            'DummyColumns', $this->getColumns(), $stub
161
        );
162
163
        return $this;
164
    }
165
166
    /**
167
     * Get the columns to be used.
168
     *
169
     * @return string
170
     */
171
    protected function getColumns()
172
    {
173
        if ($this->option('table')) {
174
            return $this->parseColumns(Schema::getColumnListing($this->option('table')));
175
        }
176
177
        if ($this->option('columns') != '') {
178
            return $this->parseColumns($this->option('columns'));
179
        } else {
180
            return $this->parseColumns(
181
                $this->laravel['config']->get(
182
                    'datatables-buttons.generator.columns',
183
                    'id,add your columns,created_at,updated_at'
184
                )
185
            );
186
        }
187
    }
188
189
    /**
190
     * Parse array from definition.
191
     *
192
     * @param string $definition
193
     * @param int $indentation
194
     * @return string
195
     */
196
    protected function parseColumns($definition, $indentation = 12)
197
    {
198
        $columns = is_array($definition) ? $definition : explode(',', $definition);
199
        $stub    = '';
200
        foreach ($columns as $key => $column) {
201
            $stub .= "Column::make('{$column}'),";
202
203 View Code Duplication
            if ($key < count($columns) - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
204
                $stub .= PHP_EOL . str_repeat(' ', $indentation);
205
            }
206
        }
207
208
        return $stub;
209
    }
210
211
    /**
212
     * Replace builder name.
213
     *
214
     * @param string $stub
215
     * @return self
216
     */
217 View Code Duplication
    protected function replaceBuilder(&$stub)
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...
218
    {
219
        $name  = $this->qualifyClass($this->getNameInput());
220
        $class = str_replace($this->getNamespace($name) . '\\', '', $name);
221
222
        $stub = str_replace('DummyBuilder', $class . 'Html', $stub);
223
224
        return $this;
225
    }
226
227
    /**
228
     * Parse the name and format according to the root namespace.
229
     *
230
     * @param string $name
231
     * @return string
232
     */
233 View Code Duplication
    protected function qualifyClass($name)
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...
234
    {
235
        $rootNamespace = $this->laravel->getNamespace();
236
237
        if (Str::startsWith($name, $rootNamespace)) {
238
            return $name;
239
        }
240
241
        if (Str::contains($name, '/')) {
242
            $name = str_replace('/', '\\', $name);
243
        }
244
245
        if (! Str::contains(Str::lower($name), 'datatable')) {
246
            $name .= 'DataTableHtml';
247
        }
248
249
        return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
250
    }
251
252
    /**
253
     * Get the default namespace for the class.
254
     *
255
     * @param string $rootNamespace
256
     * @return string
257
     */
258
    protected function getDefaultNamespace($rootNamespace)
259
    {
260
        return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables');
261
    }
262
263
    /**
264
     * Get the stub file for the generator.
265
     *
266
     * @return string
267
     */
268 View Code Duplication
    protected function getStub()
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...
269
    {
270
        $config = $this->laravel['config'];
271
272
        return $config->get('datatables-buttons.stub')
273
            ? base_path() . $config->get('datatables-buttons.stub') . '/html.stub'
274
            : __DIR__ . '/stubs/html.stub';
275
    }
276
277
    /**
278
     * Get the console command options.
279
     *
280
     * @return array
281
     */
282 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...
283
    {
284
        return [
285
            ['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null],
286
            ['buttons', null, InputOption::VALUE_OPTIONAL, 'Use the provided buttons.', null],
287
            ['dom', null, InputOption::VALUE_OPTIONAL, 'Use the provided DOM.', null],
288
        ];
289
    }
290
}
291