Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FieldReverse_Relationship 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 FieldReverse_Relationship, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class FieldReverse_Relationship extends FieldRelationship |
||
19 | { |
||
20 | /** |
||
21 | * |
||
22 | * Name of the field table |
||
23 | * @var string |
||
24 | */ |
||
25 | const FIELD_TBL_NAME = 'tbl_fields_reverse_relationship'; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * Constructor for the Reverse_Relationship Field object |
||
30 | */ |
||
31 | public function __construct() |
||
64 | |||
65 | public function isSortable() |
||
69 | |||
70 | public function canFilter() |
||
74 | |||
75 | public function canPublishFilter() |
||
79 | |||
80 | public function canImport() |
||
84 | |||
85 | public function canPrePopulate() |
||
89 | |||
90 | public function mustBeUnique() |
||
94 | |||
95 | public function allowDatasourceOutputGrouping() |
||
99 | |||
100 | public function requiresSQLGrouping() |
||
104 | |||
105 | public function allowDatasourceParamOutput() |
||
109 | |||
110 | public function requiresTable() |
||
114 | |||
115 | public function createTable() |
||
119 | |||
120 | public function fetchIncludableElements() |
||
123 | |||
124 | /* ********** INPUT AND FIELD *********** */ |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | * Validates input |
||
129 | * Called before <code>processRawFieldData</code> |
||
130 | * @param $data |
||
131 | * @param $message |
||
132 | * @param $entry_id |
||
133 | */ |
||
134 | public function checkPostFieldData($data, &$message, $entry_id=null) |
||
138 | |||
139 | |||
140 | /** |
||
141 | * |
||
142 | * Process data before saving into database. |
||
143 | * |
||
144 | * @param array $data |
||
145 | * @param int $status |
||
146 | * @param boolean $simulate |
||
147 | * @param int $entry_id |
||
148 | * |
||
149 | * @return Array - data to be inserted into DB |
||
150 | */ |
||
151 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
156 | |||
157 | |||
158 | /** |
||
159 | * |
||
160 | * Validates the field settings before saving it into the field's table |
||
161 | */ |
||
162 | public function checkFields(Array &$errors, $checkForDuplicates = true) |
||
180 | |||
181 | /** |
||
182 | * |
||
183 | * Save field settings into the field's table |
||
184 | */ |
||
185 | public function commit() |
||
207 | |||
208 | /** |
||
209 | * Appends data into the XML tree of a Data Source |
||
210 | * @param $wrapper |
||
211 | * @param $data |
||
212 | */ |
||
213 | public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) |
||
217 | |||
218 | /* ********* UI *********** */ |
||
219 | |||
220 | /** |
||
221 | * |
||
222 | * Builds the UI for the field's settings when creating/editing a section |
||
223 | * @param XMLElement $wrapper |
||
224 | * @param array $errors |
||
225 | */ |
||
226 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
296 | |||
297 | /** |
||
298 | * |
||
299 | * Builds the UI for the publish page |
||
300 | * @param XMLElement $wrapper |
||
301 | * @param mixed $data |
||
302 | * @param mixed $flagWithError |
||
303 | * @param string $fieldnamePrefix |
||
304 | * @param string $fieldnamePostfix |
||
305 | */ |
||
306 | public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) |
||
348 | |||
349 | private function createActionBarMenu($field) |
||
383 | |||
384 | private static $erFields = array(); |
||
385 | private function getERFields() |
||
397 | |||
398 | private static $erSections = array(); |
||
399 | private function getERSections() |
||
400 | { |
||
401 | if (empty(self::$erSections) && !empty(self::$erFields)) { |
||
402 | $erFields = self::getERFields(); |
||
403 | $sectionIds = array_map(function ($erField) { |
||
404 | return $erField->get('parent_section'); |
||
405 | }, $erFields); |
||
406 | self::$erSections = $this->sectionManager |
||
407 | ->select() |
||
408 | ->sections($sectionIds) |
||
409 | ->execute() |
||
410 | ->rows(); |
||
411 | } |
||
412 | return self::$erSections; |
||
413 | } |
||
414 | |||
415 | private function buildSectionSelect($name) |
||
428 | |||
429 | View Code Duplication | private function appendSelectionSelect(&$wrapper) |
|
442 | |||
443 | private function buildFieldSelect($name) |
||
468 | |||
469 | View Code Duplication | protected function appendFieldSelect(&$wrapper) |
|
482 | |||
483 | /** |
||
484 | * |
||
485 | * Return a plain text representation of the field's data |
||
486 | * @param array $data |
||
487 | * @param int $entry_id |
||
488 | */ |
||
489 | public function prepareTextValue($data, $entry_id = null) |
||
518 | |||
519 | /** |
||
520 | * Format this field value for display as readable text value. |
||
521 | * |
||
522 | * @param array $data |
||
523 | * an associative array of data for this string. At minimum this requires a |
||
524 | * key of 'value'. |
||
525 | * @param integer $entry_id (optional) |
||
526 | * An option entry ID for more intelligent processing. Defaults to null. |
||
527 | * @param string $defaultValue (optional) |
||
528 | * The value to use when no plain text representation of the field's data |
||
529 | * can be made. Defaults to null. |
||
530 | * @return string |
||
531 | * the readable text summary of the values of this field instance. |
||
532 | */ |
||
533 | public function prepareReadableValue($data, $entry_id = null, $truncate = false, $defaultValue = 'None') |
||
566 | |||
567 | /** |
||
568 | * Format this field value for display in the publish index tables. |
||
569 | * |
||
570 | * @param array $data |
||
571 | * an associative array of data for this string. At minimum this requires a |
||
572 | * key of 'value'. |
||
573 | * @param XMLElement $link (optional) |
||
574 | * an XML link structure to append the content of this to provided it is not |
||
575 | * null. it defaults to null. |
||
576 | * @param integer $entry_id (optional) |
||
577 | * An option entry ID for more intelligent processing. defaults to null |
||
578 | * @return string |
||
579 | * the formatted string summary of the values of this field instance. |
||
580 | */ |
||
581 | public function prepareTableValue($data, XMLElement $link = null, $entry_id = null) |
||
640 | |||
641 | /** |
||
642 | * Build the SQL command to append to the default query to enable |
||
643 | * sorting of this field. By default this will sort the results by |
||
644 | * the entry id in ascending order. |
||
645 | * |
||
646 | * Extension developers should always implement both `buildSortingSQL()` |
||
647 | * and `buildSortingSelectSQL()`. |
||
648 | * |
||
649 | * @uses Field::isRandomOrder() |
||
650 | * @see Field::buildSortingSelectSQL() |
||
651 | * @param string $joins |
||
652 | * the join element of the query to append the custom join sql to. |
||
653 | * @param string $where |
||
654 | * the where condition of the query to append to the existing where clause. |
||
655 | * @param string $sort |
||
656 | * the existing sort component of the sql query to append the custom |
||
657 | * sort sql code to. |
||
658 | * @param string $order (optional) |
||
659 | * an optional sorting direction. this defaults to ascending. if this |
||
660 | * is declared either 'random' or 'rand' then a random sort is applied. |
||
661 | */ |
||
662 | public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC') |
||
670 | |||
671 | /** |
||
672 | * Build the needed SQL clause command to make `buildSortingSQL()` work on |
||
673 | * MySQL 5.7 in strict mode, which requires all columns in the ORDER BY |
||
674 | * clause to be included in the SELECT's projection. |
||
675 | * |
||
676 | * If no new projection is needed (like if the order is made via a sub-query), |
||
677 | * simply return null. |
||
678 | * |
||
679 | * For backward compatibility, this method checks if the sort expression |
||
680 | * contains `ed`.`value`. This check will be removed in Symphony 3.0.0. |
||
681 | * |
||
682 | * Extension developers should make their Fields implement |
||
683 | * `buildSortingSelectSQL()` when overriding `buildSortingSQL()`. |
||
684 | * |
||
685 | * @since Symphony 2.7.0 |
||
686 | * @uses Field::isRandomOrder() |
||
687 | * @see Field::buildSortingSQL() |
||
688 | * @param string $sort |
||
689 | * the existing sort component of the sql query, after it has been passed |
||
690 | * to `buildSortingSQL()` |
||
691 | * @param string $order (optional) |
||
692 | * an optional sorting direction. this defaults to ascending. Should be the |
||
693 | * same value that was passed to `buildSortingSQL()` |
||
694 | * @return string |
||
695 | * an optional select clause to append to the generated SQL query. |
||
696 | * This is needed when sorting on a column that is not part of the projection. |
||
697 | */ |
||
698 | public function buildSortingSelectSQL($sort, $order = 'ASC') |
||
721 | |||
722 | /** |
||
723 | * Creates the table needed for the settings of the field |
||
724 | */ |
||
725 | public static function createFieldTable() |
||
764 | |||
765 | public static function update_200() |
||
769 | |||
770 | public static function update_210() |
||
796 | |||
797 | /** |
||
798 | * |
||
799 | * Drops the table needed for the settings of the field |
||
800 | */ |
||
801 | public static function deleteFieldTable() |
||
809 | } |
||
810 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: