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 Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class Field |
||
| 10 | { |
||
| 11 | use RenderTrait; |
||
| 12 | |||
| 13 | const DEFAULT_TEMPLATE = 'row'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $name; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var mixed |
||
| 22 | */ |
||
| 23 | protected $value; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $type; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Form |
||
| 32 | */ |
||
| 33 | protected $parent; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $label; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $help; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $theme; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $template = self::DEFAULT_TEMPLATE; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $rules; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $attr = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $labelAttr = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $rowAttr = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $helpAttr = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Name of the property for value setting |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $valueProperty = 'value'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $required = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Field constructor. |
||
| 94 | * @param $name |
||
| 95 | * @param $type |
||
| 96 | * @param Form $parent |
||
| 97 | * @param array $options |
||
| 98 | */ |
||
| 99 | public function __construct($name, $type, Form $parent, array $options = []) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getName() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param mixed $name |
||
| 121 | */ |
||
| 122 | public function setName($name) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | public function getValue() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Set value (can be string, can be array etc) |
||
| 137 | * |
||
| 138 | * @param mixed $value |
||
| 139 | */ |
||
| 140 | public function setValue($value) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $type |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | protected function setType($type) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getType() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param Form $parent |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | protected function setParent(Form $parent) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return Form |
||
| 175 | */ |
||
| 176 | public function getParent() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getLabel() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $label |
||
| 191 | */ |
||
| 192 | public function setLabel($label) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $rules |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setRules($rules) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getRules() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getHelp() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param mixed $help |
||
| 233 | */ |
||
| 234 | public function setHelp($help) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function getAttr() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param array $attr |
||
| 249 | */ |
||
| 250 | public function setAttr($attr) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getLabelAttr() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param array $labelAttr |
||
| 265 | */ |
||
| 266 | public function setLabelAttr($labelAttr) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getRowAttr() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $rowAttr |
||
| 281 | */ |
||
| 282 | public function setRowAttr($rowAttr) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function getHelpAttr() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param array $helpAttr |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function setHelpAttr($helpAttr) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | public function isRequired() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Method alias |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function getRequired() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param boolean $required |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function setRequired($required) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $options |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function mergeOptions(array $options) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param array $old |
||
| 367 | * @param array $new |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | protected function arrayMerge($old, $new) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return array|null |
||
| 377 | */ |
||
| 378 | public function getErrors() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return string|null |
||
| 385 | */ |
||
| 386 | public function getError() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return string |
||
| 393 | * @throws \Exception |
||
| 394 | * @throws \Throwable |
||
| 395 | */ |
||
| 396 | public function renderLabel() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return string |
||
| 403 | * @throws \Exception |
||
| 404 | * @throws \Throwable |
||
| 405 | */ |
||
| 406 | public function renderWidget() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | * @throws \Exception |
||
| 414 | * @throws \Throwable |
||
| 415 | */ |
||
| 416 | public function renderError() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Render entire element |
||
| 423 | * |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | public function render() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Setup field value when initialized |
||
| 433 | */ |
||
| 434 | protected function setupValue() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param mixed $data |
||
| 450 | * @param string $name |
||
| 451 | * @return mixed|null |
||
| 452 | */ |
||
| 453 | protected function getDataValue($data, $name) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $key |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | protected function getOldInput($key) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $key |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | protected function hasOldInput($key) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * If object is a instance of Eloquent model, we have to make sure that relations were loaded |
||
| 489 | * |
||
| 490 | * @param $model |
||
| 491 | * @param string $key |
||
| 492 | * @return mixed |
||
| 493 | */ |
||
| 494 | protected function loadModelRelation($model, $key) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | protected function viewData() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | protected function getWidgetName() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param array $options |
||
| 544 | */ |
||
| 545 | protected function setDefaultOptions(array $options) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $string |
||
| 567 | * @return string |
||
| 568 | */ |
||
| 569 | public function transformToDotSyntax($string) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function __toString() |
||
| 581 | } |
||
| 582 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.