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:
| 1 | <?php |
||
| 12 | class StringTagField extends DropdownField |
||
|
|
|||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | public static $allowed_actions = array( |
||
| 18 | 'suggest', |
||
| 19 | ); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | protected $shouldLazyLoad = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $lazyLoadItemLimit = 10; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var null|DataObject |
||
| 33 | */ |
||
| 34 | protected $record; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $isMultiple = true; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $name |
||
| 43 | * @param string $title |
||
| 44 | * @param array|SS_List $source |
||
| 45 | * @param array|SS_List $value |
||
| 46 | */ |
||
| 47 | public function __construct($name, $title = '', $source = array(), $value = array()) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | public function getShouldLazyLoad() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param bool $shouldLazyLoad |
||
| 62 | * |
||
| 63 | * @return static |
||
| 64 | */ |
||
| 65 | public function setShouldLazyLoad($shouldLazyLoad) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | public function getLazyLoadItemLimit() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $lazyLoadItemLimit |
||
| 82 | * |
||
| 83 | * @return static |
||
| 84 | */ |
||
| 85 | public function setLazyLoadItemLimit($lazyLoadItemLimit) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function getIsMultiple() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param bool $isMultiple |
||
| 102 | * |
||
| 103 | * @return static |
||
| 104 | */ |
||
| 105 | public function setIsMultiple($isMultiple) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return null|DataObject |
||
| 114 | */ |
||
| 115 | public function getRecord() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param DataObject $record |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function setRecord(DataObject $record) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function Field($properties = array()) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | protected function getSuggestURL() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return ArrayList |
||
| 182 | */ |
||
| 183 | protected function getOptions() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function setValue($value, $source = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function getAttributes() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function saveInto(DataObjectInterface $record) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns a JSON string of tags, for lazy loading. |
||
| 259 | * |
||
| 260 | * @param SS_HTTPRequest $request |
||
| 261 | * |
||
| 262 | * @return SS_HTTPResponse |
||
| 263 | */ |
||
| 264 | public function suggest(SS_HTTPRequest $request) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns array of arrays representing tags. |
||
| 293 | * |
||
| 294 | * @param string $term |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | protected function getTags($term) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * DropdownField assumes value will be a scalar so we must |
||
| 335 | * override validate. This only applies to Silverstripe 3.2+ |
||
| 336 | * |
||
| 337 | * @param Validator $validator |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function validate($validator) |
||
| 344 | } |
||
| 345 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.