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 |
||
| 19 | class Form extends Element\Html { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var null|string |
||
| 23 | */ |
||
| 24 | protected $uid = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var boolean|null |
||
| 28 | */ |
||
| 29 | protected $validationResult = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $errorList = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $isSubmitted = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var DataElementInterface[] |
||
| 43 | */ |
||
| 44 | protected $elements = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Default form attributes |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $attributes = [ |
||
| 52 | 'method' => 'post', |
||
| 53 | ]; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * @param $name |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | 14 | public function setName($name) { |
|
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * @param FormData $data |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | 27 | public function handle(FormData $data) { |
|
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 31 | public function getMethod() { |
|
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $method |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | 3 | public function setMethod($method) { |
|
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * |
||
| 112 | */ |
||
| 113 | 32 | protected function cleanValidationFlag() { |
|
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Check if form is submitted and all elements are valid |
||
| 121 | * |
||
| 122 | * @return boolean |
||
| 123 | */ |
||
| 124 | 14 | public function isValid() { |
|
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | 2 | public function getErrors() { |
|
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $error |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 7 | protected function addError($error) { |
|
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if form is submitted |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 17 | public function isSubmitted() { |
|
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Return unique id of form |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | 31 | public function getUid() { |
|
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @return DataElementInterface[] |
||
| 208 | */ |
||
| 209 | 32 | public function getElements() { |
|
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $name |
||
| 216 | * @return DataElementInterface |
||
| 217 | * @throws \InvalidArgumentException |
||
| 218 | */ |
||
| 219 | 2 | public function getElement($name) { |
|
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Attach element to this form. Overwrite element with same name |
||
| 229 | * |
||
| 230 | * @param DataElementInterface $element |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | 2 | public function setElement(DataElementInterface $element) { |
|
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param DataElementInterface $element |
||
| 242 | * @return $this |
||
| 243 | * @throws \Exception |
||
| 244 | */ |
||
| 245 | 28 | public function addElement(DataElementInterface $element) { |
|
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $name |
||
| 258 | * @param string|null $text |
||
| 259 | * @return \Fiv\Form\Element\Input |
||
| 260 | */ |
||
| 261 | 12 | public function input($name, $text = null) { |
|
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $name |
||
| 272 | * @param string|null $text |
||
| 273 | * @return \Fiv\Form\Element\Input |
||
| 274 | */ |
||
| 275 | public function password($name, $text = null) { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $name |
||
| 286 | * @param null $text |
||
| 287 | * @return Select |
||
| 288 | */ |
||
| 289 | public function select($name, $text = null) { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $name |
||
| 300 | * @param string $text |
||
| 301 | * @return RadioList |
||
| 302 | */ |
||
| 303 | public function radioList($name, $text = null) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * @deprecated |
||
| 314 | * @see TextArea |
||
| 315 | * |
||
| 316 | * @param string $name |
||
| 317 | * @param null $text |
||
| 318 | * @return TextArea |
||
| 319 | */ |
||
| 320 | public function textarea($name, $text = null) { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * @deprecated |
||
| 333 | * @see Element\Hidden |
||
| 334 | * |
||
| 335 | * @param string $name |
||
| 336 | * @param null $value |
||
| 337 | * @return \Fiv\Form\Element\Input |
||
| 338 | */ |
||
| 339 | public function hidden($name, $value = null) { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * ``` |
||
| 352 | * $form->submit('register', 'зареєструватись'); |
||
| 353 | * ``` |
||
| 354 | * @param string $name |
||
| 355 | * @param null $value |
||
| 356 | * @return Submit |
||
| 357 | */ |
||
| 358 | 3 | public function submit($name, $value = null) { |
|
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * ``` |
||
| 369 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 370 | * ``` |
||
| 371 | * @param string $name |
||
| 372 | * @param string|null $label |
||
| 373 | * @return Checkbox |
||
| 374 | */ |
||
| 375 | public function checkbox($name, $label = null) { |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $name |
||
| 385 | * @param null $text |
||
| 386 | * @return CheckboxList |
||
| 387 | */ |
||
| 388 | public function checkboxList($name, $text = null) { |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Render full form |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | 2 | public function render() { |
|
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * You can easy rewrite this method for custom design of your forms |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 2 | protected function renderElements() { |
|
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 3 | public function renderStart() { |
|
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | 3 | public function renderEnd() { |
|
| 483 | } |