Total Complexity | 46 |
Total Lines | 230 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DataOption 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 DataOption, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class DataOption |
||
6 | { |
||
7 | const TYPE_PARENT = 'parent'; |
||
8 | |||
9 | const TYPE_RELATED = 'related'; |
||
10 | |||
11 | const COLUMN_UNIQUE = 'unique'; |
||
12 | |||
13 | const COLUMN_REQUIRED = 'required'; |
||
14 | |||
15 | protected $specialType; |
||
16 | |||
17 | protected $uniqueFlag; |
||
18 | |||
19 | protected $requiredFlag; |
||
20 | |||
21 | protected $size; |
||
22 | |||
23 | protected $column; |
||
24 | |||
25 | /** |
||
26 | * List of all types of data. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | public static $types = [ |
||
31 | 'integer', |
||
32 | 'string', |
||
33 | 'boolean', |
||
34 | 'text', |
||
35 | 'date', |
||
36 | 'datetime' |
||
37 | ]; |
||
38 | |||
39 | public static $fileTypes = [ |
||
40 | 'image', |
||
41 | 'file' |
||
42 | ]; |
||
43 | |||
44 | public static $specialTypes = [ |
||
45 | 'parent', |
||
46 | 'related' |
||
47 | ]; |
||
48 | |||
49 | public static $relatedMultiple = [ |
||
50 | |||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Array of data type options |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $optionArray; |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Key to type conversion array. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $keyToType = [ |
||
67 | 'integer' =>'integer', |
||
68 | 'string' =>'string', |
||
69 | 'image' =>'string', |
||
70 | 'file' =>'string', |
||
71 | 'boolean' =>'boolean', |
||
72 | 'text' =>'text', |
||
73 | 'datetime' =>'datetime', |
||
74 | 'date' =>'date' |
||
75 | ]; |
||
76 | |||
77 | public function __construct($columnName, $optionString) |
||
78 | { |
||
79 | $this->column = $columnName; |
||
80 | $this->size = false; |
||
81 | // dump($optionString); |
||
82 | if(is_array($optionString) ){ |
||
83 | $this->dataType = 'multiple'; |
||
|
|||
84 | $this->multipleOptions = []; |
||
85 | foreach($optionString as $col => $multString){ |
||
86 | $this->multipleOptions[] = new Self($col, $multString); |
||
87 | } |
||
88 | // dd($this->multipleOptions[0]); |
||
89 | $column = $this->multipleOptions[0]->column; |
||
90 | // dd($column); |
||
91 | // $option = ; |
||
92 | |||
93 | }else{ |
||
94 | $this->optionArray = explode('|', $optionString); |
||
95 | $typePieces = array_shift($this->optionArray); |
||
96 | $type = explode(':', $typePieces); |
||
97 | $this->dataType = is_array($type) ? $type[0] : $type; |
||
98 | $this->typeOption = is_array($type)&&count($type)>=2 ? $type[1] : false; |
||
99 | |||
100 | foreach ($this->optionArray as $option) { |
||
101 | if ($option == self::COLUMN_UNIQUE) { |
||
102 | $this->setUnique(); |
||
103 | continue; |
||
104 | } |
||
105 | if ($option == self::COLUMN_REQUIRED) { |
||
106 | $this->setRequired(); |
||
107 | continue; |
||
108 | } |
||
109 | if(Str::contains($option, ':')){ |
||
110 | $optionPieces = explode(':', $option); |
||
111 | $this->setOptions($optionPieces[0], $optionPieces[1]); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | |||
117 | } |
||
118 | |||
119 | public function getSchema() |
||
120 | { |
||
121 | if ($this->dataType=='parent') { |
||
122 | $schema = $this->processParent(); |
||
123 | } else if( in_array($this->dataType, ['gallery', 'related', 'multiple']) ){ |
||
124 | $schema = ''; |
||
125 | } else { |
||
126 | $schema = '$table->'.$this->getColumnType()."('{$this->column}'"; |
||
127 | $schema .= $this->getSize() ? ", {$this->getSize()})" : ")"; |
||
128 | $schema .= $this->isUnique() ? "->unique()" : ""; |
||
129 | $schema .= $this->isRequired() ? "" : "->nullable()"; |
||
130 | $schema .= ";"; |
||
131 | } |
||
132 | return $schema; |
||
133 | } |
||
134 | |||
135 | public function getKey() { |
||
136 | return $this->column; |
||
137 | } |
||
138 | |||
139 | public function getDisplay() { |
||
140 | return ucfirst(str_replace('_', ' ', $this->column)); |
||
141 | } |
||
142 | |||
143 | public function getType() { |
||
150 | } |
||
151 | |||
152 | public function getSize() { |
||
153 | return $this->size; |
||
154 | } |
||
155 | |||
156 | public function getParentModule() { |
||
157 | return (in_array($this->dataType, self::$specialTypes)) ? $this->typeOption : ''; |
||
158 | } |
||
159 | |||
160 | public function getParentModel() { |
||
161 | return (in_array($this->dataType, self::$specialTypes)) ? ucfirst(camel_case(str_singular($this->typeOption))) : ''; |
||
162 | } |
||
163 | |||
164 | public function isRequired() { |
||
165 | return $this->requiredFlag; |
||
166 | } |
||
167 | |||
168 | public function getFormOptions() { |
||
169 | $options = ""; |
||
170 | $options .= $this->isRequired() ? 'required="required" ' :''; |
||
171 | $options .= $this->getType()=='text' ? 'rows="'.$this->getTextRows().'" ' :''; |
||
172 | return $options; |
||
173 | } |
||
174 | |||
175 | |||
176 | public function getTextRows() { |
||
177 | if(!$this->size) |
||
178 | return 4; |
||
179 | |||
180 | return floor($this->getsize()/120); |
||
181 | } |
||
182 | |||
183 | public function getTabs($number) |
||
184 | { |
||
185 | $schema = ""; |
||
186 | for ($i = 0; $i < $number; $i++) { |
||
187 | $schema .= " "; |
||
188 | } |
||
189 | return $schema; |
||
190 | } |
||
191 | |||
192 | public function isUnique() { |
||
193 | return $this->uniqueFlag; |
||
194 | } |
||
195 | |||
196 | protected function getColumnType() { |
||
197 | return $this->keyToType[$this->dataType]; |
||
198 | } |
||
199 | |||
200 | protected function setOptions($optionType, $optionParam) { |
||
201 | switch ($optionType) { |
||
202 | case 'max': |
||
203 | $this->setSize($optionParam); |
||
204 | break; |
||
205 | |||
206 | default: |
||
207 | $this->$optionType = $optionParam; |
||
208 | break; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | protected function setUnique($set = true) { |
||
213 | $this->uniqueFlag = ($set === true) ? true : false; |
||
214 | } |
||
215 | |||
216 | protected function setRequired($set = true) { |
||
217 | $this->requiredFlag = ($set === true) ? true : false; |
||
218 | } |
||
219 | |||
220 | protected function setSize($size = null) { |
||
221 | $this->size = $size; |
||
222 | } |
||
223 | |||
224 | public function optionArray(){ |
||
225 | return $this->optionArray; |
||
226 | } |
||
227 | |||
228 | protected function processParent() { |
||
235 | } |
||
236 | |||
237 | } |
||
238 |