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) |
||
74 | |||
75 | /** |
||
76 | * Adjust table name with proper sufix and prefix |
||
77 | * |
||
78 | * @param string $refTbl |
||
79 | * @return string |
||
80 | */ |
||
81 | private function fixTableSource($refTbl) |
||
97 | |||
98 | /** |
||
99 | * Creates a mask to differentiate between Mandatory and Optional fields |
||
100 | * |
||
101 | * @param array $details |
||
102 | * @return string |
||
103 | */ |
||
104 | private function getFieldCompletionType($details) |
||
112 | |||
113 | /** |
||
114 | * Returns the name of a field for displaying |
||
115 | * |
||
116 | * @param array $details |
||
117 | * @return string |
||
118 | */ |
||
119 | private function getFieldNameForDisplay($details) |
||
120 | { |
||
121 | $tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME']; |
||
122 | if ($details['COLUMN_COMMENT'] != '') { |
||
123 | return $details['COLUMN_COMMENT']; |
||
124 | } elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) { |
||
125 | return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']]; |
||
126 | } |
||
127 | return $details['COLUMN_NAME']; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns a Date field 2 use in a form |
||
132 | * |
||
133 | * @param array $value |
||
134 | * @return string |
||
135 | */ |
||
136 | private function getFieldOutputDate($value) |
||
156 | |||
157 | /** |
||
158 | * Returns a Enum or Set field to use in form |
||
159 | * |
||
160 | * @param string $tblSrc |
||
161 | * @param string $fldType |
||
162 | * @param array $val |
||
163 | * @param array $iar |
||
164 | * @return string |
||
165 | */ |
||
166 | private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = []) |
||
180 | |||
181 | /** |
||
182 | * Creats an input for ENUM or SET if marked Read-Only |
||
183 | * |
||
184 | * @param array $val |
||
185 | * @param array $adnlThings |
||
186 | * @return string |
||
187 | */ |
||
188 | private function getFieldOutputEnumSetReadOnly($val, $adnlThings) |
||
200 | |||
201 | /** |
||
202 | * Returns a Numeric field 2 use in a form |
||
203 | * |
||
204 | * @param string $tblSrc |
||
205 | * @param array $value |
||
206 | * @param array $iar |
||
207 | * @return string |
||
208 | */ |
||
209 | private function getFieldOutputNumeric($tblSrc, $value, $iar = []) |
||
210 | { |
||
211 | if ($value['EXTRA'] == 'auto_increment') { |
||
212 | return $this->getFieldOutputNumericAI($value, $iar); |
||
213 | } |
||
214 | $fkArray = $this->getForeignKeysToArray($this->advCache['workingDatabase'], $tblSrc, $value['COLUMN_NAME']); |
||
215 | if (is_null($fkArray)) { |
||
216 | $fldNos = $this->setFieldNumbers($value); |
||
217 | return $this->getFieldOutputTT($value, min(50, $fldNos['l']), $iar); |
||
218 | } |
||
219 | return $this->getFieldOutputNumericNonFK($fkArray, $value, $iar); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Handles creation of Auto Increment numeric field type output |
||
224 | * |
||
225 | * @param array $value |
||
226 | * @param array $iar |
||
227 | * @return string |
||
228 | */ |
||
229 | private function getFieldOutputNumericAI($value, $iar = []) |
||
246 | |||
247 | /** |
||
248 | * Builds field output type for numeric types if not FK |
||
249 | * |
||
250 | * @param array $fkArray |
||
251 | * @param array $value |
||
252 | * @param array $iar |
||
253 | * @return string |
||
254 | */ |
||
255 | private function getFieldOutputNumericNonFK($fkArray, $value, $iar = []) |
||
273 | |||
274 | /** |
||
275 | * Returns a Char field 2 use in a form |
||
276 | * |
||
277 | * @param string $tbl |
||
278 | * @param string $fieldType |
||
279 | * @param array $value |
||
280 | * @param array $iar |
||
281 | * @return string |
||
282 | */ |
||
283 | private function getFieldOutputText($tbl, $fieldType, $value, $iar = []) |
||
284 | { |
||
285 | if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) { |
||
286 | return ''; |
||
287 | } |
||
288 | $foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value); |
||
289 | if (!is_null($foreignKeysArray)) { |
||
290 | return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar); |
||
291 | } |
||
292 | return $this->getFieldOutputTextNonFK($value, $iar); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Prepares the output of text fields defined w. FKs |
||
297 | * |
||
298 | * @param array $foreignKeysArray |
||
299 | * @param array $value |
||
300 | * @param array $iar |
||
301 | * @return string |
||
302 | */ |
||
303 | private function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
||
324 | |||
325 | /** |
||
326 | * Returns a Text field 2 use in a form |
||
327 | * |
||
328 | * @param string $fieldType |
||
329 | * @param array $value |
||
330 | * @param array $iar |
||
331 | * @return string |
||
332 | */ |
||
333 | private function getFieldOutputTextLarge($fieldType, $value, $iar = []) |
||
334 | { |
||
335 | if (!in_array($fieldType, ['blob', 'text'])) { |
||
336 | return ''; |
||
337 | } |
||
338 | $inAdtnl = [ |
||
339 | 'name' => $value['COLUMN_NAME'], |
||
340 | 'id' => $value['COLUMN_NAME'], |
||
341 | 'rows' => 4, |
||
342 | 'cols' => 55, |
||
343 | ]; |
||
344 | if ($iar !== []) { |
||
345 | $inAdtnl = array_merge($inAdtnl, $iar); |
||
346 | } |
||
347 | return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Prepares the output of text fields w/o FKs |
||
352 | * |
||
353 | * @param array $value |
||
354 | * @param array $iar |
||
355 | * @return string |
||
356 | */ |
||
357 | private function getFieldOutputTextNonFK($value, $iar) |
||
373 | |||
374 | /** |
||
375 | * Prepares the text output fields |
||
376 | * |
||
377 | * @param string $tbl |
||
378 | * @param array $value |
||
379 | * @return null|array |
||
380 | */ |
||
381 | private function getFieldOutputTextPrerequisites($tbl, $value) |
||
393 | |||
394 | /** |
||
395 | * Builds output as text input type |
||
396 | * |
||
397 | * @param array $value |
||
398 | * @param integer $szN |
||
399 | * @param array $iar |
||
400 | * @return string |
||
401 | */ |
||
402 | private function getFieldOutputTT($value, $szN, $iar = []) |
||
417 | |||
418 | /** |
||
419 | * Returns a Time field 2 use in a form |
||
420 | * |
||
421 | * @param array $value |
||
422 | * @param array $iar |
||
423 | * @return string |
||
424 | */ |
||
425 | private function getFieldOutputTime($value, $iar = []) |
||
429 | |||
430 | /** |
||
431 | * Returns a Timestamp field 2 use in a form |
||
432 | * |
||
433 | * @param array $dtl |
||
434 | * @param array $iar |
||
435 | * @return string |
||
436 | */ |
||
437 | private function getFieldOutputTimestamp($dtl, $iar = []) |
||
448 | |||
449 | /** |
||
450 | * Returns a Year field 2 use in a form |
||
451 | * |
||
452 | * @param array $details |
||
453 | * @param array $iar |
||
454 | * @return string |
||
455 | */ |
||
456 | private function getFieldOutputYear($tblName, $details, $iar) |
||
468 | |||
469 | /** |
||
470 | * prepares the query to detect FKs |
||
471 | * |
||
472 | * @param array $value |
||
473 | * @return string |
||
474 | */ |
||
475 | private function getForeignKeysQuery($value) |
||
484 | |||
485 | /** |
||
486 | * Returns an array with fields referenced by a Foreign key |
||
487 | * |
||
488 | * @param string $database |
||
489 | * @param string $tblName |
||
490 | * @param string|array $onlyCol |
||
491 | * @return array |
||
492 | */ |
||
493 | private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
||
512 | |||
513 | /** |
||
514 | * Build label html tag |
||
515 | * |
||
516 | * @param array $details |
||
517 | * @return string |
||
518 | */ |
||
519 | private function getLabel($details) |
||
523 | |||
524 | /** |
||
525 | * Returns an array with possible values of a SET or ENUM column |
||
526 | * |
||
527 | * @param string $refTbl |
||
528 | * @param string $refCol |
||
529 | * @return array |
||
530 | */ |
||
531 | protected function getSetOrEnum2Array($refTbl, $refCol) |
||
546 | |||
547 | /** |
||
548 | * Returns a timestamp field value |
||
549 | * |
||
550 | * @param array $dtl |
||
551 | * @return array |
||
552 | */ |
||
553 | private function getTimestamping($dtl) |
||
568 | |||
569 | /** |
||
570 | * Glues Database and Table into 1 single string |
||
571 | * |
||
572 | * @param string $dbName |
||
573 | * @param string $tbName |
||
574 | * @return string |
||
575 | */ |
||
576 | private function glueDbTb($dbName, $tbName) |
||
580 | |||
581 | /** |
||
582 | * Manages features flag |
||
583 | * |
||
584 | * @param string $fieldName |
||
585 | * @param array $features |
||
586 | * @return string |
||
587 | */ |
||
588 | private function handleFeatures($fieldName, $features) |
||
598 | |||
599 | /** |
||
600 | * Handles the features |
||
601 | * |
||
602 | * @param string $fieldName |
||
603 | * @param array $features |
||
604 | * @param string $featureKey |
||
605 | * @return array |
||
606 | */ |
||
607 | private function handleFeaturesSingle($fieldName, $features, $featureKey) |
||
624 | |||
625 | /** |
||
626 | * Builds field output w. special column name |
||
627 | * |
||
628 | * @param string $tableSource |
||
629 | * @param array $dtl |
||
630 | * @param array $features |
||
631 | * @param string $fieldLabel |
||
632 | * @return array |
||
633 | */ |
||
634 | private function setField($tableSource, $dtl, $features, $fieldLabel) |
||
646 | |||
647 | /** |
||
648 | * Builds field output w. another special column name |
||
649 | * |
||
650 | * @param string $tableSource |
||
651 | * @param array $dtl |
||
652 | * @param array $features |
||
653 | * @return string |
||
654 | */ |
||
655 | private function setFieldInput($tableSource, $dtl, $features) |
||
663 | |||
664 | /** |
||
665 | * Prepares the label for inputs |
||
666 | * |
||
667 | * @param array $details |
||
668 | * @param array $features |
||
669 | * @param string $fieldLabel |
||
670 | * @return string |
||
671 | */ |
||
672 | private function setFieldLabel($details, $features, $fieldLabel) |
||
682 | |||
683 | /** |
||
684 | * Form default buttons |
||
685 | * |
||
686 | * @param array $feat |
||
687 | * @param array $hiddenInfo |
||
688 | * @return string |
||
689 | */ |
||
690 | private function setFormButtons($feat, $hiddenInfo = []) |
||
705 | |||
706 | /** |
||
707 | * Builds javascript to avoid multiple form submission |
||
708 | * |
||
709 | * @param string $frmId |
||
710 | * @return string |
||
711 | */ |
||
712 | private function setFormJavascriptFinal($frmId) |
||
730 | |||
731 | /** |
||
732 | * Returns a generic form based on a given table |
||
733 | * |
||
734 | * @param string $tblSrc |
||
735 | * @param array $feat |
||
736 | * @param array $hdnInf |
||
737 | * |
||
738 | * @return string Form to add/modify detail for a single row within a table |
||
739 | */ |
||
740 | protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = []) |
||
757 | |||
758 | /** |
||
759 | * Analyse the field and returns the proper line 2 use in forms |
||
760 | * |
||
761 | * @param string $tableSource |
||
762 | * @param array $details |
||
763 | * @param array $features |
||
764 | * @return string|array |
||
765 | */ |
||
766 | private function setNeededField($tableSource, $details, $features) |
||
779 | |||
780 | /** |
||
781 | * Analyse the field type and returns the proper lines 2 use in forms |
||
782 | * |
||
783 | * @param string $tblName |
||
784 | * @param array $dtls |
||
785 | * @param array $features |
||
786 | * @return string|array |
||
787 | */ |
||
788 | private function setNeededFieldByType($tblName, $dtls, $features) |
||
796 | |||
797 | private function setNeededFieldKnown($tblName, $dtls, $features) |
||
811 | |||
812 | private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel) |
||
822 | |||
823 | private function setNeededFieldSingleType($tblName, $dtls, $iar) |
||
834 | |||
835 | private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
||
844 | |||
845 | /** |
||
846 | * create a Cache for given table to use it in many places |
||
847 | * |
||
848 | * @param string $tblSrc |
||
849 | */ |
||
850 | private function setTableCache($tblSrc) |
||
862 | |||
863 | private function setTableForeginKeyCache($dbName, $tblName) |
||
875 | } |
||
876 |
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.