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 |
||
| 18 | trait FormTrait |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The URI of a program that processes the form information. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $action = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The HTTP method that the browser uses to submit the form. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $method = 'post'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The form's display mode for multilingual fields. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $l10nMode = 'loop_inputs'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The form's display mode for groups. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $groupDisplayMode; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The form's field groups. |
||
| 50 | * |
||
| 51 | * @var FormGroupInterface[] |
||
| 52 | */ |
||
| 53 | protected $groups = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The form's predefined data. |
||
| 57 | * |
||
| 58 | * @var array $formData |
||
| 59 | */ |
||
| 60 | private $formData = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Store the form's metadata instance. |
||
| 64 | * |
||
| 65 | * @var MetadataInterface |
||
| 66 | */ |
||
| 67 | private $metadata; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Store the form's group factory instance. |
||
| 71 | * |
||
| 72 | * @var FactoryInterface |
||
| 73 | */ |
||
| 74 | protected $formGroupFactory; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Store the form's group callback. |
||
| 78 | * |
||
| 79 | * @var callable |
||
| 80 | */ |
||
| 81 | private $groupCallback; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param FactoryInterface $factory A factory, to create customized form gorup objects. |
||
| 85 | * @return FormInterface Chainable |
||
| 86 | */ |
||
| 87 | public function setFormGroupFactory(FactoryInterface $factory) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @throws Exception If the form group factory object was not set / injected. |
||
| 96 | * @return FormInterface Chainable |
||
| 97 | */ |
||
| 98 | protected function formGroupFactory() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param callable $cb The group callback. |
||
| 111 | * @return FormInterface Chainable |
||
| 112 | */ |
||
| 113 | public function setGroupCallback(callable $cb) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $action The "action" value, typically a URL. |
||
| 122 | * @throws InvalidArgumentException If the action argument is not a string. |
||
| 123 | * @return FormInterface Chainable |
||
| 124 | */ |
||
| 125 | public function setAction($action) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function action() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the method (forcing lowercase) to "post" or "get". |
||
| 147 | * |
||
| 148 | * @param string $method Either "post" or "get". |
||
| 149 | * @throws InvalidArgumentException If the method is not post or get. |
||
| 150 | * @return FormInterface Chainable |
||
| 151 | */ |
||
| 152 | public function setMethod($method) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string Either "post" or "get". |
||
| 167 | */ |
||
| 168 | public function method() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $mode The l10n mode. |
||
| 175 | * @return FormInterface Chainable |
||
| 176 | */ |
||
| 177 | public function setL10nMode($mode) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function l10nMode() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the object's form groups. |
||
| 194 | * |
||
| 195 | * @param array $groups A collection of group structures. |
||
| 196 | * @return FormInterface Chainable |
||
| 197 | */ |
||
| 198 | public function setGroups(array $groups) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Add a form group. |
||
| 211 | * |
||
| 212 | * @param string $groupIdent The group identifier. |
||
| 213 | * @param array|FormGroupInterface $group The group object or structure. |
||
| 214 | * @throws InvalidArgumentException If the identifier is not a string or the group is invalid. |
||
| 215 | * @return FormInterface Chainable |
||
| 216 | */ |
||
| 217 | public function addGroup($groupIdent, $group) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Create a new form group widget. |
||
| 256 | * |
||
| 257 | * @param array|null $data Optional. The form group data to set. |
||
| 258 | * @return FormGroupInterface |
||
| 259 | */ |
||
| 260 | protected function createFormGroup(array $data = null) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Update the given form group widget. |
||
| 280 | * |
||
| 281 | * @param FormGroupInterface $group The form group to update. |
||
| 282 | * @param array|null $groupData Optional. The new group data to apply. |
||
| 283 | * @param string|null $groupIdent Optional. The new group identifier. |
||
| 284 | * @return FormGroupInterface |
||
| 285 | */ |
||
| 286 | protected function updateFormGroup( |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Retrieve the default form group class name. |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function defaultGroupType() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Retrieve the form groups. |
||
| 316 | * |
||
| 317 | * @param callable $groupCallback Optional callback applied to each form group. |
||
| 318 | * @return FormGroupInterface[]|Generator |
||
| 319 | */ |
||
| 320 | public function groups(callable $groupCallback = null) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Determine if the form has any groups. |
||
| 361 | * |
||
| 362 | * @return boolean |
||
| 363 | */ |
||
| 364 | public function hasGroups() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Determine if the form has a given group. |
||
| 371 | * |
||
| 372 | * @param string $groupIdent The group identifier to look up. |
||
| 373 | * @throws InvalidArgumentException If the group identifier is invalid. |
||
| 374 | * @return boolean |
||
| 375 | */ |
||
| 376 | public function hasGroup($groupIdent) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Count the number of form groups. |
||
| 389 | * |
||
| 390 | * @return integer |
||
| 391 | */ |
||
| 392 | public function numGroups() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Set the widget's content group display mode. |
||
| 399 | * |
||
| 400 | * Currently only supports "tab". |
||
| 401 | * |
||
| 402 | * @param string $mode Group display mode. |
||
| 403 | * @throws InvalidArgumentException If the display mode is not a string. |
||
| 404 | * @return ObjectFormWidget Chainable. |
||
| 405 | */ |
||
| 406 | public function setGroupDisplayMode($mode) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Retrieve the widget's content group display mode. |
||
| 425 | * |
||
| 426 | * @return string Group display mode. |
||
| 427 | */ |
||
| 428 | public function groupDisplayMode() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Determine if content groups are to be displayed as tabbable panes. |
||
| 435 | * |
||
| 436 | * @return boolean |
||
| 437 | */ |
||
| 438 | public function isTabbable() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param array $formData The (pre-populated) form data, as [$key=>$val] array. |
||
| 445 | * @return FormInterface Chainable |
||
| 446 | */ |
||
| 447 | public function setFormData(array $formData) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $key The form data key, or poperty identifier. |
||
| 456 | * @param mixed $val The form data value, for a given key. |
||
| 457 | * @throws InvalidArgumentException If the key argument is not a string. |
||
| 458 | * @return FormInterface Chainable |
||
| 459 | */ |
||
| 460 | View Code Duplication | public function addFormData($key, $val) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | public function formData() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Static comparison function used by {@see uasort()}. |
||
| 482 | * |
||
| 483 | * @param FormGroupInterface $a Form Group A. |
||
| 484 | * @param FormGroupInterface $b Form Group B. |
||
| 485 | * @return integer Sorting value: -1 or 1 |
||
| 486 | */ |
||
| 487 | View Code Duplication | protected static function sortGroupsByPriority( |
|
| 496 | } |
||
| 497 |
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.