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 |
||
| 15 | class Group extends Tag |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The Container |
||
| 19 | * |
||
| 20 | * @var Container |
||
| 21 | */ |
||
| 22 | protected $app; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The current state of the group |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $state = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Whether the field should be displayed raw or not |
||
| 33 | * |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | protected $raw = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The group label |
||
| 40 | * |
||
| 41 | * @var Element |
||
| 42 | */ |
||
| 43 | protected $label; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The group help |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $help = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * An array of elements to preprend the field |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $prepend = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * An array of elements to append the field |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $append = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The field validations to be checked for errors |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $validations = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The group's element |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $element = 'div'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Whether a custom group is opened or not |
||
| 82 | * |
||
| 83 | * @var boolean |
||
| 84 | */ |
||
| 85 | public static $opened = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The custom group that is open |
||
| 89 | * |
||
| 90 | * @var Former\Form\Group |
||
| 91 | */ |
||
| 92 | public static $openGroup = null; |
||
| 93 | |||
| 94 | //////////////////////////////////////////////////////////////////// |
||
| 95 | /////////////////////////// CORE METHODS /////////////////////////// |
||
| 96 | //////////////////////////////////////////////////////////////////// |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Creates a group |
||
| 100 | * |
||
| 101 | * @param string $label Its label |
||
| 102 | */ |
||
| 103 | public function __construct(Container $app, $label, $validations = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Prints out the opening of the Control Group |
||
| 125 | * |
||
| 126 | * @return string A control group opening tag |
||
| 127 | */ |
||
| 128 | public function __toString() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Opens a group |
||
| 135 | * |
||
| 136 | * @return string Opening tag |
||
| 137 | */ |
||
| 138 | public function open() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set the contents of the current group |
||
| 159 | * |
||
| 160 | * @param string $contents The group contents |
||
| 161 | * |
||
| 162 | * @return string A group |
||
| 163 | */ |
||
| 164 | public function contents($contents) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Wrap a Field with the current group |
||
| 171 | * |
||
| 172 | * @param \Former\Traits\Field $field A Field instance |
||
| 173 | * |
||
| 174 | * @return string A group |
||
| 175 | */ |
||
| 176 | public function wrapField($field) |
||
| 206 | |||
| 207 | //////////////////////////////////////////////////////////////////// |
||
| 208 | //////////////////////////// FIELD METHODS ///////////////////////// |
||
| 209 | //////////////////////////////////////////////////////////////////// |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the state of the group |
||
| 213 | * |
||
| 214 | * @param string $state A Bootstrap state class |
||
| 215 | */ |
||
| 216 | public function state($state) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set a class on the Group |
||
| 226 | * |
||
| 227 | * @param string $class The class(es) to add on the Group |
||
| 228 | */ |
||
| 229 | public function addGroupClass($class) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Remove one or more classes on the Group |
||
| 236 | * |
||
| 237 | * @param string $class The class(es) to remove on the Group |
||
| 238 | */ |
||
| 239 | public function removeGroupClass($class) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set a class on the Label |
||
| 246 | * |
||
| 247 | * @param string $class The class(es) to add on the Label |
||
| 248 | */ |
||
| 249 | public function addLabelClass($class) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Remove one or more classes on the Label |
||
| 263 | * |
||
| 264 | * @param string $class The class(es) to remove on the Label |
||
| 265 | */ |
||
| 266 | public function removeLabelClass($class) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Adds a label to the group |
||
| 280 | * |
||
| 281 | * @param string $label A label |
||
| 282 | */ |
||
| 283 | public function setLabel($label) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get the formatted group label |
||
| 295 | * |
||
| 296 | * @return string|null |
||
| 297 | */ |
||
| 298 | public function getFormattedLabel() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Disables the control group for the current field |
||
| 309 | */ |
||
| 310 | public function raw() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check if the current group is to be displayed or not |
||
| 317 | * |
||
| 318 | * @return boolean |
||
| 319 | */ |
||
| 320 | public function isRaw() |
||
| 324 | |||
| 325 | //////////////////////////////////////////////////////////////////// |
||
| 326 | ///////////////////////////// HELP BLOCKS ////////////////////////// |
||
| 327 | //////////////////////////////////////////////////////////////////// |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Alias for inlineHelp |
||
| 331 | * |
||
| 332 | * @param string $help The help text |
||
| 333 | * @param array $attributes Facultative attributes |
||
| 334 | */ |
||
| 335 | public function help($help, $attributes = array()) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Add an inline help |
||
| 342 | * |
||
| 343 | * @param string $help The help text |
||
| 344 | * @param array $attributes Facultative attributes |
||
| 345 | */ |
||
| 346 | public function inlineHelp($help, $attributes = array()) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Add an block help |
||
| 358 | * |
||
| 359 | * @param string $help The help text |
||
| 360 | * @param array $attributes Facultative attributes |
||
| 361 | */ |
||
| 362 | public function blockHelp($help, $attributes = array()) |
||
| 380 | |||
| 381 | //////////////////////////////////////////////////////////////////// |
||
| 382 | ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
||
| 383 | //////////////////////////////////////////////////////////////////// |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Prepend elements to the field |
||
| 387 | */ |
||
| 388 | public function prepend() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Append elements to the field |
||
| 395 | */ |
||
| 396 | public function append() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Prepends an icon to a field |
||
| 403 | * |
||
| 404 | * @param string $icon The icon to prepend |
||
| 405 | * @param array $attributes Its attributes |
||
| 406 | */ |
||
| 407 | public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Append an icon to a field |
||
| 416 | * |
||
| 417 | * @param string $icon The icon to prepend |
||
| 418 | * @param array $attributes Its attributes |
||
| 419 | */ |
||
| 420 | public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
||
| 426 | |||
| 427 | //////////////////////////////////////////////////////////////////// |
||
| 428 | //////////////////////////////// HELPERS /////////////////////////// |
||
| 429 | //////////////////////////////////////////////////////////////////// |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the errors for the group |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function getErrors() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Wraps content in a group |
||
| 457 | * |
||
| 458 | * @param string $contents The content |
||
| 459 | * @param string $label The label to add |
||
| 460 | * |
||
| 461 | * @return string A group |
||
| 462 | */ |
||
| 463 | public function wrap($contents, $label = null) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Wraps content in a group with floating label |
||
| 475 | * |
||
| 476 | * @param string $contents The content |
||
| 477 | * @param string $label The label to add |
||
| 478 | * |
||
| 479 | * @return string A group |
||
| 480 | */ |
||
| 481 | public function wrapWithFloatingLabel($contents, $label = null) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Prints out the current label |
||
| 492 | * |
||
| 493 | * @param string $field The field to create a label for |
||
| 494 | * |
||
| 495 | * @return string A <label> tag |
||
| 496 | */ |
||
| 497 | protected function getLabel($field = null) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Prints out the current help |
||
| 521 | * |
||
| 522 | * @return string A .help-block or .help-inline |
||
| 523 | */ |
||
| 524 | protected function getHelp() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Format the field with prepended/appended elements |
||
| 540 | * |
||
| 541 | * @param Field $field The field to format |
||
| 542 | * |
||
| 543 | * @return string Field plus supplementary elements |
||
| 544 | */ |
||
| 545 | protected function prependAppend($field) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Place elements around the field |
||
| 556 | * |
||
| 557 | * @param array $items An array of items to place |
||
| 558 | * @param string $place Where they should end up (prepend|append) |
||
| 559 | */ |
||
| 560 | protected function placeAround($items, $place) |
||
| 568 | } |
||
| 569 |
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: