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 | * Comparison function used by {@see uasort()}. |
||
| 87 | * |
||
| 88 | * @param PrioritizableInterface $a Sortable entity A. |
||
| 89 | * @param PrioritizableInterface $b Sortable entity B. |
||
| 90 | * @return integer Sorting value: -1 or 1. |
||
| 91 | */ |
||
| 92 | abstract protected function sortItemsByPriority( |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param FactoryInterface $factory A factory, to create customized form gorup objects. |
||
| 99 | * @return FormInterface Chainable |
||
| 100 | */ |
||
| 101 | public function setFormGroupFactory(FactoryInterface $factory) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @throws Exception If the form group factory object was not set / injected. |
||
| 110 | * @return FormInterface Chainable |
||
| 111 | */ |
||
| 112 | protected function formGroupFactory() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param callable $cb The group callback. |
||
| 125 | * @return FormInterface Chainable |
||
| 126 | */ |
||
| 127 | public function setGroupCallback(callable $cb) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $action The "action" value, typically a URL. |
||
| 136 | * @throws InvalidArgumentException If the action argument is not a string. |
||
| 137 | * @return FormInterface Chainable |
||
| 138 | */ |
||
| 139 | public function setAction($action) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function action() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set the method (forcing lowercase) to "post" or "get". |
||
| 161 | * |
||
| 162 | * @param string $method Either "post" or "get". |
||
| 163 | * @throws InvalidArgumentException If the method is not post or get. |
||
| 164 | * @return FormInterface Chainable |
||
| 165 | */ |
||
| 166 | public function setMethod($method) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return string Either "post" or "get". |
||
| 181 | */ |
||
| 182 | public function method() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $mode The l10n mode. |
||
| 189 | * @return FormInterface Chainable |
||
| 190 | */ |
||
| 191 | public function setL10nMode($mode) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function l10nMode() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the object's form groups. |
||
| 208 | * |
||
| 209 | * @param array $groups A collection of group structures. |
||
| 210 | * @return FormInterface Chainable |
||
| 211 | */ |
||
| 212 | public function setGroups(array $groups) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Add a form group. |
||
| 225 | * |
||
| 226 | * @param string $groupIdent The group identifier. |
||
| 227 | * @param array|FormGroupInterface $group The group object or structure. |
||
| 228 | * @throws InvalidArgumentException If the identifier is not a string or the group is invalid. |
||
| 229 | * @return FormInterface Chainable |
||
| 230 | */ |
||
| 231 | public function addGroup($groupIdent, $group) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Parse a form group. |
||
| 250 | * |
||
| 251 | * @param string $groupIdent The group identifier. |
||
| 252 | * @param array|FormGroupInterface $group The group object or structure. |
||
| 253 | * @throws InvalidArgumentException If the identifier is not a string or the group is invalid. |
||
| 254 | * @return FormGroupInterface |
||
| 255 | */ |
||
| 256 | protected function parseFormGroup($groupIdent, $group) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Create a new form group widget. |
||
| 287 | * |
||
| 288 | * @param array|null $data Optional. The form group data to set. |
||
| 289 | * @return FormGroupInterface |
||
| 290 | */ |
||
| 291 | protected function createFormGroup(array $data = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Update the given form group widget. |
||
| 311 | * |
||
| 312 | * @param FormGroupInterface $group The form group to update. |
||
| 313 | * @param array|null $groupData Optional. The new group data to apply. |
||
| 314 | * @param string|null $groupIdent Optional. The new group identifier. |
||
| 315 | * @return FormGroupInterface |
||
| 316 | */ |
||
| 317 | protected function updateFormGroup( |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Retrieve the default form group class name. |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function defaultGroupType() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Retrieve the form groups. |
||
| 347 | * |
||
| 348 | * @param callable $groupCallback Optional callback applied to each form group. |
||
| 349 | * @return FormGroupInterface[]|Generator |
||
| 350 | */ |
||
| 351 | public function groups(callable $groupCallback = null) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Determine if the form has any groups. |
||
| 398 | * |
||
| 399 | * @return boolean |
||
| 400 | */ |
||
| 401 | public function hasGroups() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Determine if the form has a given group. |
||
| 408 | * |
||
| 409 | * @param string $groupIdent The group identifier to look up. |
||
| 410 | * @throws InvalidArgumentException If the group identifier is invalid. |
||
| 411 | * @return boolean |
||
| 412 | */ |
||
| 413 | public function hasGroup($groupIdent) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Count the number of form groups. |
||
| 426 | * |
||
| 427 | * @return integer |
||
| 428 | */ |
||
| 429 | public function numGroups() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set the widget's content group display mode. |
||
| 436 | * |
||
| 437 | * Currently only supports "tab". |
||
| 438 | * |
||
| 439 | * @param string $mode Group display mode. |
||
| 440 | * @throws InvalidArgumentException If the display mode is not a string. |
||
| 441 | * @return ObjectFormWidget Chainable. |
||
| 442 | */ |
||
| 443 | public function setGroupDisplayMode($mode) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Retrieve the widget's content group display mode. |
||
| 462 | * |
||
| 463 | * @return string Group display mode. |
||
| 464 | */ |
||
| 465 | public function groupDisplayMode() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Determine if content groups are to be displayed as tabbable panes. |
||
| 472 | * |
||
| 473 | * @return boolean |
||
| 474 | */ |
||
| 475 | public function isTabbable() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param array $formData The (pre-populated) form data, as [$key=>$val] array. |
||
| 482 | * @return FormInterface Chainable |
||
| 483 | */ |
||
| 484 | public function setFormData(array $formData) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $key The form data key, or poperty identifier. |
||
| 493 | * @param mixed $val The form data value, for a given key. |
||
| 494 | * @throws InvalidArgumentException If the key argument is not a string. |
||
| 495 | * @return FormInterface Chainable |
||
| 496 | */ |
||
| 497 | View Code Duplication | public function addFormData($key, $val) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * @return array |
||
| 511 | */ |
||
| 512 | public function formData() |
||
| 516 | } |
||
| 517 |
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.