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 | * Returns an array with fields referenced by a Foreign key |
||
| 471 | * |
||
| 472 | * @param string $database |
||
| 473 | * @param string $tblName |
||
| 474 | * @param string|array $onlyCol |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Build label html tag |
||
| 499 | * |
||
| 500 | * @param array $details |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | private function getLabel($details) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Returns an array with possible values of a SET or ENUM column |
||
| 510 | * |
||
| 511 | * @param string $refTbl |
||
| 512 | * @param string $refCol |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | protected function getSetOrEnum2Array($refTbl, $refCol) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Returns a timestamp field value |
||
| 533 | * |
||
| 534 | * @param array $dtl |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | private function getTimestamping($dtl) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Glues Database and Table into 1 single string |
||
| 555 | * |
||
| 556 | * @param string $dbName |
||
| 557 | * @param string $tbName |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | private function glueDbTb($dbName, $tbName) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Manages features flag |
||
| 567 | * |
||
| 568 | * @param string $fieldName |
||
| 569 | * @param array $features |
||
| 570 | * @return string |
||
| 571 | */ |
||
| 572 | private function handleFeatures($fieldName, $features) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Handles the features |
||
| 585 | * |
||
| 586 | * @param string $fieldName |
||
| 587 | * @param array $features |
||
| 588 | * @param string $featureKey |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | private function handleFeaturesSingle($fieldName, $features, $featureKey) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Builds field output w. special column name |
||
| 611 | * |
||
| 612 | * @param string $tableSource |
||
| 613 | * @param array $dtl |
||
| 614 | * @param array $features |
||
| 615 | * @param string $fieldLabel |
||
| 616 | * @return array |
||
| 617 | */ |
||
| 618 | private function setField($tableSource, $dtl, $features, $fieldLabel) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Builds field output w. another special column name |
||
| 633 | * |
||
| 634 | * @param string $tableSource |
||
| 635 | * @param array $dtl |
||
| 636 | * @param array $features |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | private function setFieldInput($tableSource, $dtl, $features) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Returns a generic form based on a given table |
||
| 650 | * |
||
| 651 | * @param string $tblSrc |
||
| 652 | * @param array $feat |
||
| 653 | * @param array $hdnInf |
||
| 654 | * |
||
| 655 | * @return string Form to add/modify detail for a single row within a table |
||
| 656 | */ |
||
| 657 | protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = []) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Analyse the field and returns the proper line 2 use in forms |
||
| 677 | * |
||
| 678 | * @param string $tableSource |
||
| 679 | * @param array $details |
||
| 680 | * @param array $features |
||
| 681 | * @return string|array |
||
| 682 | */ |
||
| 683 | private function setNeededField($tableSource, $details, $features) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Analyse the field type and returns the proper lines 2 use in forms |
||
| 699 | * |
||
| 700 | * @param string $tblName |
||
| 701 | * @param array $dtls |
||
| 702 | * @param array $features |
||
| 703 | * @return string|array |
||
| 704 | */ |
||
| 705 | private function setNeededFieldByType($tblName, $dtls, $features) |
||
| 713 | |||
| 714 | private function setNeededFieldKnown($tblName, $dtls, $features) |
||
| 728 | |||
| 729 | private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel) |
||
| 739 | |||
| 740 | private function setNeededFieldSingleType($tblName, $dtls, $iar) |
||
| 751 | |||
| 752 | private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
||
| 761 | |||
| 762 | /** |
||
| 763 | * create a Cache for given table to use it in many places |
||
| 764 | * |
||
| 765 | * @param string $tblSrc |
||
| 766 | */ |
||
| 767 | private function setTableCache($tblSrc) |
||
| 779 | |||
| 780 | private function setTableForeginKeyCache($dbName, $tblName) |
||
| 792 | } |
||
| 793 |
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.