Passed
Push — master ( 9c82b2...472045 )
by Prateek
07:54 queued 04:44
created

Module   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 54
eloc 105
dl 0
loc 221
rs 6.4799
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getParentColumns() 0 10 3
A getFileColumns() 0 15 5
A getModelNamePlural() 0 3 1
A getGalleries() 0 10 3
A getForeignColumns() 0 15 5
A getModelNameLowercase() 0 3 1
A getName() 0 3 1
A getModelName() 0 3 1
A getNativeData() 0 10 3
A getModuleDisplayName() 0 3 1
A __construct() 0 12 3
A getMultipleColumns() 0 3 1
A getData() 0 6 1
A getWritableColumns() 0 10 3
A getModuleName() 0 3 1
A getNativeColumns() 0 17 6
A getPivotName() 0 5 1
A getForeignData() 0 19 5
A getPivotTableName() 0 5 1
A getBackendColumnTitles() 0 10 4
A getDisplayColumn() 0 6 4

How to fix   Complexity   

Complex Class

Complex classes like Module often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Module, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Prateekkarki\Laragen\Models;
3
use Prateekkarki\Laragen\Models\DataOption;
4
5
class Module
6
{
7
    protected $module;
8
9
    protected $data;
10
11
    protected $name;
12
13
    public function __construct($moduleName, $moduleData)
14
    {
15
        $this->module = (object) $moduleData;
16
        $this->data = array_filter($moduleData, function($elem){
17
            return (is_array($elem)) ? false : true;
18
        });
19
        $this->multipleData = [];
0 ignored issues
show
Bug Best Practice introduced by
The property multipleData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->multipleData[] = array_filter($moduleData, function($elem){
21
            return (is_array($elem)) ? true : false;
22
        });
23
24
        $this->name = $moduleName;
25
    }
26
27
    public function getName()
28
    {
29
        return $this->name;
30
    }
31
32
    public function getMultipleColumns()
33
    {
34
        return $this->multipleData;
35
    }
36
37
    public function getData()
38
    {
39
        $this->data['sort'] = 'integer';
40
        $this->data['status'] = 'boolean';
41
42
        return $this->data;
43
    }
44
45
    public function getBackendColumnTitles()
46
    {
47
        $data = ['S.N.'];
48
        foreach ($this->data as $column => $optionString) {
49
            $optionArray = explode('|', $optionString);
50
            if (in_array($optionArray[0], ['string', 'int'])&&in_array($column, ['title', 'firstname', 'lastname', 'name'])) {
51
                $data[] = ucwords($column);
52
            }
53
        }
54
        return array_merge($data, ['Last Updated', 'Status', 'Actions']);
55
    }
56
57
    public function getNativeColumns()
58
    {
59
        $data = [];
60
        foreach ($this->data as $column => $optionString) {
61
            if (is_array($optionString)) continue;
62
            $optionArray = explode('|', $optionString);
63
            if (in_array($optionArray[0], DataOption::$types)) {
64
                $data[] = $column;
65
            }
66
        }
67
        if($this->getForeignColumns()){
68
            foreach($this->getForeignColumns() as $relation => $tablename ){
69
                $columnName = array_values($tablename)[0];
70
                $data[] = str_singular($columnName).'_id';
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
                $data[] = /** @scrutinizer ignore-deprecated */ str_singular($columnName).'_id';

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

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

Loading history...
71
            }
72
        }
73
        return $data;
74
    }
75
76
    public function getNativeData()
77
    {
78
        $data = [];
79
        foreach ($this->data as $column => $optionString) {
80
            $optionArray = explode('|', $optionString);
81
            if (in_array($optionArray[0], DataOption::$types)) {
82
                $data[] = [$column => $optionArray[0]];
83
            }
84
        }
85
        return $data;
86
    }
87
88
    public function getWritableColumns()
89
    {
90
        $data = [];
91
        foreach ($this->data as $column => $optionString) {
92
            $optionArray = explode('|', $optionString);
93
            if (in_array($optionArray[0], DataOption::$types)) {
94
                $data[] = [$column => $optionArray[0]];
95
            }
96
        }
97
        return $data;
98
    }
99
100
    public function getFileColumns($type = 'all')
101
    {
102
        if (is_array($type))
103
            $types = $type;
104
        else
105
            $types = ($type == "all") ? DataOption::$fileTypes : [$type];
106
        
107
        $data = [];
108
        foreach ($this->data as $column => $optionString) {
109
            $dataOption = new DataOption($column, $optionString);
110
            if (in_array($dataOption->getType(), $types)) {
111
                $data[] = $column;
112
            }
113
        }
114
        return $data;
115
    }
116
117
    public function getParentColumns()
118
    {
119
        $data = [];
120
        foreach ($this->data as $column => $optionString) {
121
            $dataOption = new DataOption($column, $optionString);
122
            if ($dataOption->getType() == DataOption::TYPE_PARENT) {
123
                $data[] = $column;
124
            }
125
        }
126
        return $data;
127
    }
128
129
    public function getGalleries()
130
    {
131
        $data = [];
132
        foreach ($this->data as $column => $optionString) {
133
            $dataOption = new DataOption($column, $optionString);
134
            if ($dataOption->getType() == 'gallery') {
135
                $data[] = $column;
136
            }
137
        }
138
        return $data;
139
    }
140
141
    public function getForeignColumns($type = 'all')
142
    {
143
        if (is_array($type))
144
            $types = $type;
145
        else
146
            $types = ($type == "all") ? DataOption::$specialTypes : [$type];
147
        
148
        $data = [];
149
        foreach ($this->data as $column => $optionString) {
150
            $dataOption = new DataOption($column, $optionString);
151
            if (in_array($dataOption->getType(), $types)) {
152
                $data[] = [$column => $dataOption->getParentModule()];
153
            }
154
        }
155
        return $data;
156
    }
157
158
    public function getForeignData($type = 'all')
159
    {
160
        if (is_array($type))
161
            $types = $type;
162
        else
163
            $types = ($type == "all") ? DataOption::$specialTypes : [$type];
164
        
165
        $data = [];
166
        foreach ($this->data as $column => $optionString) {
167
            $dataOption = new DataOption($column, $optionString);
168
            if (in_array($dataOption->getType(), $types)) {
169
                $data[] = [
170
                    'columnName'   => $column,
171
                    'parentModule' => $dataOption->getParentModule(),
172
                    'parentModel'  => $dataOption->getParentModel()
173
                ];
174
            }
175
        }
176
        return $data;
177
    }
178
179
    public function getPivotName($related)
180
    {
181
        $modelArray = [$this->getModelName(), ucfirst(camel_case(str_singular($related)))];
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

181
        $modelArray = [$this->getModelName(), ucfirst(camel_case(/** @scrutinizer ignore-deprecated */ str_singular($related)))];

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

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

Loading history...
Deprecated Code introduced by
The function camel_case() has been deprecated: Str::camel() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

181
        $modelArray = [$this->getModelName(), ucfirst(/** @scrutinizer ignore-deprecated */ camel_case(str_singular($related)))];

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

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

Loading history...
182
        sort($modelArray);
183
        return implode("", $modelArray);
184
    }
185
186
    public function getPivotTableName($related)
187
    {
188
        $moduleArray = [str_singular($this->getModelNameLowercase()), str_singular($related)];
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

188
        $moduleArray = [str_singular($this->getModelNameLowercase()), /** @scrutinizer ignore-deprecated */ str_singular($related)];

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

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

Loading history...
189
        sort($moduleArray);
190
        return implode("_", $moduleArray);
191
    }
192
193
    public function getModuleName()
194
    {
195
        return $this->name;
196
    }
197
198
    public function getModuleDisplayName()
199
    {
200
        return ucfirst(str_replace('_', '', $this->name));
201
    }
202
203
    public function getDisplayColumn()
204
    {
205
        foreach ($this->data as $column => $optionString) {
206
            $optionArray = explode('|', $optionString);
207
            if (in_array($optionArray[0], ['string', 'int'])&&in_array($column, ['title', 'firstname', 'lastname', 'name'])) {
208
                return $column;
209
            }
210
        }
211
    }
212
213
    public function getModelName()
214
    {
215
        return ucfirst(camel_case(str_singular($this->name)));
0 ignored issues
show
Deprecated Code introduced by
The function camel_case() has been deprecated: Str::camel() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

215
        return ucfirst(/** @scrutinizer ignore-deprecated */ camel_case(str_singular($this->name)));

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

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

Loading history...
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

215
        return ucfirst(camel_case(/** @scrutinizer ignore-deprecated */ str_singular($this->name)));

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

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

Loading history...
216
    }
217
218
    public function getModelNamePlural()
219
    {
220
        return ucfirst(camel_case($this->name));
0 ignored issues
show
Deprecated Code introduced by
The function camel_case() has been deprecated: Str::camel() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

220
        return ucfirst(/** @scrutinizer ignore-deprecated */ camel_case($this->name));

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

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

Loading history...
221
    }
222
223
    public function getModelNameLowercase()
224
    {
225
        return str_singular($this->name);
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

225
        return /** @scrutinizer ignore-deprecated */ str_singular($this->name);

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

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

Loading history...
226
    }
227
}
228