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