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 ListboxField 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 ListboxField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ListboxField extends DropdownField { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The size of the field in rows. |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $size; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Should the user be able to select multiple |
||
| 38 | * items on this dropdown field? |
||
| 39 | * |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | protected $multiple = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Array |
||
| 46 | */ |
||
| 47 | protected $disabledItems = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Array |
||
| 51 | */ |
||
| 52 | protected $defaultItems = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a new dropdown field. |
||
| 56 | * |
||
| 57 | * @param string $name The field name |
||
| 58 | * @param string $title The field title |
||
| 59 | * @param array $source An map of the dropdown items |
||
| 60 | * @param string|array $value You can pass an array of values or a single value like a drop down to be selected |
||
| 61 | * @param int $size Optional size of the select element |
||
| 62 | * @param form The parent form |
||
| 63 | */ |
||
| 64 | public function __construct($name, $title = '', $source = array(), $value = '', $size = null, $multiple = false) { |
||
| 65 | if($size) $this->size = $size; |
||
|
|
|||
| 66 | if($multiple) $this->multiple = $multiple; |
||
| 67 | |||
| 68 | parent::__construct($name, $title, $source, $value); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns a <select> tag containing all the appropriate <option> tags |
||
| 73 | */ |
||
| 74 | public function Field($properties = array()) { |
||
| 75 | if($this->multiple) $this->name .= '[]'; |
||
| 76 | $options = array(); |
||
| 77 | |||
| 78 | // We have an array of values |
||
| 79 | if(is_array($this->value)){ |
||
| 80 | // Loop through and figure out which values were selected. |
||
| 81 | View Code Duplication | foreach($this->getSource() as $value => $title) { |
|
| 82 | $options[] = new ArrayData(array( |
||
| 83 | 'Title' => $title, |
||
| 84 | 'Value' => $value, |
||
| 85 | 'Selected' => (in_array($value, $this->value) || in_array($value, $this->defaultItems)), |
||
| 86 | 'Disabled' => $this->disabled || in_array($value, $this->disabledItems), |
||
| 87 | )); |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | // Listbox was based a singlular value, so treat it like a dropdown. |
||
| 91 | View Code Duplication | foreach($this->getSource() as $value => $title) { |
|
| 92 | $options[] = new ArrayData(array( |
||
| 93 | 'Title' => $title, |
||
| 94 | 'Value' => $value, |
||
| 95 | 'Selected' => ($value == $this->value || in_array($value, $this->defaultItems)), |
||
| 96 | 'Disabled' => $this->disabled || in_array($value, $this->disabledItems), |
||
| 97 | )); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $properties = array_merge($properties, array( |
||
| 102 | 'Options' => new ArrayList($options) |
||
| 103 | )); |
||
| 104 | |||
| 105 | return $this->customise($properties)->renderWith($this->getTemplates()); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getAttributes() { |
||
| 109 | return array_merge( |
||
| 110 | parent::getAttributes(), |
||
| 111 | array( |
||
| 112 | 'multiple' => $this->multiple, |
||
| 113 | 'size' => $this->size |
||
| 114 | ) |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets the size of this dropdown in rows. |
||
| 120 | * @param int $size The height in rows (e.g. 3) |
||
| 121 | */ |
||
| 122 | public function setSize($size) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Sets this field to have a muliple select attribute |
||
| 129 | * @param boolean $bool |
||
| 130 | */ |
||
| 131 | public function setMultiple($bool) { |
||
| 135 | |||
| 136 | public function setSource($source) { |
||
| 137 | if($source) { |
||
| 138 | $hasCommas = array_filter(array_keys($source), |
||
| 139 | create_function('$key', 'return strpos($key, ",") !== FALSE;')); |
||
| 140 | if($hasCommas) { |
||
| 141 | throw new InvalidArgumentException('No commas allowed in $source keys'); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | parent::setSource($source); |
||
| 146 | |||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return the CheckboxSetField value as a string |
||
| 152 | * selected item keys. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function dataValue() { |
|
| 157 | if($this->value && is_array($this->value) && $this->multiple) { |
||
| 158 | $filtered = array(); |
||
| 159 | foreach($this->value as $item) { |
||
| 160 | if($item) { |
||
| 161 | $filtered[] = str_replace(",", "{comma}", $item); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | return implode(',', $filtered); |
||
| 165 | } else { |
||
| 166 | return parent::dataValue(); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Save the current value of this field into a DataObject. |
||
| 172 | * If the field it is saving to is a has_many or many_many relationship, |
||
| 173 | * it is saved by setByIDList(), otherwise it creates a comma separated |
||
| 174 | * list for a standard DB text/varchar field. |
||
| 175 | * |
||
| 176 | * @param DataObject $record The record to save into |
||
| 177 | */ |
||
| 178 | public function saveInto(DataObjectInterface $record) { |
||
| 179 | if($this->multiple) { |
||
| 180 | $fieldname = $this->name; |
||
| 181 | $relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null; |
||
| 182 | if($fieldname && $record && $relation && |
||
| 183 | ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { |
||
| 184 | $idList = (is_array($this->value)) ? array_values($this->value) : array(); |
||
| 185 | if(!$record->ID) { |
||
| 186 | $record->write(); // record needs to have an ID in order to set relationships |
||
| 187 | $relation = ($fieldname && $record && $record->hasMethod($fieldname)) |
||
| 188 | ? $record->$fieldname() |
||
| 189 | : null; |
||
| 190 | } |
||
| 191 | $relation->setByIDList($idList); |
||
| 192 | View Code Duplication | } elseif($fieldname && $record) { |
|
| 193 | if($this->value) { |
||
| 194 | $this->value = str_replace(',', '{comma}', $this->value); |
||
| 195 | $record->$fieldname = implode(",", $this->value); |
||
| 196 | } else { |
||
| 197 | $record->$fieldname = null; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | parent::saveInto($record); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Load a value into this ListboxField |
||
| 207 | */ |
||
| 208 | public function setValue($val, $obj = null) { |
||
| 209 | // If we're not passed a value directly, |
||
| 210 | // we can look for it in a relation method on the object passed as a second arg |
||
| 211 | View Code Duplication | if(!$val && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) { |
|
| 212 | $funcName = $this->name; |
||
| 213 | $val = array_values($obj->$funcName()->getIDList()); |
||
| 214 | } |
||
| 215 | |||
| 216 | if($val) { |
||
| 217 | if(!$this->multiple && is_array($val)) { |
||
| 218 | throw new InvalidArgumentException('Array values are not allowed (when multiple=false).'); |
||
| 219 | } |
||
| 220 | |||
| 221 | if($this->multiple) { |
||
| 222 | $parts = (is_array($val)) ? $val : preg_split("/ *, */", trim($val)); |
||
| 223 | if(ArrayLib::is_associative($parts)) { |
||
| 224 | // This is due to the possibility of accidentally passing an array of values (as keys) and titles (as values) when only the keys were intended to be saved. |
||
| 225 | throw new InvalidArgumentException('Associative arrays are not allowed as values (when multiple=true), only indexed arrays.'); |
||
| 226 | } |
||
| 227 | |||
| 228 | // Doesn't check against unknown values in order to allow for less rigid data handling. |
||
| 229 | // They're silently ignored and overwritten the next time the field is saved. |
||
| 230 | parent::setValue($parts); |
||
| 231 | } else { |
||
| 232 | if(!in_array($val, array_keys($this->getSource()))) { |
||
| 233 | throw new InvalidArgumentException(sprintf( |
||
| 234 | 'Invalid value "%s" for multiple=false', |
||
| 235 | Convert::raw2xml($val) |
||
| 236 | )); |
||
| 237 | } |
||
| 238 | |||
| 239 | parent::setValue($val); |
||
| 240 | } |
||
| 241 | } else { |
||
| 242 | parent::setValue($val); |
||
| 243 | } |
||
| 244 | |||
| 245 | return $this; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Mark certain elements as disabled, |
||
| 250 | * regardless of the {@link setDisabled()} settings. |
||
| 251 | * |
||
| 252 | * @param array $items Collection of array keys, as defined in the $source array |
||
| 253 | */ |
||
| 254 | public function setDisabledItems($items) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return Array |
||
| 261 | */ |
||
| 262 | public function getDisabledItems() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Default selections, regardless of the {@link setValue()} settings. |
||
| 268 | * Note: Items marked as disabled through {@link setDisabledItems()} can still be |
||
| 269 | * selected by default through this method. |
||
| 270 | * |
||
| 271 | * @param Array $items Collection of array keys, as defined in the $source array |
||
| 272 | */ |
||
| 273 | public function setDefaultItems($items) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return Array |
||
| 280 | */ |
||
| 281 | public function getDefaultItems() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Validate this field |
||
| 287 | * |
||
| 288 | * @param Validator $validator |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function validate($validator) { |
||
| 325 | |||
| 326 | } |
||
| 327 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: