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 FieldInput 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 FieldInput, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class FieldInput extends Field implements ExportableField, ImportableField |
||
| 11 | { |
||
| 12 | View Code Duplication | public function __construct() |
|
| 13 | { |
||
| 14 | parent::__construct(); |
||
| 15 | $this->_name = __('Text Input'); |
||
|
|
|||
| 16 | $this->_required = true; |
||
| 17 | |||
| 18 | $this->set('required', 'no'); |
||
| 19 | } |
||
| 20 | |||
| 21 | /*------------------------------------------------------------------------- |
||
| 22 | Definition: |
||
| 23 | -------------------------------------------------------------------------*/ |
||
| 24 | |||
| 25 | public function canFilter() |
||
| 29 | |||
| 30 | public function canPrePopulate() |
||
| 34 | |||
| 35 | public function isSortable() |
||
| 39 | |||
| 40 | public function allowDatasourceOutputGrouping() |
||
| 44 | |||
| 45 | public function allowDatasourceParamOutput() |
||
| 49 | |||
| 50 | /*------------------------------------------------------------------------- |
||
| 51 | Setup: |
||
| 52 | -------------------------------------------------------------------------*/ |
||
| 53 | |||
| 54 | public function createTable() |
||
| 55 | { |
||
| 56 | return Symphony::Database()->query( |
||
| 57 | "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` ( |
||
| 58 | `id` int(11) unsigned NOT null auto_increment, |
||
| 59 | `entry_id` int(11) unsigned NOT null, |
||
| 60 | `handle` varchar(255) default null, |
||
| 61 | `value` varchar(255) default null, |
||
| 62 | PRIMARY KEY (`id`), |
||
| 63 | UNIQUE KEY `entry_id` (`entry_id`), |
||
| 64 | KEY `handle` (`handle`), |
||
| 65 | KEY `value` (`value`) |
||
| 66 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /*------------------------------------------------------------------------- |
||
| 71 | Utilities: |
||
| 72 | -------------------------------------------------------------------------*/ |
||
| 73 | |||
| 74 | private function __applyValidationRules($data) |
||
| 75 | { |
||
| 76 | $rule = $this->get('validator'); |
||
| 77 | |||
| 78 | return ($rule ? General::validateString($data, $rule) : true); |
||
| 79 | } |
||
| 80 | |||
| 81 | private function __replaceAmpersands($value) |
||
| 85 | |||
| 86 | /*------------------------------------------------------------------------- |
||
| 87 | Settings: |
||
| 88 | -------------------------------------------------------------------------*/ |
||
| 89 | |||
| 90 | public function setFromPOST(array $settings = array()) |
||
| 91 | { |
||
| 92 | parent::setFromPOST($settings); |
||
| 93 | |||
| 94 | if ($this->get('validator') == '') { |
||
| 95 | $this->remove('validator'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
| 100 | { |
||
| 101 | parent::displaySettingsPanel($wrapper, $errors); |
||
| 102 | |||
| 103 | // Validation rule |
||
| 104 | $this->buildValidationSelect($wrapper, $this->get('validator'), 'fields['.$this->get('sortorder').'][validator]', 'input', $errors); |
||
| 105 | |||
| 106 | // Requirements and table display |
||
| 107 | $this->appendStatusFooter($wrapper); |
||
| 108 | } |
||
| 109 | |||
| 110 | View Code Duplication | public function commit() |
|
| 128 | |||
| 129 | /*------------------------------------------------------------------------- |
||
| 130 | Publish: |
||
| 131 | -------------------------------------------------------------------------*/ |
||
| 132 | |||
| 133 | public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) |
||
| 150 | |||
| 151 | public function checkPostFieldData($data, &$message, $entry_id = null) |
||
| 171 | |||
| 172 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
| 188 | |||
| 189 | /*------------------------------------------------------------------------- |
||
| 190 | Output: |
||
| 191 | -------------------------------------------------------------------------*/ |
||
| 192 | |||
| 193 | public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) |
||
| 214 | |||
| 215 | /*------------------------------------------------------------------------- |
||
| 216 | Import: |
||
| 217 | -------------------------------------------------------------------------*/ |
||
| 218 | |||
| 219 | public function getImportModes() |
||
| 226 | |||
| 227 | View Code Duplication | public function prepareImportValue($data, $mode, $entry_id = null) |
|
| 240 | |||
| 241 | /*------------------------------------------------------------------------- |
||
| 242 | Export: |
||
| 243 | -------------------------------------------------------------------------*/ |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Return a list of supported export modes for use with `prepareExportValue`. |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function getExportModes() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Give the field some data and ask it to return a value using one of many |
||
| 261 | * possible modes. |
||
| 262 | * |
||
| 263 | * @param mixed $data |
||
| 264 | * @param integer $mode |
||
| 265 | * @param integer $entry_id |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | public function prepareExportValue($data, $mode, $entry_id = null) |
||
| 289 | |||
| 290 | /*------------------------------------------------------------------------- |
||
| 291 | Filtering: |
||
| 292 | -------------------------------------------------------------------------*/ |
||
| 293 | |||
| 294 | View Code Duplication | public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) |
|
| 342 | |||
| 343 | /*------------------------------------------------------------------------- |
||
| 344 | Sorting: |
||
| 345 | -------------------------------------------------------------------------*/ |
||
| 346 | |||
| 347 | public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC') |
||
| 364 | |||
| 365 | /*------------------------------------------------------------------------- |
||
| 366 | Grouping: |
||
| 367 | -------------------------------------------------------------------------*/ |
||
| 368 | |||
| 369 | View Code Duplication | public function groupRecords($records) |
|
| 394 | } |
||
| 395 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: