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 |
||
19 | class FieldReverse_Relationship extends FieldRelationship |
||
20 | { |
||
21 | /** |
||
22 | * |
||
23 | * Name of the field table |
||
24 | * @var string |
||
25 | */ |
||
26 | const FIELD_TBL_NAME = 'tbl_fields_reverse_relationship'; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * Constructor for the Reverse_Relationship Field object |
||
31 | */ |
||
32 | public function __construct() |
||
61 | |||
62 | public function isSortable() |
||
66 | |||
67 | public function canFilter() |
||
71 | |||
72 | public function canPublishFilter() |
||
76 | |||
77 | public function canImport() |
||
81 | |||
82 | public function canPrePopulate() |
||
86 | |||
87 | public function mustBeUnique() |
||
91 | |||
92 | public function allowDatasourceOutputGrouping() |
||
96 | |||
97 | public function requiresSQLGrouping() |
||
101 | |||
102 | public function allowDatasourceParamOutput() |
||
106 | |||
107 | public function requiresTable() |
||
111 | |||
112 | public function createTable() |
||
116 | |||
117 | public function fetchIncludableElements() |
||
120 | |||
121 | /* ********** INPUT AND FIELD *********** */ |
||
122 | |||
123 | /** |
||
124 | * |
||
125 | * Validates input |
||
126 | * Called before <code>processRawFieldData</code> |
||
127 | * @param $data |
||
128 | * @param $message |
||
129 | * @param $entry_id |
||
130 | */ |
||
131 | public function checkPostFieldData($data, &$message, $entry_id=null) |
||
135 | |||
136 | |||
137 | /** |
||
138 | * |
||
139 | * Process data before saving into database. |
||
140 | * |
||
141 | * @param array $data |
||
142 | * @param int $status |
||
143 | * @param boolean $simulate |
||
144 | * @param int $entry_id |
||
145 | * |
||
146 | * @return Array - data to be inserted into DB |
||
147 | */ |
||
148 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
153 | |||
154 | |||
155 | /** |
||
156 | * |
||
157 | * Validates the field settings before saving it into the field's table |
||
158 | */ |
||
159 | public function checkFields(Array &$errors, $checkForDuplicates = true) |
||
177 | |||
178 | /** |
||
179 | * |
||
180 | * Save field settings into the field's table |
||
181 | */ |
||
182 | public function commit() |
||
204 | |||
205 | /** |
||
206 | * Appends data into the XML tree of a Data Source |
||
207 | * @param $wrapper |
||
208 | * @param $data |
||
209 | */ |
||
210 | public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) |
||
214 | |||
215 | /* ********* UI *********** */ |
||
216 | |||
217 | /** |
||
218 | * |
||
219 | * Builds the UI for the field's settings when creating/editing a section |
||
220 | * @param XMLElement $wrapper |
||
221 | * @param array $errors |
||
222 | */ |
||
223 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
293 | |||
294 | /** |
||
295 | * |
||
296 | * Builds the UI for the publish page |
||
297 | * @param XMLElement $wrapper |
||
298 | * @param mixed $data |
||
299 | * @param mixed $flagWithError |
||
300 | * @param string $fieldnamePrefix |
||
301 | * @param string $fieldnamePostfix |
||
302 | */ |
||
303 | public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) |
||
337 | |||
338 | private function createActionBarMenu($field) |
||
365 | |||
366 | private static $erFields = array(); |
||
367 | private function getERFields() |
||
374 | |||
375 | private static $erSections = array(); |
||
376 | private function getERSections() |
||
387 | |||
388 | private function buildSectionSelect($name) |
||
401 | |||
402 | View Code Duplication | private function appendSelectionSelect(&$wrapper) |
|
415 | |||
416 | private function buildFieldSelect($name) |
||
433 | |||
434 | View Code Duplication | protected function appendFieldSelect(&$wrapper) |
|
447 | |||
448 | /** |
||
449 | * |
||
450 | * Return a plain text representation of the field's data |
||
451 | * @param array $data |
||
452 | * @param int $entry_id |
||
453 | */ |
||
454 | public function prepareTextValue($data, $entry_id = null) |
||
469 | |||
470 | /** |
||
471 | * Format this field value for display as readable text value. |
||
472 | * |
||
473 | * @param array $data |
||
474 | * an associative array of data for this string. At minimum this requires a |
||
475 | * key of 'value'. |
||
476 | * @param integer $entry_id (optional) |
||
477 | * An option entry ID for more intelligent processing. Defaults to null. |
||
478 | * @param string $defaultValue (optional) |
||
479 | * The value to use when no plain text representation of the field's data |
||
480 | * can be made. Defaults to null. |
||
481 | * @return string |
||
482 | * the readable text summary of the values of this field instance. |
||
483 | */ |
||
484 | public function prepareReadableValue($data, $entry_id = null, $truncate = false, $defaultValue = 'None') |
||
502 | |||
503 | /** |
||
504 | * Format this field value for display in the publish index tables. |
||
505 | * |
||
506 | * @param array $data |
||
507 | * an associative array of data for this string. At minimum this requires a |
||
508 | * key of 'value'. |
||
509 | * @param XMLElement $link (optional) |
||
510 | * an XML link structure to append the content of this to provided it is not |
||
511 | * null. it defaults to null. |
||
512 | * @param integer $entry_id (optional) |
||
513 | * An option entry ID for more intelligent processing. defaults to null |
||
514 | * @return string |
||
515 | * the formatted string summary of the values of this field instance. |
||
516 | */ |
||
517 | public function prepareTableValue($data, XMLElement $link = null, $entry_id = null) |
||
557 | |||
558 | /** |
||
559 | * Build the SQL command to append to the default query to enable |
||
560 | * sorting of this field. By default this will sort the results by |
||
561 | * the entry id in ascending order. |
||
562 | * |
||
563 | * Extension developers should always implement both `buildSortingSQL()` |
||
564 | * and `buildSortingSelectSQL()`. |
||
565 | * |
||
566 | * @uses Field::isRandomOrder() |
||
567 | * @see Field::buildSortingSelectSQL() |
||
568 | * @param string $joins |
||
569 | * the join element of the query to append the custom join sql to. |
||
570 | * @param string $where |
||
571 | * the where condition of the query to append to the existing where clause. |
||
572 | * @param string $sort |
||
573 | * the existing sort component of the sql query to append the custom |
||
574 | * sort sql code to. |
||
575 | * @param string $order (optional) |
||
576 | * an optional sorting direction. this defaults to ascending. if this |
||
577 | * is declared either 'random' or 'rand' then a random sort is applied. |
||
578 | */ |
||
579 | public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC') |
||
587 | |||
588 | /** |
||
589 | * Build the needed SQL clause command to make `buildSortingSQL()` work on |
||
590 | * MySQL 5.7 in strict mode, which requires all columns in the ORDER BY |
||
591 | * clause to be included in the SELECT's projection. |
||
592 | * |
||
593 | * If no new projection is needed (like if the order is made via a sub-query), |
||
594 | * simply return null. |
||
595 | * |
||
596 | * For backward compatibility, this method checks if the sort expression |
||
597 | * contains `ed`.`value`. This check will be removed in Symphony 3.0.0. |
||
598 | * |
||
599 | * Extension developers should make their Fields implement |
||
600 | * `buildSortingSelectSQL()` when overriding `buildSortingSQL()`. |
||
601 | * |
||
602 | * @since Symphony 2.7.0 |
||
603 | * @uses Field::isRandomOrder() |
||
604 | * @see Field::buildSortingSQL() |
||
605 | * @param string $sort |
||
606 | * the existing sort component of the sql query, after it has been passed |
||
607 | * to `buildSortingSQL()` |
||
608 | * @param string $order (optional) |
||
609 | * an optional sorting direction. this defaults to ascending. Should be the |
||
610 | * same value that was passed to `buildSortingSQL()` |
||
611 | * @return string |
||
612 | * an optional select clause to append to the generated SQL query. |
||
613 | * This is needed when sorting on a column that is not part of the projection. |
||
614 | */ |
||
615 | public function buildSortingSelectSQL($sort, $order = 'ASC') |
||
638 | |||
639 | /** |
||
640 | * Creates the table needed for the settings of the field |
||
641 | */ |
||
642 | public static function createFieldTable() |
||
661 | |||
662 | public static function update_200() |
||
666 | |||
667 | public static function update_210() |
||
683 | |||
684 | /** |
||
685 | * |
||
686 | * Drops the table needed for the settings of the field |
||
687 | */ |
||
688 | public static function deleteFieldTable() |
||
696 | } |
||
697 |
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: