Complex classes like Form 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 Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Form extends Element\Html { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var null|string |
||
| 16 | */ |
||
| 17 | protected $uid = null; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var boolean|null |
||
| 21 | */ |
||
| 22 | protected $validationResult = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array|null |
||
| 26 | */ |
||
| 27 | protected $data = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Element\BaseElement[] |
||
| 31 | */ |
||
| 32 | protected $elements = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Default form attributes |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $attributes = [ |
||
| 40 | 'method' => 'post', |
||
| 41 | ]; |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @param $name |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | 6 | public function setName($name) { |
|
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * @return array |
||
| 57 | * @throws \Exception |
||
| 58 | */ |
||
| 59 | 11 | public function getData() { |
|
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array|\Iterator $data |
||
| 70 | * @throws \Exception |
||
| 71 | */ |
||
| 72 | 11 | public function setData($data) { |
|
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 4 | public function getMethod() { |
|
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $method |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | 2 | public function setMethod($method) { |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * |
||
| 129 | */ |
||
| 130 | 19 | protected function cleanValidationFlag() { |
|
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Check if form is submitted and all elements are valid |
||
| 137 | * |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | 9 | public function isValid() { |
|
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Check if form is submitted |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 11 | public function isSubmitted() { |
|
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Return unique id of form |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 14 | public function getUid() { |
|
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @return Element\BaseElement[] |
||
| 188 | */ |
||
| 189 | 4 | public function getElements() { |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $name |
||
| 196 | * @param string|null $text |
||
| 197 | * @return \Fiv\Form\Element\Input |
||
| 198 | */ |
||
| 199 | 7 | public function input($name, $text = null) { |
|
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $name |
||
| 210 | * @param string|null $text |
||
| 211 | * @return \Fiv\Form\Element\Input |
||
| 212 | */ |
||
| 213 | public function password($name, $text = null) { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $name |
||
| 224 | * @param null $text |
||
| 225 | * @return Select |
||
| 226 | */ |
||
| 227 | public function select($name, $text = null) { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $name |
||
| 238 | * @param string $text |
||
| 239 | * @return RadioList |
||
| 240 | */ |
||
| 241 | public function radioList($name, $text = null) { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $name |
||
| 252 | * @param null $text |
||
| 253 | * @return TextArea |
||
| 254 | */ |
||
| 255 | 4 | public function textarea($name, $text = null) { |
|
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * ``` |
||
| 266 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 267 | * ``` |
||
| 268 | * @param string $name |
||
| 269 | * @param null $value |
||
| 270 | * @return \Fiv\Form\Element\Input |
||
| 271 | */ |
||
| 272 | 3 | public function hidden($name, $value = null) { |
|
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * ``` |
||
| 284 | * $form->submit('register', 'зареєструватись'); |
||
| 285 | * ``` |
||
| 286 | * @param string $name |
||
| 287 | * @param null $value |
||
| 288 | * @return Submit |
||
| 289 | */ |
||
| 290 | 1 | public function submit($name, $value = null) { |
|
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * ``` |
||
| 301 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 302 | * ``` |
||
| 303 | * @param string $name |
||
| 304 | * @param string|null $label |
||
| 305 | * @return Checkbox |
||
| 306 | */ |
||
| 307 | public function checkbox($name, $label = null) { |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $name |
||
| 318 | * @param null $text |
||
| 319 | * @return CheckboxList |
||
| 320 | */ |
||
| 321 | public function checkboxList($name, $text = null) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Attach element to this form. Overwrite element with same name |
||
| 332 | * |
||
| 333 | * @param Element\BaseElement $element |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | 2 | public function setElement(Element\BaseElement $element) { |
|
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * @param Element\BaseElement $element |
||
| 345 | * @return $this |
||
| 346 | * @throws \Exception |
||
| 347 | */ |
||
| 348 | 16 | public function addElement(Element\BaseElement $element) { |
|
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Render full form |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | 2 | public function render() { |
|
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * You can easy rewrite this method for custom design of your forms |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 2 | protected function renderElements() { |
|
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | 3 | public function renderStart() { |
|
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 3 | public function renderEnd() { |
|
| 430 | |||
| 431 | } |