Passed
Branch master (d88b3b)
by Prateek
03:56
created

LaragenType   F

Complexity

Total Complexity 61

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 96
c 0
b 0
f 0
dl 0
loc 229
rs 3.52
wmc 61

31 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 7
A getChildModel() 0 3 2
A getDataType() 0 2 1
A optionArray() 0 2 1
A getParentModelLowercase() 0 3 1
A getFilteredColumns() 0 13 6
A getDisplay() 0 3 1
A setSize() 0 2 1
A getValidationRule() 0 2 1
A getColumn() 0 3 1
A __call() 0 12 6
A getValidationLine() 0 17 3
A getTextRows() 0 5 2
A getFormOptions() 0 4 2
A getTabs() 0 7 2
A getParentModel() 0 3 1
A getRelatedModelLowercase() 0 3 1
A isUnique() 0 2 1
A getRelatedModule() 0 3 1
A setOptions() 0 9 2
A getSchema() 0 9 4
A isRelational() 0 3 1
A setIsDisplay() 0 2 2
A getRelatedModel() 0 3 1
A getStub() 0 3 2
A setUnique() 0 2 2
A setRequired() 0 2 2
A getParentModule() 0 3 1
A getColumnKey() 0 3 1
A getForeignKey() 0 3 1
A isRequired() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like LaragenType 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 LaragenType, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Prateekkarki\Laragen\Models\Types;
3
4
use Illuminate\Support\Str;
5
use Prateekkarki\Laragen\Models\TypeResolver;
6
7
abstract class LaragenType
8
{
9
    protected $uniqueFlag;
10
    protected $requiredFlag;
11
    protected $isDisplay;
12
	protected $dataType;
13
	protected $formType;
14
	protected $stubs = [];
15
	protected $size = false;
16
	protected $validationRule = null;
17
	protected $moduleName;
18
	protected $columnName;
19
	protected $optionString;
20
	
21
    public function __construct($moduleName, $columnName, $optionString)
22
    {
23
        $this->moduleName = $moduleName;
24
        $this->columnName = $columnName;
25
        $this->optionString = $optionString;
26
27
        $this->optionArray = is_string($optionString) ? explode('|', $optionString) : [];
0 ignored issues
show
Bug Best Practice introduced by
The property optionArray does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $typePieces = array_shift($this->optionArray);
29
        $type = explode(':', $typePieces);
30
        $this->typeOption = is_array($type) && count($type) >= 2 ? $type[1] : false;
0 ignored issues
show
Bug Best Practice introduced by
The property typeOption does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
32
        if(in_array(TypeResolver::COLUMN_UNIQUE, $this->optionArray)){
33
            $this->setUnique();
34
        }
35
        if(in_array(TypeResolver::COLUMN_REQUIRED, $this->optionArray)){
36
            $this->setRequired();
37
        }
38
        if(in_array("*", $this->optionArray)){
39
            $this->setIsDisplay();
40
        }
41
    }
42
    
43
    function __call($method, $params) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
44
        $var = lcfirst(substr($method, 3));
45
        
46
        if (strncasecmp($method, "get", 3) === 0) {
47
            return property_exists($this, $var) ? $this->$var : "";
48
        }
49
50
        if (strncasecmp($method, "set", 3) === 0 && isset($params[0])) {
51
            $this->$var = $params[0];
52
        }
53
54
        return property_exists($this, $method) ? $this->$method : "";
55
   }
56
	
57
    public function isRelational()
58
    {
59
       return $this->relationalType;
60
    }
61
62
    public function getSchema()
63
    {
64
        $schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'";
65
        $schema .= $this->getSize() ? ", {$this->getSize()})" : ")";
0 ignored issues
show
Bug introduced by
The method getSize() does not exist on Prateekkarki\Laragen\Models\Types\LaragenType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

65
        $schema .= $this->/** @scrutinizer ignore-call */ getSize() ? ", {$this->getSize()})" : ")";
Loading history...
66
        $schema .= $this->isUnique() ? "->unique()" : "";
67
        $schema .= $this->isRequired() ? "" : "->nullable()";
68
        $schema .= ";";
69
70
        return $schema;
71
	}
72
73
    public function getValidationLine()
74
    {
75
        $validationSegments = [];
76
        $modelname = strtolower(Str::camel(str_singular($this->moduleName)));
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

76
        $modelname = strtolower(Str::camel(/** @scrutinizer ignore-deprecated */ str_singular($this->moduleName)));

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...
77
78
        $validationSegments[] = $this->isRequired() ? 'required' : 'nullable';
79
        $validationSegments[] = $this->getValidationRule() ?? $this->getDataType();
80
        $rules = implode('|', $validationSegments);
81
82
        if ($this->isUnique()) {
83
            $validationLine = '($this->'.$modelname.') ? \'';
84
            $validationLine .= $rules . '|unique:'.$this->moduleName.','.$this->getColumn().','.'\''.'.$this->'.$modelname.'->id : \'';
85
            $validationLine .= $rules . '|unique:'.$this->moduleName.'\'';
86
        } else{
87
            $validationLine = "'{$rules}'";
88
        }
89
        return $validationLine;
90
    }
91
92
    public function getFormOptions() {
93
        $options = "";
94
        $options .= $this->isRequired() ? 'required="required" ' : '';
95
        return $options;
96
    }
97
    
98
    
99
    public function getForeignKey()
100
    {
101
        return $this->columnName . "_id";
102
    }
103
104
    public function getFilteredColumns($options = [], $columnsOnly = false)
105
    {
106
        $filteredTypes = [];
107
        $options = is_array($options) ? $options : [$options];
108
        foreach($this->getPivotColumns() as $type){
0 ignored issues
show
Bug introduced by
The method getPivotColumns() does not exist on Prateekkarki\Laragen\Models\Types\LaragenType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

108
        foreach($this->/** @scrutinizer ignore-call */ getPivotColumns() as $type){
Loading history...
109
            foreach ($options as $option) {
110
                if($type->$option()){
111
                    $filteredTypes[] = $columnsOnly ? $type->getColumn() : $type;
112
                    break;
113
                }
114
            }
115
        }
116
        return $filteredTypes;
117
    }
118
119
    public function getRelatedModel()
120
    {
121
        return $this->getChildModel();
122
    }
123
124
    public function getRelatedModule()
125
    {
126
        return Str::snake(Str::plural($this->getRelatedModel()));
127
    }
128
129
    public function getRelatedModelLowercase()
130
    {
131
        return strtolower($this->getRelatedModel());
132
    }
133
134
    public function getChildModel()
135
    {
136
        return ucfirst(Str::camel(Str::singular($this->typeOption ?: $this->columnName )));
0 ignored issues
show
Bug introduced by
It seems like $this->typeOption ?: $this->columnName can also be of type true; however, parameter $value of Illuminate\Support\Str::singular() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

136
        return ucfirst(Str::camel(Str::singular(/** @scrutinizer ignore-type */ $this->typeOption ?: $this->columnName )));
Loading history...
137
    }
138
    
139
    public function getParentModel()
140
    {
141
        return ucfirst(Str::camel(Str::singular($this->moduleName)));
142
    }
143
144
    public function getParentModule()
145
    {
146
        return $this->moduleName;
147
    }
148
149
    public function getParentModelLowercase()
150
    {
151
        return Str::singular($this->moduleName);
152
    }
153
154
    public function getStub($type)
155
    {
156
        return isset($this->stubs[$type]) ? $this->stubs[$type] : false;
157
    }
158
159
160
    public function getTextRows() {
161
        if (!$this->size)
162
            return 4;
163
        
164
        return floor($this->getsize() / 120);
0 ignored issues
show
Bug introduced by
The method getsize() does not exist on Prateekkarki\Laragen\Models\Types\LaragenType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

164
        return floor($this->/** @scrutinizer ignore-call */ getsize() / 120);
Loading history...
165
    }
166
    public function isUnique() {
167
        return $this->uniqueFlag;
168
    }
169
170
    public function isRequired() {
171
        return $this->requiredFlag;
172
    }
173
174
    public function optionArray() {
175
        return $this->optionArray;
176
    }
177
178
    public function getDisplay()
179
    {
180
        return Str::title(str_replace("_", " ", $this->columnName));
181
    }
182
183
    public function getColumn()
184
    {
185
        return $this->columnName;
186
    }
187
188
    public function getColumnKey()
189
    {
190
        return $this->columnName;
191
    }
192
    
193
    public function getDataType() {
194
        return $this->dataType;
195
    }
196
    
197
    public function getValidationRule() {
198
        return $this->validationRule;
199
    }
200
201
    protected function setUnique($set = true) {
202
        $this->uniqueFlag = ($set === true) ? true : false;
203
    }
204
205
    protected function setRequired($set = true) {
206
        $this->requiredFlag = ($set === true) ? true : false;
207
    }
208
209
    protected function setIsDisplay($set = true) {
210
        $this->isDisplay = ($set === true) ? true : false;
211
    }
212
213
    protected function setSize($size = null) {
214
        $this->size = $size;
215
    }
216
217
    protected function setOptions($optionType, $optionParam) {
218
        switch ($optionType) {
219
            case 'max':
220
                $this->setSize($optionParam);
221
                break;
222
            
223
            default:
224
                $this->$optionType = $optionParam;
225
                break;
226
        }
227
    }
228
229
    public function getTabs($number)
230
    {
231
        $schema = "";
232
        for ($i = 0; $i < $number; $i++) { 
233
            $schema .= "    ";
234
        }
235
        return $schema;
236
    }
237
}
238