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

DataTablesHtmlCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 29.55 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 26
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildClass() 0 14 1
A getStub() 8 8 2
A qualifyClass() 18 18 4

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
7
class DataTablesHtmlCommand extends DataTablesMakeCommand
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'datatables:html
15
                            {name : The name of the datatable html.}
16
                            {--dom= : The dom of the datatable.}
17
                            {--buttons= : The buttons of the datatable.}
18
                            {--table= : Scaffold columns from the table.}
19
                            {--columns= : The columns of the datatable.}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new dataTable html class.';
27
28
    /**
29
     * The type of class being generated.
30
     *
31
     * @var string
32
     */
33
    protected $type = 'DataTableHtml';
34
35
    /**
36
     * Build the class with the given name.
37
     *
38
     * @param string $name
39
     * @return string
40
     */
41
    protected function buildClass($name)
42
    {
43
        $stub = $this->files->get($this->getStub());
44
45
        $stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
46
47
        $this->replaceBuilder($stub)
48
             ->replaceColumns($stub)
49
             ->replaceButtons($stub)
50
             ->replaceDOM($stub)
51
             ->replaceTableId($stub);
52
53
        return $stub;
54
    }
55
56
    /**
57
     * Get the stub file for the generator.
58
     *
59
     * @return string
60
     */
61 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...
62
    {
63
        $config = $this->laravel['config'];
64
65
        return $config->get('datatables-buttons.stub')
66
            ? base_path() . $config->get('datatables-buttons.stub') . '/html.stub'
67
            : __DIR__ . '/stubs/html.stub';
68
    }
69
70
    /**
71
     * Parse the name and format according to the root namespace.
72
     *
73
     * @param string $name
74
     * @return string
75
     */
76 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...
77
    {
78
        $rootNamespace = $this->laravel->getNamespace();
79
80
        if (Str::startsWith($name, $rootNamespace)) {
81
            return $name;
82
        }
83
84
        if (Str::contains($name, '/')) {
85
            $name = str_replace('/', '\\', $name);
86
        }
87
88
        if (! Str::contains(Str::lower($name), 'datatable')) {
89
            $name .= 'DataTableHtml';
90
        }
91
92
        return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
93
    }
94
}
95