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 AbstractField 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 AbstractField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class AbstractField implements FieldsInterface { |
||
| 8 | |||
| 9 | protected $name; |
||
| 10 | |||
| 11 | protected $title; |
||
| 12 | |||
| 13 | protected $label; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string Index elementu w pliku konfiguracyjnym |
||
| 17 | */ |
||
| 18 | protected $fieldName; |
||
| 19 | |||
| 20 | protected $id; |
||
| 21 | |||
| 22 | protected $attributes; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * List of all properties |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $properties = []; |
||
| 29 | |||
| 30 | protected $value; |
||
| 31 | |||
| 32 | protected $rules; |
||
| 33 | protected $customRules; |
||
| 34 | |||
| 35 | protected $optionKeys; |
||
| 36 | |||
| 37 | protected $_viewNamespace = 'forms::'; |
||
| 38 | protected $_view; //Widok do załadowania |
||
| 39 | protected $_type; //Typ elementu (np text, password) ten sam widok tylko inny typ |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Pola specjalne dla których nie nadajemy automatycznie klas (form-control) itp. |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $specialFields = [ |
||
| 46 | 'separator', 'header', 'htmlTag' |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructs base element |
||
| 51 | * @param $fieldName |
||
| 52 | * @param array $data |
||
| 53 | */ |
||
| 54 | public function __construct($fieldName, $data = []) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Parses base data passed to field. More field specific data has to be parsed in classes extending AbstractField |
||
| 61 | * @param array $data |
||
| 62 | */ |
||
| 63 | public function parseBaseData($data) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Gets or sets attributes |
||
| 119 | * @param null|array $data |
||
| 120 | * @return Attributes |
||
| 121 | */ |
||
| 122 | public function attributes($data = null) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sets or gets value |
||
| 134 | * @param $value |
||
| 135 | * @param $key |
||
| 136 | * @return FieldsInterface|mixed $this |
||
| 137 | */ |
||
| 138 | public function value($value = null, $key = null) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Shortcut for values() method. Used to set value of field after initiation from config. Is needed for fields |
||
| 209 | * with checked traits where method is overrided |
||
| 210 | * @param null $value |
||
| 211 | * @param null $key |
||
| 212 | * @return FieldsInterface|mixed |
||
| 213 | */ |
||
| 214 | public function setValue($value = null, $key = null) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param null $id |
||
| 220 | * @return string|FieldsInterface |
||
| 221 | */ |
||
| 222 | View Code Duplication | public function id($id = null) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param null $name |
||
| 233 | * @return string|FieldsInterface |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function name($name = null) { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param null $title |
||
| 246 | * @return string|FieldsInterface |
||
| 247 | */ |
||
| 248 | public function title($title = null) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param null $_type |
||
| 259 | * @return string|FieldsInterface |
||
| 260 | */ |
||
| 261 | public function _type($_type = null) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sets or retrives rules for field and child fields |
||
| 272 | * @param null $rules |
||
| 273 | * @return string|FieldsInterface |
||
| 274 | */ |
||
| 275 | public function rules($rules = null) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns name=>rules array for validator |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getRulesForValidator() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns name of field in 'dot' notation. Eg. field[] -> field, field[test] -> field.test |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getDotNotatedName() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Adds rule if it don't exist |
||
| 315 | * @param $rule |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | View Code Duplication | public function addRule($rule) { |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Removes rule from field |
||
| 329 | * @param $rule |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | View Code Duplication | public function removeRule($rule) { |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param null $customRules |
||
| 343 | * @return string|FieldsInterface |
||
| 344 | */ |
||
| 345 | public function customRules($customRules = null) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Renders element |
||
| 356 | * @return \Illuminate\View\View |
||
| 357 | */ |
||
| 358 | public function render() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function __toString() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Sets property |
||
| 371 | * @param string $propertyName |
||
| 372 | * @param mixed $value |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function setProperty($propertyName, $value) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Gets property |
||
| 386 | * @param string $propertyName |
||
| 387 | * @return mixed |
||
| 388 | */ |
||
| 389 | public function getProperty($propertyName) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Magic methods |
||
| 400 | * @param $method |
||
| 401 | * @param $args |
||
| 402 | * @return AbstractField|mixed |
||
| 403 | */ |
||
| 404 | public function __call($method, $args) { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Checks if passed array is associative or indexed |
||
| 417 | * @param $arr |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | private function isAssoc($arr) { |
||
| 423 | |||
| 424 | |||
| 425 | } |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: