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 CheckboxSetField 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 CheckboxSetField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class CheckboxSetField extends OptionsetField { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $defaultItems = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @todo Explain different source data that can be used with this field, |
||
| 48 | * e.g. SQLMap, ArrayList or an array. |
||
| 49 | */ |
||
| 50 | public function Field($properties = array()) { |
||
| 51 | Requirements::css(FRAMEWORK_DIR . '/css/CheckboxSetField.css'); |
||
| 52 | |||
| 53 | $properties = array_merge($properties, array( |
||
| 54 | 'Options' => $this->getOptions() |
||
| 55 | )); |
||
| 56 | |||
| 57 | return $this->customise($properties)->renderWith( |
||
| 58 | $this->getTemplates() |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return ArrayList |
||
| 64 | */ |
||
| 65 | public function getOptions() { |
||
| 66 | $odd = 0; |
||
| 67 | |||
| 68 | $source = $this->source; |
||
| 69 | $values = $this->value; |
||
| 70 | $items = array(); |
||
| 71 | |||
| 72 | // Get values from the join, if available |
||
| 73 | if(is_object($this->form)) { |
||
| 74 | $record = $this->form->getRecord(); |
||
| 75 | if(!$values && $record && $record->hasMethod($this->name)) { |
||
| 76 | $funcName = $this->name; |
||
| 77 | $join = $record->$funcName(); |
||
| 78 | if($join) { |
||
| 79 | foreach($join as $joinItem) { |
||
| 80 | $values[] = $joinItem->ID; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | // Source is not an array |
||
| 87 | if(!is_array($source) && !is_a($source, 'SQLMap')) { |
||
| 88 | if(is_array($values)) { |
||
| 89 | $items = $values; |
||
| 90 | } else { |
||
| 91 | // Source and values are DataObject sets. |
||
| 92 | if($values && is_a($values, 'SS_List')) { |
||
| 93 | foreach($values as $object) { |
||
|
|
|||
| 94 | if(is_a($object, 'DataObject')) { |
||
| 95 | $items[] = $object->ID; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | View Code Duplication | } elseif($values && is_string($values)) { |
|
| 99 | if(!empty($values)) { |
||
| 100 | $items = explode(',', $values); |
||
| 101 | $items = str_replace('{comma}', ',', $items); |
||
| 102 | } else { |
||
| 103 | $items = array(); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } else { |
||
| 108 | // Sometimes we pass a singluar default value thats ! an array && !SS_List |
||
| 109 | if($values instanceof SS_List || is_array($values)) { |
||
| 110 | $items = $values; |
||
| 111 | View Code Duplication | } else { |
|
| 112 | if($values === null) { |
||
| 113 | $items = array(); |
||
| 114 | } |
||
| 115 | else { |
||
| 116 | if(!empty($values)) { |
||
| 117 | $items = explode(',', $values); |
||
| 118 | $items = str_replace('{comma}', ',', $items); |
||
| 119 | } else { |
||
| 120 | $items = array(); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if(is_array($source)) { |
||
| 127 | unset($source['']); |
||
| 128 | } |
||
| 129 | |||
| 130 | $options = array(); |
||
| 131 | |||
| 132 | if ($source == null) { |
||
| 133 | $source = array(); |
||
| 134 | } |
||
| 135 | |||
| 136 | foreach($source as $value => $item) { |
||
| 137 | if($item instanceof DataObject) { |
||
| 138 | $value = $item->ID; |
||
| 139 | $title = $item->Title; |
||
| 140 | } else { |
||
| 141 | $title = $item; |
||
| 142 | } |
||
| 143 | |||
| 144 | $itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value); |
||
| 145 | $odd = ($odd + 1) % 2; |
||
| 146 | $extraClass = $odd ? 'odd' : 'even'; |
||
| 147 | $extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value); |
||
| 148 | |||
| 149 | $options[] = new ArrayData(array( |
||
| 150 | 'ID' => $itemID, |
||
| 151 | 'Class' => $extraClass, |
||
| 152 | 'Name' => "{$this->name}[{$value}]", |
||
| 153 | 'Value' => $value, |
||
| 154 | 'Title' => $title, |
||
| 155 | 'isChecked' => in_array($value, $items) || in_array($value, $this->defaultItems), |
||
| 156 | 'isDisabled' => $this->disabled || in_array($value, $this->disabledItems) |
||
| 157 | )); |
||
| 158 | } |
||
| 159 | |||
| 160 | $options = new ArrayList($options); |
||
| 161 | |||
| 162 | $this->extend('updateGetOptions', $options); |
||
| 163 | |||
| 164 | return $options; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Default selections, regardless of the {@link setValue()} settings. |
||
| 169 | * Note: Items marked as disabled through {@link setDisabledItems()} can still be |
||
| 170 | * selected by default through this method. |
||
| 171 | * |
||
| 172 | * @param Array $items Collection of array keys, as defined in the $source array |
||
| 173 | */ |
||
| 174 | public function setDefaultItems($items) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return Array |
||
| 181 | */ |
||
| 182 | public function getDefaultItems() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Load a value into this CheckboxSetField |
||
| 188 | */ |
||
| 189 | public function setValue($value, $obj = null) { |
||
| 190 | // If we're not passed a value directly, we can look for it in a relation method on the object passed as a |
||
| 191 | // second arg |
||
| 192 | View Code Duplication | if(!$value && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) { |
|
| 193 | $funcName = $this->name; |
||
| 194 | $value = $obj->$funcName()->getIDList(); |
||
| 195 | } |
||
| 196 | |||
| 197 | parent::setValue($value, $obj); |
||
| 198 | |||
| 199 | return $this; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Save the current value of this CheckboxSetField into a DataObject. |
||
| 204 | * If the field it is saving to is a has_many or many_many relationship, |
||
| 205 | * it is saved by setByIDList(), otherwise it creates a comma separated |
||
| 206 | * list for a standard DB text/varchar field. |
||
| 207 | * |
||
| 208 | * @param DataObject $record The record to save into |
||
| 209 | */ |
||
| 210 | public function saveInto(DataObjectInterface $record) { |
||
| 211 | $fieldname = $this->name; |
||
| 212 | $relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null; |
||
| 213 | if($fieldname && $record && $relation && |
||
| 214 | ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { |
||
| 215 | $idList = array(); |
||
| 216 | if($this->value) foreach($this->value as $id => $bool) { |
||
| 217 | if($bool) { |
||
| 218 | $idList[] = $id; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | $relation->setByIDList($idList); |
||
| 222 | View Code Duplication | } elseif($fieldname && $record) { |
|
| 223 | if($this->value) { |
||
| 224 | $this->value = str_replace(',', '{comma}', $this->value); |
||
| 225 | $record->$fieldname = implode(',', (array) $this->value); |
||
| 226 | } else { |
||
| 227 | $record->$fieldname = ''; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Return the CheckboxSetField value as a string |
||
| 234 | * selected item keys. |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function dataValue() { |
|
| 239 | if($this->value && is_array($this->value)) { |
||
| 240 | $filtered = array(); |
||
| 241 | foreach($this->value as $item) { |
||
| 242 | if($item) { |
||
| 243 | $filtered[] = str_replace(",", "{comma}", $item); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | return implode(',', $filtered); |
||
| 248 | } |
||
| 249 | |||
| 250 | return ''; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function performDisabledTransformation() { |
||
| 254 | $clone = clone $this; |
||
| 255 | $clone->setDisabled(true); |
||
| 256 | |||
| 257 | return $clone; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Transforms the source data for this CheckboxSetField |
||
| 262 | * into a comma separated list of values. |
||
| 263 | * |
||
| 264 | * @return ReadonlyField |
||
| 265 | */ |
||
| 266 | public function performReadonlyTransformation() { |
||
| 267 | $values = ''; |
||
| 268 | $data = array(); |
||
| 269 | |||
| 270 | $items = $this->value; |
||
| 271 | if($this->source) { |
||
| 272 | foreach($this->source as $source) { |
||
| 273 | if(is_object($source)) { |
||
| 274 | $sourceTitles[$source->ID] = $source->Title; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | if($items) { |
||
| 280 | // Items is a DO Set |
||
| 281 | if($items instanceof SS_List) { |
||
| 282 | foreach($items as $item) { |
||
| 283 | $data[] = $item->Title; |
||
| 284 | } |
||
| 285 | if($data) $values = implode(', ', $data); |
||
| 286 | |||
| 287 | // Items is an array or single piece of string (including comma seperated string) |
||
| 288 | } else { |
||
| 289 | if(!is_array($items)) { |
||
| 290 | $items = preg_split('/ *, */', trim($items)); |
||
| 291 | } |
||
| 292 | |||
| 293 | foreach($items as $item) { |
||
| 294 | if(is_array($item)) { |
||
| 295 | $data[] = $item['Title']; |
||
| 296 | } elseif(is_array($this->source) && !empty($this->source[$item])) { |
||
| 297 | $data[] = $this->source[$item]; |
||
| 298 | } elseif(is_a($this->source, 'SS_List')) { |
||
| 299 | $data[] = $sourceTitles[$item]; |
||
| 300 | } else { |
||
| 301 | $data[] = $item; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | $values = implode(', ', $data); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | $field = $this->castedCopy('ReadonlyField'); |
||
| 310 | $field->setValue($values); |
||
| 311 | |||
| 312 | return $field; |
||
| 313 | } |
||
| 314 | |||
| 315 | public function Type() { |
||
| 318 | |||
| 319 | public function ExtraOptions() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Validate this field |
||
| 325 | * |
||
| 326 | * @param Validator $validator |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | public function validate($validator) { |
||
| 364 | |||
| 365 | } |
||
| 366 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.