Complex classes like MySQLiAdvancedOutput 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MySQLiAdvancedOutput, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | trait MySQLiAdvancedOutput |
||
37 | { |
||
38 | |||
39 | use MySQLiByDanielGPstructures; |
||
40 | |||
41 | protected $advCache = null; |
||
42 | |||
43 | /** |
||
44 | * Establish Database and Table intended to work with |
||
45 | * (in case the DB is ommited get the default one) |
||
46 | * |
||
47 | * @param string $tblSrc |
||
48 | */ |
||
49 | private function establishDatabaseAndTable($tblSrc) |
||
50 | { |
||
51 | if (strpos($tblSrc, '.') === false) { |
||
52 | if (!array_key_exists('workingDatabase', $this->advCache)) { |
||
53 | $this->advCache['workingDatabase'] = $this->getMySqlCurrentDatabase(); |
||
54 | } |
||
55 | return [$this->advCache['workingDatabase'], $tblSrc]; |
||
56 | } |
||
57 | return explode('.', str_replace('`', '', $tblSrc)); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Establishes the defaults for ENUM or SET field |
||
62 | * |
||
63 | * @param string $fldType |
||
64 | * @return array |
||
65 | */ |
||
66 | private function establishDefaultEnumSet($fldType) |
||
67 | { |
||
68 | $dfltArray = [ |
||
69 | 'enum' => ['additional' => ['size' => 1], 'suffix' => ''], |
||
70 | 'set' => ['additional' => ['size' => 5, 'multiselect'], 'suffix' => '[]'], |
||
71 | ]; |
||
72 | return $dfltArray[$fldType]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Creates a mask to differentiate between Mandatory and Optional fields |
||
77 | * |
||
78 | * @param array $details |
||
79 | * @return string |
||
80 | */ |
||
81 | private function getFieldCompletionType($details) |
||
82 | { |
||
83 | $inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']]; |
||
84 | if ($details['IS_NULLABLE'] == 'YES') { |
||
85 | $inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']]; |
||
86 | } |
||
87 | return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Returns the name of a field for displaying |
||
92 | * |
||
93 | * @param array $details |
||
94 | * @return string |
||
95 | */ |
||
96 | private function getFieldNameForDisplay($details) |
||
97 | { |
||
98 | $tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME']; |
||
99 | if ($details['COLUMN_COMMENT'] != '') { |
||
100 | return $details['COLUMN_COMMENT']; |
||
101 | } elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) { |
||
102 | return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']]; |
||
103 | } |
||
104 | return $details['COLUMN_NAME']; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns a Date field 2 use in a form |
||
109 | * |
||
110 | * @param array $value |
||
111 | * @return string |
||
112 | */ |
||
113 | private function getFieldOutputDate($value) |
||
114 | { |
||
115 | $defaultValue = $this->getFieldValue($value); |
||
116 | if (is_null($defaultValue)) { |
||
117 | $defaultValue = date('Y-m-d'); |
||
118 | } |
||
119 | $inA = [ |
||
120 | 'type' => 'text', |
||
121 | 'name' => $value['Field'], |
||
122 | 'id' => $value['Field'], |
||
123 | 'value' => $defaultValue, |
||
124 | 'size' => 10, |
||
125 | 'maxlength' => 10, |
||
126 | 'onfocus' => implode('', [ |
||
127 | 'javascript:NewCssCal(\'' . $value['Field'], |
||
128 | '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);', |
||
129 | ]), |
||
130 | ]; |
||
131 | return $this->setStringIntoShortTag('input', $inA) . $this->setCalendarControl($value['Field']); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns a Enum or Set field to use in form |
||
136 | * |
||
137 | * @param string $tblSrc |
||
138 | * @param string $fldType |
||
139 | * @param array $val |
||
140 | * @param array $iar |
||
141 | * @return string |
||
142 | */ |
||
143 | private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = []) |
||
144 | { |
||
145 | $adnlThings = $this->establishDefaultEnumSet($fldType); |
||
146 | if (array_key_exists('readonly', $val)) { |
||
147 | return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings); |
||
148 | } |
||
149 | $inAdtnl = $adnlThings['additional']; |
||
150 | if ($iar !== []) { |
||
151 | $inAdtnl = array_merge($inAdtnl, $iar); |
||
152 | } |
||
153 | $vlSlct = explode(',', $this->getFieldValue($val)); |
||
154 | $slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']); |
||
155 | return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Creats an input for ENUM or SET if marked Read-Only |
||
160 | * |
||
161 | * @param array $val |
||
162 | * @param array $adnlThings |
||
163 | * @return string |
||
164 | */ |
||
165 | private function getFieldOutputEnumSetReadOnly($val, $adnlThings) |
||
177 | |||
178 | /** |
||
179 | * Returns a Numeric field 2 use in a form |
||
180 | * |
||
181 | * @param string $tblSrc |
||
182 | * @param array $value |
||
183 | * @param array $iar |
||
184 | * @return string |
||
185 | */ |
||
186 | private function getFieldOutputNumeric($tblSrc, $value, $iar = []) |
||
198 | |||
199 | /** |
||
200 | * Handles creation of Auto Increment numeric field type output |
||
201 | * |
||
202 | * @param array $value |
||
203 | * @param array $iar |
||
204 | * @return string |
||
205 | */ |
||
206 | private function getFieldOutputNumericAI($value, $iar = []) |
||
223 | |||
224 | /** |
||
225 | * Builds field output type for numeric types if not FK |
||
226 | * |
||
227 | * @param array $fkArray |
||
228 | * @param array $value |
||
229 | * @param array $iar |
||
230 | * @return string |
||
231 | */ |
||
232 | private function getFieldOutputNumericNonFK($fkArray, $value, $iar = []) |
||
250 | |||
251 | /** |
||
252 | * Returns a Char field 2 use in a form |
||
253 | * |
||
254 | * @param string $tbl |
||
255 | * @param string $fieldType |
||
256 | * @param array $value |
||
257 | * @param array $iar |
||
258 | * @return string |
||
259 | */ |
||
260 | private function getFieldOutputText($tbl, $fieldType, $value, $iar = []) |
||
271 | |||
272 | /** |
||
273 | * Returns a Text field 2 use in a form |
||
274 | * |
||
275 | * @param string $fieldType |
||
276 | * @param array $value |
||
277 | * @param array $iar |
||
278 | * @return string |
||
279 | */ |
||
280 | private function getFieldOutputTextLarge($fieldType, $value, $iar = []) |
||
296 | |||
297 | /** |
||
298 | * Prepares the text output fields |
||
299 | * |
||
300 | * @param string $tbl |
||
301 | * @param array $value |
||
302 | * @return null|array |
||
303 | */ |
||
304 | private function getFieldOutputTextPrerequisites($tbl, $value) |
||
316 | |||
317 | /** |
||
318 | * Returns a Time field 2 use in a form |
||
319 | * |
||
320 | * @param array $value |
||
321 | * @param array $iar |
||
322 | * @return string |
||
323 | */ |
||
324 | private function getFieldOutputTime($value, $iar = []) |
||
328 | |||
329 | /** |
||
330 | * Returns a Timestamp field 2 use in a form |
||
331 | * |
||
332 | * @param array $dtl |
||
333 | * @param array $iar |
||
334 | * @return string |
||
335 | */ |
||
336 | private function getFieldOutputTimestamp($dtl, $iar = []) |
||
347 | |||
348 | /** |
||
349 | * Returns a Year field 2 use in a form |
||
350 | * |
||
351 | * @param array $details |
||
352 | * @param array $iar |
||
353 | * @return string |
||
354 | */ |
||
355 | private function getFieldOutputYear($tblName, $details, $iar) |
||
367 | |||
368 | /** |
||
369 | * Returns an array with fields referenced by a Foreign key |
||
370 | * |
||
371 | * @param string $database |
||
372 | * @param string $tblName |
||
373 | * @param string|array $onlyCol |
||
374 | * @return array |
||
375 | */ |
||
376 | private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
||
395 | |||
396 | /** |
||
397 | * Build label html tag |
||
398 | * |
||
399 | * @param array $details |
||
400 | * @return string |
||
401 | */ |
||
402 | private function getLabel($details) |
||
406 | |||
407 | /** |
||
408 | * Returns an array with possible values of a SET or ENUM column |
||
409 | * |
||
410 | * @param string $refTbl |
||
411 | * @param string $refCol |
||
412 | * @return array |
||
413 | */ |
||
414 | protected function getSetOrEnum2Array($refTbl, $refCol) |
||
429 | |||
430 | /** |
||
431 | * Returns a timestamp field value |
||
432 | * |
||
433 | * @param array $dtl |
||
434 | * @return array |
||
435 | */ |
||
436 | private function getTimestamping($dtl) |
||
452 | |||
453 | /** |
||
454 | * Builds field output w. special column name |
||
455 | * |
||
456 | * @param string $tableSource |
||
457 | * @param array $dtl |
||
458 | * @param array $features |
||
459 | * @param string $fieldLabel |
||
460 | * @return array |
||
461 | */ |
||
462 | private function setField($tableSource, $dtl, $features, $fieldLabel) |
||
474 | |||
475 | /** |
||
476 | * Builds field output w. another special column name |
||
477 | * |
||
478 | * @param string $tableSource |
||
479 | * @param array $dtl |
||
480 | * @param array $features |
||
481 | * @return string |
||
482 | */ |
||
483 | private function setFieldInput($tableSource, $dtl, $features) |
||
491 | |||
492 | /** |
||
493 | * Returns a generic form based on a given table |
||
494 | * |
||
495 | * @param string $tblSrc |
||
496 | * @param array $feat |
||
497 | * @param array $hdnInf |
||
498 | * |
||
499 | * @return string Form to add/modify detail for a single row within a table |
||
500 | */ |
||
501 | protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = []) |
||
518 | |||
519 | /** |
||
520 | * Analyse the field and returns the proper line 2 use in forms |
||
521 | * |
||
522 | * @param string $tableSource |
||
523 | * @param array $details |
||
524 | * @param array $features |
||
525 | * @return string|array |
||
526 | */ |
||
527 | private function setNeededField($tableSource, $details, $features) |
||
540 | |||
541 | /** |
||
542 | * Analyse the field type and returns the proper lines 2 use in forms |
||
543 | * |
||
544 | * @param string $tblName |
||
545 | * @param array $dtls |
||
546 | * @param array $features |
||
547 | * @return string|array |
||
548 | */ |
||
549 | private function setNeededFieldByType($tblName, $dtls, $features) |
||
557 | |||
558 | private function setNeededFieldKnown($tblName, $dtls, $features) |
||
572 | |||
573 | private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel) |
||
583 | |||
584 | private function setNeededFieldSingleType($tblName, $dtls, $iar) |
||
595 | |||
596 | private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
||
605 | |||
606 | /** |
||
607 | * create a Cache for given table to use it in many places |
||
608 | * |
||
609 | * @param string $tblSrc |
||
610 | */ |
||
611 | private function setTableCache($tblSrc) |
||
623 | |||
624 | private function setTableForeignKeyCache($dbName, $tblName) |
||
636 | } |
||
637 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.