Complex classes like Group 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 Group, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Group extends Tag |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The Container |
||
| 18 | * |
||
| 19 | * @var Container |
||
| 20 | */ |
||
| 21 | protected $app; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The current state of the group |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $state = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Whether the field should be displayed raw or not |
||
| 32 | * |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | protected $raw = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The group label |
||
| 39 | * |
||
| 40 | * @var Element |
||
| 41 | */ |
||
| 42 | protected $label; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The group help |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $help = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * An array of elements to preprend the field |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $prepend = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * An array of elements to append the field |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $append = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The field validations to be checked for errors |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $validations = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The group's element |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $element = 'div'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Whether a custom group is opened or not |
||
| 81 | * |
||
| 82 | * @var boolean |
||
| 83 | */ |
||
| 84 | public static $opened = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The custom group that is open |
||
| 88 | * |
||
| 89 | * @var Former\Form\Group |
||
| 90 | */ |
||
| 91 | public static $openGroup = null; |
||
| 92 | |||
| 93 | //////////////////////////////////////////////////////////////////// |
||
| 94 | /////////////////////////// CORE METHODS /////////////////////////// |
||
| 95 | //////////////////////////////////////////////////////////////////// |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Creates a group |
||
| 99 | * |
||
| 100 | * @param string $label Its label |
||
| 101 | */ |
||
| 102 | public function __construct(Container $app, $label, $validations = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Prints out the opening of the Control Group |
||
| 124 | * |
||
| 125 | * @return string A control group opening tag |
||
| 126 | */ |
||
| 127 | public function __toString() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Opens a group |
||
| 134 | * |
||
| 135 | * @return string Opening tag |
||
| 136 | */ |
||
| 137 | public function open() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Set the contents of the current group |
||
| 158 | * |
||
| 159 | * @param string $contents The group contents |
||
| 160 | * |
||
| 161 | * @return string A group |
||
| 162 | */ |
||
| 163 | public function contents($contents) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Wrap a Field with the current group |
||
| 170 | * |
||
| 171 | * @param \Former\Traits\Field $field A Field instance |
||
| 172 | * |
||
| 173 | * @return string A group |
||
| 174 | */ |
||
| 175 | public function wrapField($field) |
||
| 202 | |||
| 203 | //////////////////////////////////////////////////////////////////// |
||
| 204 | //////////////////////////// FIELD METHODS ///////////////////////// |
||
| 205 | //////////////////////////////////////////////////////////////////// |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the state of the group |
||
| 209 | * |
||
| 210 | * @param string $state A Bootstrap state class |
||
| 211 | */ |
||
| 212 | public function state($state) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set a class on the Group |
||
| 222 | * |
||
| 223 | * @param string $class The class(es) to add on the Group |
||
| 224 | */ |
||
| 225 | public function addGroupClass($class) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Remove one or more classes on the Group |
||
| 232 | * |
||
| 233 | * @param string $class The class(es) to remove on the Group |
||
| 234 | */ |
||
| 235 | public function removeGroupClass($class) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set a class on the Label |
||
| 242 | * |
||
| 243 | * @param string $class The class(es) to add on the Label |
||
| 244 | */ |
||
| 245 | public function addLabelClass($class) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Remove one or more classes on the Label |
||
| 259 | * |
||
| 260 | * @param string $class The class(es) to remove on the Label |
||
| 261 | */ |
||
| 262 | public function removeLabelClass($class) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adds a label to the group |
||
| 276 | * |
||
| 277 | * @param string $label A label |
||
| 278 | */ |
||
| 279 | public function setLabel($label) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the formatted group label |
||
| 291 | * |
||
| 292 | * @return string|null |
||
| 293 | */ |
||
| 294 | public function getFormattedLabel() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Disables the control group for the current field |
||
| 305 | */ |
||
| 306 | public function raw() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Check if the current group is to be displayed or not |
||
| 313 | * |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | public function isRaw() |
||
| 320 | |||
| 321 | //////////////////////////////////////////////////////////////////// |
||
| 322 | ///////////////////////////// HELP BLOCKS ////////////////////////// |
||
| 323 | //////////////////////////////////////////////////////////////////// |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Alias for inlineHelp |
||
| 327 | * |
||
| 328 | * @param string $help The help text |
||
| 329 | * @param array $attributes Facultative attributes |
||
| 330 | */ |
||
| 331 | public function help($help, $attributes = array()) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Add an inline help |
||
| 338 | * |
||
| 339 | * @param string $help The help text |
||
| 340 | * @param array $attributes Facultative attributes |
||
| 341 | */ |
||
| 342 | public function inlineHelp($help, $attributes = array()) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Add an block help |
||
| 354 | * |
||
| 355 | * @param string $help The help text |
||
| 356 | * @param array $attributes Facultative attributes |
||
| 357 | */ |
||
| 358 | public function blockHelp($help, $attributes = array()) |
||
| 376 | |||
| 377 | //////////////////////////////////////////////////////////////////// |
||
| 378 | ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
||
| 379 | //////////////////////////////////////////////////////////////////// |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Prepend elements to the field |
||
| 383 | */ |
||
| 384 | public function prepend() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Append elements to the field |
||
| 391 | */ |
||
| 392 | public function append() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Prepends an icon to a field |
||
| 399 | * |
||
| 400 | * @param string $icon The icon to prepend |
||
| 401 | * @param array $attributes Its attributes |
||
| 402 | */ |
||
| 403 | public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Append an icon to a field |
||
| 412 | * |
||
| 413 | * @param string $icon The icon to prepend |
||
| 414 | * @param array $attributes Its attributes |
||
| 415 | */ |
||
| 416 | public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
||
| 422 | |||
| 423 | //////////////////////////////////////////////////////////////////// |
||
| 424 | //////////////////////////////// HELPERS /////////////////////////// |
||
| 425 | //////////////////////////////////////////////////////////////////// |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get the errors for the group |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function getErrors() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Wraps content in a group |
||
| 453 | * |
||
| 454 | * @param string $contents The content |
||
| 455 | * @param string $label The label to add |
||
| 456 | * |
||
| 457 | * @return string A group |
||
| 458 | */ |
||
| 459 | public function wrap($contents, $label = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Wraps content in a group with floating label |
||
| 471 | * |
||
| 472 | * @param string $contents The content |
||
| 473 | * @param string $label The label to add |
||
| 474 | * |
||
| 475 | * @return string A group |
||
| 476 | */ |
||
| 477 | public function wrapWithFloatingLabel($contents, $label = null) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Prints out the current label |
||
| 488 | * |
||
| 489 | * @param string $field The field to create a label for |
||
| 490 | * |
||
| 491 | * @return string A <label> tag |
||
| 492 | */ |
||
| 493 | protected function getLabel($field = null) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Prints out the current help |
||
| 517 | * |
||
| 518 | * @return string A .help-block or .help-inline |
||
| 519 | */ |
||
| 520 | protected function getHelp() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Format the field with prepended/appended elements |
||
| 536 | * |
||
| 537 | * @param Field $field The field to format |
||
| 538 | * |
||
| 539 | * @return string Field plus supplementary elements |
||
| 540 | */ |
||
| 541 | protected function prependAppend($field) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Place elements around the field |
||
| 552 | * |
||
| 553 | * @param array $items An array of items to place |
||
| 554 | * @param string $place Where they should end up (prepend|append) |
||
| 555 | */ |
||
| 556 | protected function placeAround($items, $place) |
||
| 564 | } |
||
| 565 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: