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) |
||
| 192 | |||
| 193 | //////////////////////////////////////////////////////////////////// |
||
| 194 | //////////////////////////// FIELD METHODS ///////////////////////// |
||
| 195 | //////////////////////////////////////////////////////////////////// |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set the state of the group |
||
| 199 | * |
||
| 200 | * @param string $state A Bootstrap state class |
||
| 201 | */ |
||
| 202 | public function state($state) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set a class on the Group |
||
| 212 | * |
||
| 213 | * @param string $class The class to add |
||
| 214 | */ |
||
| 215 | public function addGroupClass($class) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Adds a label to the group |
||
| 222 | * |
||
| 223 | * @param string $label A label |
||
| 224 | */ |
||
| 225 | public function setLabel($label) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the formatted group label |
||
| 237 | * |
||
| 238 | * @return string|null |
||
| 239 | */ |
||
| 240 | public function getFormattedLabel() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Disables the control group for the current field |
||
| 251 | */ |
||
| 252 | public function raw() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Check if the current group is to be displayed or not |
||
| 259 | * |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | public function isRaw() |
||
| 266 | |||
| 267 | //////////////////////////////////////////////////////////////////// |
||
| 268 | ///////////////////////////// HELP BLOCKS ////////////////////////// |
||
| 269 | //////////////////////////////////////////////////////////////////// |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Alias for inlineHelp |
||
| 273 | * |
||
| 274 | * @param string $help The help text |
||
| 275 | * @param array $attributes Facultative attributes |
||
| 276 | */ |
||
| 277 | public function help($help, $attributes = array()) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Add an inline help |
||
| 284 | * |
||
| 285 | * @param string $help The help text |
||
| 286 | * @param array $attributes Facultative attributes |
||
| 287 | */ |
||
| 288 | public function inlineHelp($help, $attributes = array()) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Add an block help |
||
| 300 | * |
||
| 301 | * @param string $help The help text |
||
| 302 | * @param array $attributes Facultative attributes |
||
| 303 | */ |
||
| 304 | public function blockHelp($help, $attributes = array()) |
||
| 321 | |||
| 322 | //////////////////////////////////////////////////////////////////// |
||
| 323 | ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
||
| 324 | //////////////////////////////////////////////////////////////////// |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Prepend elements to the field |
||
| 328 | */ |
||
| 329 | public function prepend() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Append elements to the field |
||
| 336 | */ |
||
| 337 | public function append() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Prepends an icon to a field |
||
| 344 | * |
||
| 345 | * @param string $icon The icon to prepend |
||
| 346 | * @param array $attributes Its attributes |
||
| 347 | */ |
||
| 348 | public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Append an icon to a field |
||
| 357 | * |
||
| 358 | * @param string $icon The icon to prepend |
||
| 359 | * @param array $attributes Its attributes |
||
| 360 | */ |
||
| 361 | public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
||
| 367 | |||
| 368 | //////////////////////////////////////////////////////////////////// |
||
| 369 | //////////////////////////////// HELPERS /////////////////////////// |
||
| 370 | //////////////////////////////////////////////////////////////////// |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the errors for the group |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getErrors() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Wraps content in a group |
||
| 398 | * |
||
| 399 | * @param string $contents The content |
||
| 400 | * @param string $label The label to add |
||
| 401 | * |
||
| 402 | * @return string A group |
||
| 403 | */ |
||
| 404 | public function wrap($contents, $label = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Prints out the current label |
||
| 416 | * |
||
| 417 | * @param string $field The field to create a label for |
||
| 418 | * |
||
| 419 | * @return string A <label> tag |
||
| 420 | */ |
||
| 421 | protected function getLabel($field = null) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Prints out the current help |
||
| 445 | * |
||
| 446 | * @return string A .help-block or .help-inline |
||
| 447 | */ |
||
| 448 | protected function getHelp() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Format the field with prepended/appended elements |
||
| 464 | * |
||
| 465 | * @param Field $field The field to format |
||
| 466 | * |
||
| 467 | * @return string Field plus supplementary elements |
||
| 468 | */ |
||
| 469 | protected function prependAppend($field) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Place elements around the field |
||
| 480 | * |
||
| 481 | * @param array $items An array of items to place |
||
| 482 | * @param string $place Where they should end up (prepend|append) |
||
| 483 | */ |
||
| 484 | protected function placeAround($items, $place) |
||
| 492 | } |
||
| 493 |
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: