Complex classes like Checkable 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 Checkable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class Checkable extends Field |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Renders the checkables as inline |
||
| 19 | * |
||
| 20 | * @var boolean |
||
| 21 | */ |
||
| 22 | protected $inline = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Add a text to a single element |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $text = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Renders the checkables as grouped |
||
| 33 | * |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | protected $grouped = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The checkable items currently stored |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $items = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The type of checkable item |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $checkable = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * An array of checked items |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $checked = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The checkable currently being focused on |
||
| 61 | * |
||
| 62 | * @var integer |
||
| 63 | */ |
||
| 64 | protected $focus = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Whether this particular checkable is to be pushed |
||
| 68 | * |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | protected $isPushed = null; |
||
| 72 | |||
| 73 | //////////////////////////////////////////////////////////////////// |
||
| 74 | //////////////////////////// CORE METHODS ////////////////////////// |
||
| 75 | //////////////////////////////////////////////////////////////////// |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Build a new checkable |
||
| 79 | * |
||
| 80 | * @param Container $app |
||
| 81 | * @param string $type |
||
| 82 | * @param array $name |
||
| 83 | * @param $label |
||
| 84 | * @param $value |
||
| 85 | * @param $attributes |
||
| 86 | */ |
||
| 87 | public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Apply methods to focused checkable |
||
| 103 | * |
||
| 104 | * @param string $method |
||
| 105 | * @param array $parameters |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function __call($method, $parameters) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Prints out the currently stored checkables |
||
| 121 | */ |
||
| 122 | public function render() |
||
| 147 | |||
| 148 | //////////////////////////////////////////////////////////////////// |
||
| 149 | ////////////////////////// FIELD METHODS /////////////////////////// |
||
| 150 | //////////////////////////////////////////////////////////////////// |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Focus on a particular checkable |
||
| 154 | * |
||
| 155 | * @param integer $on The checkable to focus on |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function on($on) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the checkables as inline |
||
| 172 | */ |
||
| 173 | public function inline($isInline = true) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Set the checkables as stacked |
||
| 182 | */ |
||
| 183 | public function stacked($isStacked = true) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set the checkables as grouped |
||
| 192 | */ |
||
| 193 | public function grouped($isGrouped = true) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add text to a single checkable |
||
| 202 | * |
||
| 203 | * @param string $text The checkable label |
||
| 204 | * |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function text($text) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Push this particular checkbox |
||
| 225 | * |
||
| 226 | * @param boolean $pushed |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | public function push($pushed = true) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Check a specific item |
||
| 239 | * |
||
| 240 | * @param bool|string $checked The checkable to check, or an array of checked items |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function check($checked = true) |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if the checkables are inline |
||
| 263 | * |
||
| 264 | * @return boolean |
||
| 265 | */ |
||
| 266 | public function isInline() |
||
| 270 | |||
| 271 | //////////////////////////////////////////////////////////////////// |
||
| 272 | ////////////////////////// INTERNAL METHODS //////////////////////// |
||
| 273 | //////////////////////////////////////////////////////////////////// |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Creates a series of checkable items |
||
| 277 | * |
||
| 278 | * @param array $_items Items to create |
||
| 279 | */ |
||
| 280 | protected function items($_items) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Renders a checkable |
||
| 341 | * |
||
| 342 | * @param string|array $item A checkable item |
||
| 343 | * @param integer $fallbackValue A fallback value if none is set |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | protected function createCheckable($item, $fallbackValue = 1) |
||
| 422 | |||
| 423 | //////////////////////////////////////////////////////////////////// |
||
| 424 | ///////////////////////////// HELPERS ////////////////////////////// |
||
| 425 | //////////////////////////////////////////////////////////////////// |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Generate an unique ID for a field |
||
| 429 | * |
||
| 430 | * @param string $name The field's name |
||
| 431 | * |
||
| 432 | * @return string A field number to use |
||
| 433 | */ |
||
| 434 | protected function unique($name) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set something on the currently focused checkable |
||
| 454 | * |
||
| 455 | * @param string $attribute The key to set |
||
| 456 | * @param string $value Its value |
||
| 457 | * |
||
| 458 | * @return $this|bool |
||
| 459 | */ |
||
| 460 | protected function setOnFocused($attribute, $value) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Check if a checkable is checked |
||
| 473 | * |
||
| 474 | * @return boolean Checked or not |
||
| 475 | */ |
||
| 476 | protected function isChecked($item = null, $value = null) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Check if the current element is a checkbox |
||
| 550 | * |
||
| 551 | * @return boolean Checkbox or radio |
||
| 552 | */ |
||
| 553 | protected function isCheckbox() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Check if the checkables are grouped or not |
||
| 560 | * |
||
| 561 | * @return boolean |
||
| 562 | */ |
||
| 563 | protected function isGrouped() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param array $item The item array, containing at least name and count keys. |
||
| 572 | * |
||
| 573 | * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
||
| 574 | */ |
||
| 575 | public static function getGroupIndexFromItem($item) |
||
| 584 | } |
||
| 585 |
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: