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 |
||
| 18 | class Form extends Element\Html { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var null|string |
||
| 22 | */ |
||
| 23 | protected $uid = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var boolean|null |
||
| 27 | */ |
||
| 28 | protected $validationResult = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $errorList = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $isSubmitted = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ElementInterface[] |
||
| 42 | */ |
||
| 43 | protected $elements = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Default form attributes |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $attributes = [ |
||
| 51 | 'method' => 'post', |
||
| 52 | ]; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $name |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | 13 | public function setName($name) { |
|
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * @param FormData $data |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | 23 | public function handle(FormData $data) { |
|
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 2 | public function getData() { |
|
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 27 | public function getMethod() { |
|
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $method |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | 3 | public function setMethod($method) { |
|
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * |
||
| 124 | */ |
||
| 125 | 33 | protected function cleanValidationFlag() { |
|
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Check if form is submitted and all elements are valid |
||
| 133 | * |
||
| 134 | * @return boolean |
||
| 135 | */ |
||
| 136 | 13 | public function isValid() { |
|
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 2 | public function getErrors() { |
|
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $error |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | 7 | protected function addError($error) { |
|
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Check if form is submitted |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | 16 | public function isSubmitted() { |
|
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Return unique id of form |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 27 | public function getUid() { |
|
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * @return ElementInterface[] |
||
| 209 | */ |
||
| 210 | 28 | public function getElements() { |
|
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * @param string $name |
||
| 217 | * @return ElementInterface |
||
| 218 | * @throws \InvalidArgumentException |
||
| 219 | */ |
||
| 220 | 4 | public function getElement($name) { |
|
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * Attach element to this form. Overwrite element with same name |
||
| 230 | * |
||
| 231 | * @param ElementInterface $element |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | 2 | public function setElement(ElementInterface $element) { |
|
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * @param ElementInterface $element |
||
| 243 | * @return $this |
||
| 244 | * @throws \Exception |
||
| 245 | */ |
||
| 246 | 29 | public function addElement(ElementInterface $element) { |
|
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $name |
||
| 259 | * @param string|null $text |
||
| 260 | * @return \Fiv\Form\Element\Input |
||
| 261 | */ |
||
| 262 | 13 | public function input($name, $text = null) { |
|
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $name |
||
| 273 | * @param string|null $text |
||
| 274 | * @return \Fiv\Form\Element\Input |
||
| 275 | */ |
||
| 276 | public function password($name, $text = null) { |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $name |
||
| 287 | * @param null $text |
||
| 288 | * @return Select |
||
| 289 | */ |
||
| 290 | public function select($name, $text = null) { |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $name |
||
| 301 | * @param string $text |
||
| 302 | * @return RadioList |
||
| 303 | */ |
||
| 304 | public function radioList($name, $text = null) { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $name |
||
| 315 | * @param null $text |
||
| 316 | * @return TextArea |
||
| 317 | */ |
||
| 318 | 5 | public function textarea($name, $text = null) { |
|
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * ``` |
||
| 329 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 330 | * ``` |
||
| 331 | * @param string $name |
||
| 332 | * @param null $value |
||
| 333 | * @return \Fiv\Form\Element\Input |
||
| 334 | */ |
||
| 335 | 3 | public function hidden($name, $value = null) { |
|
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * ``` |
||
| 347 | * $form->submit('register', 'зареєструватись'); |
||
| 348 | * ``` |
||
| 349 | * @param string $name |
||
| 350 | * @param null $value |
||
| 351 | * @return Submit |
||
| 352 | */ |
||
| 353 | 1 | public function submit($name, $value = null) { |
|
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * ``` |
||
| 364 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 365 | * ``` |
||
| 366 | * @param string $name |
||
| 367 | * @param string|null $label |
||
| 368 | * @return Checkbox |
||
| 369 | */ |
||
| 370 | 6 | public function checkbox($name, $label = null) { |
|
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * @param string $name |
||
| 381 | * @param null $text |
||
| 382 | * @return CheckboxList |
||
| 383 | */ |
||
| 384 | public function checkboxList($name, $text = null) { |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Render full form |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 2 | public function render() { |
|
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * You can easy rewrite this method for custom design of your forms |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 2 | protected function renderElements() { |
|
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 3 | public function renderStart() { |
|
| 456 | |||
| 457 | |||
| 458 | /** |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | 3 | public function renderEnd() { |
|
| 464 | } |