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 MySQLiByDanielGP; |
||
| 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) |
||
| 59 | |||
| 60 | private function establishDefaultEnumSet($fldType) |
||
| 68 | |||
| 69 | private function fixTableSource($refTbl) |
||
| 85 | |||
| 86 | private function getFieldCompletionType($details) |
||
| 87 | { |
||
| 88 | $inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']]; |
||
| 89 | if ($details['IS_NULLABLE'] == 'YES') { |
||
| 90 | $inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']]; |
||
| 91 | } |
||
| 92 | return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the name of a field for displaying |
||
| 97 | * |
||
| 98 | * @param array $details |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | private function getFieldNameForDisplay($details) |
||
| 102 | { |
||
| 103 | $tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME']; |
||
| 104 | if ($details['COLUMN_COMMENT'] != '') { |
||
| 105 | return $details['COLUMN_COMMENT']; |
||
| 106 | } elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) { |
||
| 107 | return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']]; |
||
| 108 | } |
||
| 109 | return $details['COLUMN_NAME']; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns a Date field 2 use in a form |
||
| 114 | * |
||
| 115 | * @param array $value |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | private function getFieldOutputDate($value) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns a Enum or Set field to use in form |
||
| 141 | * |
||
| 142 | * @param string $tblSrc |
||
| 143 | * @param string $fldType |
||
| 144 | * @param array $val |
||
| 145 | * @param array $iar |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = []) |
||
| 149 | { |
||
| 150 | $adnlThings = $this->establishDefaultEnumSet($fldType); |
||
| 151 | if (array_key_exists('readonly', $val)) { |
||
| 152 | return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings); |
||
| 153 | } |
||
| 154 | $inAdtnl = $adnlThings['additional']; |
||
| 155 | if ($iar !== []) { |
||
| 156 | $inAdtnl = array_merge($inAdtnl, $iar); |
||
| 157 | } |
||
| 158 | $vlSlct = explode(',', $this->getFieldValue($val)); |
||
| 159 | $slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']); |
||
| 160 | return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl); |
||
| 161 | } |
||
| 162 | |||
| 163 | private function getFieldOutputEnumSetReadOnly($val, $adnlThings) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns a Numeric field 2 use in a form |
||
| 178 | * |
||
| 179 | * @param string $tblSrc |
||
| 180 | * @param array $value |
||
| 181 | * @param array $iar |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | private function getFieldOutputNumeric($tblSrc, $value, $iar = []) |
||
| 185 | { |
||
| 186 | if ($value['EXTRA'] == 'auto_increment') { |
||
| 187 | return $this->getFieldOutputNumericAI($value, $iar); |
||
| 188 | } |
||
| 189 | $fkArray = $this->getForeignKeysToArray($this->advCache['workingDatabase'], $tblSrc, $value['COLUMN_NAME']); |
||
| 190 | if (is_null($fkArray)) { |
||
| 191 | $fldNos = $this->setFieldNumbers($value); |
||
| 192 | return $this->getFieldOutputTT($value, min(50, $fldNos['l']), $iar); |
||
| 193 | } |
||
| 194 | return $this->getFieldOutputNumericNonFK($fkArray, $value, $iar); |
||
| 195 | } |
||
| 196 | |||
| 197 | private function getFieldOutputNumericAI($value, $iar = []) |
||
| 214 | |||
| 215 | private function getFieldOutputNumericNonFK($fkArray, $value, $iar = []) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns a Char field 2 use in a form |
||
| 236 | * |
||
| 237 | * @param string $tbl |
||
| 238 | * @param string $fieldType |
||
| 239 | * @param array $value |
||
| 240 | * @param array $iar |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | private function getFieldOutputText($tbl, $fieldType, $value, $iar = []) |
||
| 244 | { |
||
| 245 | if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) { |
||
| 246 | return ''; |
||
| 247 | } |
||
| 248 | $foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value); |
||
| 249 | if (is_null($foreignKeysArray)) { |
||
| 250 | return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar); |
||
| 251 | } |
||
| 252 | return $this->getFieldOutputTextNonFK($value, $iar); |
||
| 253 | } |
||
| 254 | |||
| 255 | private function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns a Text field 2 use in a form |
||
| 278 | * |
||
| 279 | * @param string $fieldType |
||
| 280 | * @param array $value |
||
| 281 | * @param array $iar |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | private function getFieldOutputTextLarge($fieldType, $value, $iar = []) |
||
| 285 | { |
||
| 286 | if (!in_array($fieldType, ['blob', 'text'])) { |
||
| 287 | return ''; |
||
| 288 | } |
||
| 289 | $inAdtnl = [ |
||
| 290 | 'name' => $value['COLUMN_NAME'], |
||
| 291 | 'id' => $value['COLUMN_NAME'], |
||
| 292 | 'rows' => 4, |
||
| 293 | 'cols' => 55, |
||
| 294 | ]; |
||
| 295 | if ($iar !== []) { |
||
| 296 | $inAdtnl = array_merge($inAdtnl, $iar); |
||
| 297 | } |
||
| 298 | return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl); |
||
| 299 | } |
||
| 300 | |||
| 301 | private function getFieldOutputTextNonFK($value, $iar) |
||
| 317 | |||
| 318 | private function getFieldOutputTextPrerequisites($tbl, $value) |
||
| 330 | |||
| 331 | private function getFieldOutputTT($value, $szN, $iar = []) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns a Time field 2 use in a form |
||
| 349 | * |
||
| 350 | * @param array $value |
||
| 351 | * @param array $iar |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | private function getFieldOutputTime($value, $iar = []) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns a Timestamp field 2 use in a form |
||
| 361 | * |
||
| 362 | * @param array $value |
||
| 363 | * @param array $iar |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | private function getFieldOutputTimestamp($value, $iar = []) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns a Year field 2 use in a form |
||
| 380 | * |
||
| 381 | * @param array $details |
||
| 382 | * @param array $iar |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | private function getFieldOutputYear($tblName, $details, $iar) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns given value for a field from REQUEST global variable |
||
| 400 | * |
||
| 401 | * @param array $details |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | private function getFieldValue($details) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Handles field value ignoring any input from the user |
||
| 419 | * |
||
| 420 | * @param array $details |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | private function getFieldValueWithoutUserInput($details) |
||
| 433 | |||
| 434 | private function getForeignKeysQuery($value) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Returns an array with fields referenced by a Foreign key |
||
| 446 | * |
||
| 447 | * @param string $database |
||
| 448 | * @param string $tblName |
||
| 449 | * @param string|array $onlyCol |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
||
| 471 | |||
| 472 | private function getLabel($details) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns an array with possible values of a SET or ENUM column |
||
| 479 | * |
||
| 480 | * @param string $refTbl |
||
| 481 | * @param string $refCol |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | protected function getSetOrEnum2Array($refTbl, $refCol) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Returns a timestamp field value |
||
| 502 | * |
||
| 503 | * @param array $dtl |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | private function getTimestamping($dtl) |
||
| 521 | |||
| 522 | private function glueDbTb($dbName, $tbName) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Manages features flag |
||
| 529 | * |
||
| 530 | * @param string $fieldName |
||
| 531 | * @param array $features |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | private function handleFeatures($fieldName, $features) |
||
| 544 | |||
| 545 | private function handleFeaturesSingle($fieldName, $features, $featureKey) |
||
| 562 | |||
| 563 | private function setField($tableSource, $dtl, $features, $fieldLabel) |
||
| 575 | |||
| 576 | private function setFieldInput($tableSource, $dtl, $features) |
||
| 584 | |||
| 585 | private function setFieldLabel($details, $features, $fieldLabel) |
||
| 595 | |||
| 596 | private function setFormButtons($feat, $hiddenInfo) |
||
| 611 | |||
| 612 | private function setFormJavascriptFinal($frmId) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns a generic form based on a given table |
||
| 628 | * |
||
| 629 | * @param string $tblSrc Table Source |
||
| 630 | * @param array $feat |
||
| 631 | * @param string|array $hdnInf List of hidden fields |
||
| 632 | * |
||
| 633 | * @return string Form to add/modify detail for a single row within a table |
||
| 634 | */ |
||
| 635 | protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = '') |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Analyse the field and returns the proper line 2 use in forms |
||
| 655 | * |
||
| 656 | * @param string $tableSource |
||
| 657 | * @param array $details |
||
| 658 | * @param array $features |
||
| 659 | * @return string|array |
||
| 660 | */ |
||
| 661 | private function setNeededField($tableSource, $details, $features) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Analyse the field type and returns the proper lines 2 use in forms |
||
| 677 | * |
||
| 678 | * @param string $tblName |
||
| 679 | * @param array $dtls |
||
| 680 | * @param array $features |
||
| 681 | * @return string|array |
||
| 682 | */ |
||
| 683 | private function setNeededFieldByType($tblName, $dtls, $features) |
||
| 691 | |||
| 692 | private function setNeededFieldKnown($tblName, $dtls, $features) |
||
| 706 | |||
| 707 | private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel) |
||
| 719 | |||
| 720 | private function setNeededFieldSingleType($tblName, $dtls, $iar) |
||
| 731 | |||
| 732 | private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * create a Cache for given table to use it in many places |
||
| 744 | * |
||
| 745 | * @param type $tblSrc |
||
| 746 | */ |
||
| 747 | private function setTableCache($tblSrc) |
||
| 762 | |||
| 763 | private function setTableForeginKeyCache($dbName, $tblName) |
||
| 775 | } |
||
| 776 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.