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 | * Prepares the output of text fields defined w. FKs |
||
274 | * |
||
275 | * @param array $foreignKeysArray |
||
276 | * @param array $value |
||
277 | * @param array $iar |
||
278 | * @return string |
||
279 | */ |
||
280 | private function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
||
301 | |||
302 | /** |
||
303 | * Returns a Text field 2 use in a form |
||
304 | * |
||
305 | * @param string $fieldType |
||
306 | * @param array $value |
||
307 | * @param array $iar |
||
308 | * @return string |
||
309 | */ |
||
310 | private function getFieldOutputTextLarge($fieldType, $value, $iar = []) |
||
326 | |||
327 | /** |
||
328 | * Prepares the output of text fields w/o FKs |
||
329 | * |
||
330 | * @param array $value |
||
331 | * @param array $iar |
||
332 | * @return string |
||
333 | */ |
||
334 | private function getFieldOutputTextNonFK($value, $iar) |
||
350 | |||
351 | /** |
||
352 | * Prepares the text output fields |
||
353 | * |
||
354 | * @param string $tbl |
||
355 | * @param array $value |
||
356 | * @return null|array |
||
357 | */ |
||
358 | private function getFieldOutputTextPrerequisites($tbl, $value) |
||
370 | |||
371 | /** |
||
372 | * Builds output as text input type |
||
373 | * |
||
374 | * @param array $value |
||
375 | * @param integer $szN |
||
376 | * @param array $iar |
||
377 | * @return string |
||
378 | */ |
||
379 | private function getFieldOutputTT($value, $szN, $iar = []) |
||
394 | |||
395 | /** |
||
396 | * Returns a Time field 2 use in a form |
||
397 | * |
||
398 | * @param array $value |
||
399 | * @param array $iar |
||
400 | * @return string |
||
401 | */ |
||
402 | private function getFieldOutputTime($value, $iar = []) |
||
406 | |||
407 | /** |
||
408 | * Returns a Timestamp field 2 use in a form |
||
409 | * |
||
410 | * @param array $dtl |
||
411 | * @param array $iar |
||
412 | * @return string |
||
413 | */ |
||
414 | private function getFieldOutputTimestamp($dtl, $iar = []) |
||
425 | |||
426 | /** |
||
427 | * Returns a Year field 2 use in a form |
||
428 | * |
||
429 | * @param array $details |
||
430 | * @param array $iar |
||
431 | * @return string |
||
432 | */ |
||
433 | private function getFieldOutputYear($tblName, $details, $iar) |
||
445 | |||
446 | /** |
||
447 | * Returns an array with fields referenced by a Foreign key |
||
448 | * |
||
449 | * @param string $database |
||
450 | * @param string $tblName |
||
451 | * @param string|array $onlyCol |
||
452 | * @return array |
||
453 | */ |
||
454 | private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
||
473 | |||
474 | /** |
||
475 | * Build label html tag |
||
476 | * |
||
477 | * @param array $details |
||
478 | * @return string |
||
479 | */ |
||
480 | private function getLabel($details) |
||
484 | |||
485 | /** |
||
486 | * Returns an array with possible values of a SET or ENUM column |
||
487 | * |
||
488 | * @param string $refTbl |
||
489 | * @param string $refCol |
||
490 | * @return array |
||
491 | */ |
||
492 | protected function getSetOrEnum2Array($refTbl, $refCol) |
||
507 | |||
508 | /** |
||
509 | * Returns a timestamp field value |
||
510 | * |
||
511 | * @param array $dtl |
||
512 | * @return array |
||
513 | */ |
||
514 | private function getTimestamping($dtl) |
||
529 | |||
530 | /** |
||
531 | * Glues Database and Table into 1 single string |
||
532 | * |
||
533 | * @param string $dbName |
||
534 | * @param string $tbName |
||
535 | * @return string |
||
536 | */ |
||
537 | private function glueDbTb($dbName, $tbName) |
||
541 | |||
542 | /** |
||
543 | * Builds field output w. special column name |
||
544 | * |
||
545 | * @param string $tableSource |
||
546 | * @param array $dtl |
||
547 | * @param array $features |
||
548 | * @param string $fieldLabel |
||
549 | * @return array |
||
550 | */ |
||
551 | private function setField($tableSource, $dtl, $features, $fieldLabel) |
||
563 | |||
564 | /** |
||
565 | * Builds field output w. another special column name |
||
566 | * |
||
567 | * @param string $tableSource |
||
568 | * @param array $dtl |
||
569 | * @param array $features |
||
570 | * @return string |
||
571 | */ |
||
572 | private function setFieldInput($tableSource, $dtl, $features) |
||
580 | |||
581 | /** |
||
582 | * Returns a generic form based on a given table |
||
583 | * |
||
584 | * @param string $tblSrc |
||
585 | * @param array $feat |
||
586 | * @param array $hdnInf |
||
587 | * |
||
588 | * @return string Form to add/modify detail for a single row within a table |
||
589 | */ |
||
590 | protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = []) |
||
607 | |||
608 | /** |
||
609 | * Analyse the field and returns the proper line 2 use in forms |
||
610 | * |
||
611 | * @param string $tableSource |
||
612 | * @param array $details |
||
613 | * @param array $features |
||
614 | * @return string|array |
||
615 | */ |
||
616 | private function setNeededField($tableSource, $details, $features) |
||
629 | |||
630 | /** |
||
631 | * Analyse the field type and returns the proper lines 2 use in forms |
||
632 | * |
||
633 | * @param string $tblName |
||
634 | * @param array $dtls |
||
635 | * @param array $features |
||
636 | * @return string|array |
||
637 | */ |
||
638 | private function setNeededFieldByType($tblName, $dtls, $features) |
||
646 | |||
647 | private function setNeededFieldKnown($tblName, $dtls, $features) |
||
661 | |||
662 | private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel) |
||
672 | |||
673 | private function setNeededFieldSingleType($tblName, $dtls, $iar) |
||
684 | |||
685 | private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
||
694 | |||
695 | /** |
||
696 | * create a Cache for given table to use it in many places |
||
697 | * |
||
698 | * @param string $tblSrc |
||
699 | */ |
||
700 | private function setTableCache($tblSrc) |
||
712 | |||
713 | private function setTableForeginKeyCache($dbName, $tblName) |
||
725 | } |
||
726 |
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.