Complex classes like Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class Field extends FormerObject implements FieldInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The IoC Container |
||
| 21 | * |
||
| 22 | * @var Container |
||
| 23 | */ |
||
| 24 | protected $app; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The Form instance |
||
| 28 | * |
||
| 29 | * @var Former\Form |
||
| 30 | */ |
||
| 31 | protected $form; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * A label for the field (if not using Bootstrap) |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $label; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The field's group |
||
| 42 | * |
||
| 43 | * @var Group |
||
| 44 | */ |
||
| 45 | protected $group; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The field's default element |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $element = 'input'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Whether the Field is self-closing or not |
||
| 56 | * |
||
| 57 | * @var boolean |
||
| 58 | */ |
||
| 59 | protected $isSelfClosing = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The field's bind destination |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $bind; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the current framework instance |
||
| 70 | * |
||
| 71 | * @return Framework |
||
| 72 | */ |
||
| 73 | protected function currentFramework() |
||
| 81 | |||
| 82 | //////////////////////////////////////////////////////////////////// |
||
| 83 | ///////////////////////////// INTERFACE //////////////////////////// |
||
| 84 | //////////////////////////////////////////////////////////////////// |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set up a Field instance |
||
| 88 | * |
||
| 89 | * @param string $type A field type |
||
| 90 | */ |
||
| 91 | public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Redirect calls to the group if necessary |
||
| 123 | * |
||
| 124 | * @param string $method |
||
| 125 | */ |
||
| 126 | public function __call($method, $parameters) |
||
| 127 | { |
||
| 128 | // Translate attributes |
||
| 129 | $translatable = $this->app['former']->getOption('translatable', array()); |
||
| 130 | if (in_array($method, $translatable) and isset($parameters[0])) { |
||
| 131 | $parameters[0] = Helpers::translate($parameters[0]); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Redirect calls to the Control Group |
||
| 135 | if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) { |
||
| 136 | $method = str_replace('onGroup', '', $method); |
||
| 137 | $method = lcfirst($method); |
||
| 138 | |||
| 139 | call_user_func_array(array($this->group, $method), $parameters); |
||
| 140 | |||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | return parent::__call($method, $parameters); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Prints out the field, wrapped in its group |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function wrapAndRender() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Prints out the field |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function __toString() |
||
| 178 | |||
| 179 | //////////////////////////////////////////////////////////////////// |
||
| 180 | ////////////////////////// PUBLIC INTERFACE //////////////////////// |
||
| 181 | //////////////////////////////////////////////////////////////////// |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Whether the current field is required or not |
||
| 185 | * |
||
| 186 | * @return boolean |
||
| 187 | */ |
||
| 188 | public function isRequired() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Set required live validation attribute |
||
| 195 | * |
||
| 196 | * @param boolean $isRequired |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function required($isRequired=true) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Check if a field is unwrappable (no label) |
||
| 212 | * |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | public function isUnwrappable() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Check if field is a checkbox or a radio |
||
| 228 | * |
||
| 229 | * @return boolean |
||
| 230 | */ |
||
| 231 | public function isCheckable() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if the field is a button |
||
| 238 | * |
||
| 239 | * @return boolean |
||
| 240 | */ |
||
| 241 | public function isButton() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get the rules applied to the current field |
||
| 248 | * |
||
| 249 | * @return array An array of rules |
||
| 250 | */ |
||
| 251 | public function getRules() |
||
| 255 | |||
| 256 | //////////////////////////////////////////////////////////////////// |
||
| 257 | //////////////////////// SETTERS AND GETTERS /////////////////////// |
||
| 258 | //////////////////////////////////////////////////////////////////// |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Apply a Live Validation rule by chaining |
||
| 262 | * |
||
| 263 | * @param string $rule The rule |
||
| 264 | */ |
||
| 265 | public function rule($rule) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Apply multiple rules passed as a string. |
||
| 280 | * |
||
| 281 | * @param $rules |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function rules($rules) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Adds a label to the group/field |
||
| 324 | * |
||
| 325 | * @param string $text A label |
||
| 326 | * @param array $attributes The label's attributes |
||
| 327 | * |
||
| 328 | * @return Field A field |
||
| 329 | */ |
||
| 330 | public function label($text, $attributes = array()) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set the Field value no matter what |
||
| 347 | * |
||
| 348 | * @param string $value A new value |
||
| 349 | */ |
||
| 350 | public function forceValue($value) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Classic setting of attribute, won't overwrite any populate() attempt |
||
| 359 | * |
||
| 360 | * @param string $value A new value |
||
| 361 | */ |
||
| 362 | public function value($value) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Change the field's name |
||
| 376 | * |
||
| 377 | * @param string $name The new name |
||
| 378 | */ |
||
| 379 | public function name($name) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the field's labels |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getLabel() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Change the field's bind destination |
||
| 401 | * |
||
| 402 | * @param $destination |
||
| 403 | */ |
||
| 404 | public function bind($destination) { |
||
| 412 | |||
| 413 | //////////////////////////////////////////////////////////////////// |
||
| 414 | //////////////////////////////// HELPERS /////////////////////////// |
||
| 415 | //////////////////////////////////////////////////////////////////// |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Use values stored in Former to populate the current field |
||
| 419 | */ |
||
| 420 | private function repopulate($fallback = null) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Ponders a label and a field name, and tries to get the best out of it |
||
| 440 | * |
||
| 441 | * @param string $label A label |
||
| 442 | * @param string $name A field name |
||
| 443 | * |
||
| 444 | * @return false|null A label and a field name |
||
| 445 | */ |
||
| 446 | private function automaticLabels($name, $label) |
||
| 467 | } |
||
| 468 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.