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 FormTrait 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 FormTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait FormTrait |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The URI of a program that processes the form information. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $action = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The HTTP method that the browser uses to submit the form. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $method = 'post'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The form's display mode for multilingual fields. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $l10nMode = 'loop_inputs'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The form's display mode for groups. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $groupDisplayMode; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The form's field groups. |
||
| 52 | * |
||
| 53 | * @var FormGroupInterface[] |
||
| 54 | */ |
||
| 55 | protected $groups = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The form's predefined data. |
||
| 59 | * |
||
| 60 | * @var array $formData |
||
| 61 | */ |
||
| 62 | private $formData = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Store the form's metadata instance. |
||
| 66 | * |
||
| 67 | * @var MetadataInterface |
||
| 68 | */ |
||
| 69 | private $metadata; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Store the form's group factory instance. |
||
| 73 | * |
||
| 74 | * @var FactoryInterface |
||
| 75 | */ |
||
| 76 | protected $formGroupFactory; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Store the form's group callback. |
||
| 80 | * |
||
| 81 | * @var callable |
||
| 82 | */ |
||
| 83 | private $groupCallback; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Store the tab ident received through REQUEST. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $tabIdent; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Store the current form group. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $selectedFormGroup; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Comparison function used by {@see uasort()}. |
||
| 101 | * |
||
| 102 | * @param PrioritizableInterface $a Sortable entity A. |
||
| 103 | * @param PrioritizableInterface $b Sortable entity B. |
||
| 104 | * @return integer Sorting value: -1 or 1. |
||
| 105 | */ |
||
| 106 | abstract protected function sortItemsByPriority( |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param FactoryInterface $factory A factory, to create customized form gorup objects. |
||
| 113 | * @return FormInterface Chainable |
||
| 114 | */ |
||
| 115 | public function setFormGroupFactory(FactoryInterface $factory) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @throws Exception If the form group factory object was not set / injected. |
||
| 124 | * @return FormInterface Chainable |
||
| 125 | */ |
||
| 126 | protected function formGroupFactory() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param callable $cb The group callback. |
||
| 139 | * @return FormInterface Chainable |
||
| 140 | */ |
||
| 141 | public function setGroupCallback(callable $cb) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $action The "action" value, typically a URL. |
||
| 150 | * @throws InvalidArgumentException If the action argument is not a string. |
||
| 151 | * @return FormInterface Chainable |
||
| 152 | */ |
||
| 153 | public function setAction($action) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function action() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the method (forcing lowercase) to "post" or "get". |
||
| 175 | * |
||
| 176 | * @param string $method Either "post" or "get". |
||
| 177 | * @throws InvalidArgumentException If the method is not post or get. |
||
| 178 | * @return FormInterface Chainable |
||
| 179 | */ |
||
| 180 | public function setMethod($method) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return string Either "post" or "get". |
||
| 195 | */ |
||
| 196 | public function method() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $mode The l10n mode. |
||
| 203 | * @return FormInterface Chainable |
||
| 204 | */ |
||
| 205 | public function setL10nMode($mode) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function l10nMode() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set the object's form groups. |
||
| 222 | * |
||
| 223 | * @param array $groups A collection of group structures. |
||
| 224 | * @return FormInterface Chainable |
||
| 225 | */ |
||
| 226 | public function setGroups(array $groups) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return mixed |
||
| 239 | */ |
||
| 240 | public function tabIdent() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param mixed $tabIdent Store the tab ident received through REQUEST. |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | public function setTabIdent($tabIdent) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function selectedFormGroup() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param mixed $selectedFormGroup Store the current form group. |
||
| 266 | * @return self |
||
| 267 | */ |
||
| 268 | public function setSelectedFormGroup($selectedFormGroup) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Add a form group. |
||
| 277 | * |
||
| 278 | * @param string $groupIdent The group identifier. |
||
| 279 | * @param array|FormGroupInterface $group The group object or structure. |
||
| 280 | * @throws InvalidArgumentException If the identifier is not a string or the group is invalid. |
||
| 281 | * @return FormInterface Chainable |
||
| 282 | */ |
||
| 283 | public function addGroup($groupIdent, $group) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Parse a form group. |
||
| 302 | * |
||
| 303 | * @param string $groupIdent The group identifier. |
||
| 304 | * @param array|FormGroupInterface $group The group object or structure. |
||
| 305 | * @throws InvalidArgumentException If the identifier is not a string or the group is invalid. |
||
| 306 | * @return FormGroupInterface |
||
| 307 | */ |
||
| 308 | protected function parseFormGroup($groupIdent, $group) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Create a new form group widget. |
||
| 339 | * |
||
| 340 | * @param array|null $data Optional. The form group data to set. |
||
| 341 | * @return FormGroupInterface |
||
| 342 | */ |
||
| 343 | protected function createFormGroup(array $data = null) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Update the given form group widget. |
||
| 363 | * |
||
| 364 | * @param FormGroupInterface $group The form group to update. |
||
| 365 | * @param array|null $groupData Optional. The new group data to apply. |
||
| 366 | * @param string|null $groupIdent Optional. The new group identifier. |
||
| 367 | * @return FormGroupInterface |
||
| 368 | */ |
||
| 369 | protected function updateFormGroup( |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Retrieve the default form group class name. |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function defaultGroupType() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Retrieve the form groups. |
||
| 399 | * |
||
| 400 | * @param callable $groupCallback Optional callback applied to each form group. |
||
| 401 | * @return FormGroupInterface[]|Generator |
||
| 402 | */ |
||
| 403 | public function groups(callable $groupCallback = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param array|FormGroupInterface[] $groups Form groups to finalize. |
||
| 436 | * @return array|FormGroupInterface[] |
||
| 437 | */ |
||
| 438 | protected function finalizeFormGroups($groups) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Determine if the form has any groups. |
||
| 479 | * |
||
| 480 | * @return boolean |
||
| 481 | */ |
||
| 482 | public function hasGroups() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Determine if the form has a given group. |
||
| 489 | * |
||
| 490 | * @param string $groupIdent The group identifier to look up. |
||
| 491 | * @throws InvalidArgumentException If the group identifier is invalid. |
||
| 492 | * @return boolean |
||
| 493 | */ |
||
| 494 | public function hasGroup($groupIdent) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Count the number of form groups. |
||
| 507 | * |
||
| 508 | * @return integer |
||
| 509 | */ |
||
| 510 | public function numGroups() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Set the widget's content group display mode. |
||
| 517 | * |
||
| 518 | * Currently only supports "tab". |
||
| 519 | * |
||
| 520 | * @param string $mode Group display mode. |
||
| 521 | * @throws InvalidArgumentException If the display mode is not a string. |
||
| 522 | * @return ObjectFormWidget Chainable. |
||
| 523 | */ |
||
| 524 | public function setGroupDisplayMode($mode) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Retrieve the widget's content group display mode. |
||
| 543 | * |
||
| 544 | * @return string Group display mode. |
||
| 545 | */ |
||
| 546 | public function groupDisplayMode() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Determine if content groups are to be displayed as tabbable panes. |
||
| 553 | * |
||
| 554 | * @return boolean |
||
| 555 | */ |
||
| 556 | public function isTabbable() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param array $formData The (pre-populated) form data, as [$key=>$val] array. |
||
| 563 | * @return FormInterface Chainable |
||
| 564 | */ |
||
| 565 | public function setFormData(array $formData) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param string $key The form data key, or poperty identifier. |
||
| 574 | * @param mixed $val The form data value, for a given key. |
||
| 575 | * @throws InvalidArgumentException If the key argument is not a string. |
||
| 576 | * @return FormInterface Chainable |
||
| 577 | */ |
||
| 578 | View Code Duplication | public function addFormData($key, $val) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * @return array |
||
| 592 | */ |
||
| 593 | public function formData() |
||
| 597 | } |
||
| 598 |
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.