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 = []) |
||
355 | { |
||
356 | return $this->getFieldOutputTT($value, 8, $iar); |
||
357 | } |
||
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 = []) |
||
367 | { |
||
368 | $input = $this->getFieldOutputTT($value, 19, $iar); |
||
369 | if (!array_key_exists('readonly', $iar)) { |
||
370 | $input .= $this->setCalendarControlWithTime($value['COLUMN_NAME']); |
||
371 | } |
||
372 | return $input; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Returns a Year field 2 use in a form |
||
377 | * |
||
378 | * @param array $details |
||
379 | * @param array $iar |
||
380 | * @return string |
||
381 | */ |
||
382 | private function getFieldOutputYear($tblName, $details, $iar) |
||
394 | |||
395 | /** |
||
396 | * Returns given value for a field from REQUEST global variable |
||
397 | * |
||
398 | * @param array $details |
||
399 | * @return string |
||
400 | */ |
||
401 | private function getFieldValue($details) |
||
402 | { |
||
403 | $this->initializeSprGlbAndSession(); |
||
404 | $rqCN = $this->tCmnRequest->request->get($details['COLUMN_NAME']); |
||
405 | if (!is_null($rqCN)) { |
||
406 | if (($details['IS_NULLABLE'] == 'YES') && ($rqCN == '')) { |
||
407 | return 'NULL'; |
||
408 | } |
||
409 | return $rqCN; |
||
410 | } |
||
411 | return $this->getFieldValueWithoutUserInput($details); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Handles field value ignoring any input from the user |
||
416 | * |
||
417 | * @param array $details |
||
418 | * @return string |
||
419 | */ |
||
420 | private function getFieldValueWithoutUserInput($details) |
||
430 | |||
431 | private function getForeignKeysQuery($value) |
||
440 | |||
441 | /** |
||
442 | * Returns an array with fields referenced by a Foreign key |
||
443 | * |
||
444 | * @param string $database |
||
445 | * @param string $tblName |
||
446 | * @param string|array $onlyCol |
||
447 | * @return array |
||
448 | */ |
||
449 | private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
||
450 | { |
||
451 | $this->setTableForeginKeyCache($database, $this->fixTableSource($tblName)); |
||
452 | $array2return = null; |
||
453 | if (isset($this->advCache['tableFKs'][$database][$tblName])) { |
||
454 | foreach ($this->advCache['tableFKs'][$database][$tblName] as $value) { |
||
455 | if ($value['COLUMN_NAME'] == $onlyCol) { |
||
456 | $query = $this->getForeignKeysQuery($value); |
||
457 | $targetTblTxtFlds = $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result']; |
||
458 | $array2return[$onlyCol] = [ |
||
459 | $this->glueDbTb($value['REFERENCED_TABLE_SCHEMA'], $value['REFERENCED_TABLE_NAME']), |
||
460 | $value['REFERENCED_COLUMN_NAME'], |
||
461 | '`' . $targetTblTxtFlds[0]['COLUMN_NAME'] . '`', |
||
462 | ]; |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 | return $array2return; |
||
467 | } |
||
468 | |||
469 | private function getLabel($details) |
||
470 | { |
||
471 | return $this->setStringIntoTag($this->getFieldNameForDisplay($details), 'span', ['class' => 'fake_label']); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Returns an array with possible values of a SET or ENUM column |
||
476 | * |
||
477 | * @param string $refTbl |
||
478 | * @param string $refCol |
||
479 | * @return array |
||
480 | */ |
||
481 | protected function getSetOrEnum2Array($refTbl, $refCol) |
||
482 | { |
||
483 | $dat = $this->establishDatabaseAndTable($this->fixTableSource($refTbl)); |
||
484 | foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $value) { |
||
485 | if ($value['COLUMN_NAME'] == $refCol) { |
||
486 | $clndVls = explode(',', str_replace([$value['DATA_TYPE'], '(', "'", ')'], '', $value['COLUMN_TYPE'])); |
||
487 | $enmVls = array_combine($clndVls, $clndVls); |
||
488 | if ($value['IS_NULLABLE'] == 'YES') { |
||
489 | $enmVls['NULL'] = ''; |
||
490 | } |
||
491 | } |
||
492 | } |
||
493 | ksort($enmVls); |
||
494 | return $enmVls; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Returns a timestamp field value |
||
499 | * |
||
500 | * @param array $dtl |
||
501 | * @return array |
||
502 | */ |
||
503 | private function getTimestamping($dtl) |
||
504 | { |
||
505 | $inM = $this->setStringIntoTag($this->getFieldValue($dtl), 'span'); |
||
506 | if (in_array($this->getFieldValue($dtl), ['', 'CURRENT_TIMESTAMP', 'NULL'])) { |
||
507 | $mCN = [ |
||
508 | 'InsertDateTime' => 'data/timpul ad. informatiei', |
||
509 | 'ModificationDateTime' => 'data/timpul modificarii inf.', |
||
510 | 'modification_datetime' => 'data/timpul modificarii inf.', |
||
511 | ]; |
||
512 | if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) { |
||
513 | $inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']); |
||
514 | } |
||
515 | } |
||
516 | return ['label' => $this->getLabel($dtl), 'input' => $inM]; |
||
517 | } |
||
518 | |||
519 | private function glueDbTb($dbName, $tbName) |
||
523 | |||
524 | /** |
||
525 | * Manages features flag |
||
526 | * |
||
527 | * @param string $fieldName |
||
528 | * @param array $features |
||
529 | * @return string |
||
530 | */ |
||
531 | private function handleFeatures($fieldName, $features) |
||
541 | |||
542 | private function handleFeaturesSingle($fieldName, $features, $featureKey) |
||
559 | |||
560 | private function setField($tableSource, $dtl, $features, $fieldLabel) |
||
561 | { |
||
562 | if (in_array($dtl['DATA_TYPE'], ['datetime', 'timestamp'])) { |
||
563 | if (($dtl['COLUMN_DEFAULT'] == 'CURRENT_TIMESTAMP') || ($dtl['EXTRA'] == 'on update CURRENT_TIMESTAMP')) { |
||
564 | return $this->getTimestamping($dtl); |
||
565 | } |
||
566 | } elseif ($dtl['COLUMN_NAME'] == 'host') { |
||
567 | $inVl = gethostbyaddr($this->tCmnRequest->server->get('REMOTE_ADDR')); |
||
568 | return [ |
||
569 | 'label' => '<label for="' . $dtl['COLUMN_NAME'] . '">Numele calculatorului</label>', |
||
570 | 'input' => '<input type="text" name="host" size="15" readonly value="' . $inVl . '" />', |
||
571 | ]; |
||
572 | } |
||
576 | |||
577 | private function setFieldInput($tableSource, $dtl, $features) |
||
585 | |||
586 | private function setFieldLabel($details, $features, $fieldLabel) |
||
596 | |||
597 | private function setFormButtons($feat, $hiddenInfo) |
||
612 | |||
613 | private function setFormJavascriptFinal($frmId) |
||
626 | |||
627 | /** |
||
628 | * Returns a generic form based on a given table |
||
629 | * |
||
630 | * @param string $tblSrc Table Source |
||
631 | * @param array $feat |
||
632 | * @param string|array $hdnInf List of hidden fields |
||
633 | * |
||
634 | * @return string Form to add/modify detail for a single row within a table |
||
635 | */ |
||
636 | protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = '') |
||
653 | |||
654 | /** |
||
655 | * Analyse the field and returns the proper line 2 use in forms |
||
656 | * |
||
657 | * @param string $tableSource |
||
658 | * @param array $details |
||
659 | * @param array $features |
||
660 | * @return string|array |
||
661 | */ |
||
662 | private function setNeededField($tableSource, $details, $features) |
||
683 | |||
684 | /** |
||
685 | * Analyse the field type and returns the proper lines 2 use in forms |
||
686 | * |
||
687 | * @param string $tblName |
||
688 | * @param array $dtls |
||
689 | * @param array $features |
||
690 | * @return string|array |
||
691 | */ |
||
692 | private function setNeededFieldByType($tblName, $dtls, $features) |
||
700 | |||
701 | private function setNeededFieldKnown($tblName, $dtls, $features) |
||
715 | |||
716 | private function setNeededFieldSingleType($tblName, $dtls, $iar) |
||
727 | |||
728 | private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
||
737 | |||
738 | /** |
||
739 | * create a Cache for given table to use it in many places |
||
740 | * |
||
741 | * @param type $tblSrc |
||
742 | */ |
||
743 | private function setTableCache($tblSrc) |
||
758 | |||
759 | private function setTableForeginKeyCache($dbName, $tblName) |
||
771 | } |
||
772 |