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 | 26 | public function handle(FormData $data) { |
|
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 30 | public function getMethod() { |
|
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $method |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | 3 | public function setMethod($method) { |
|
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * |
||
| 111 | */ |
||
| 112 | 36 | protected function cleanValidationFlag() { |
|
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Check if form is submitted and all elements are valid |
||
| 120 | * |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | 13 | public function isValid() { |
|
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 2 | public function getErrors() { |
|
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $error |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | 7 | protected function addError($error) { |
|
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if form is submitted |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 16 | public function isSubmitted() { |
|
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Return unique id of form |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | 30 | public function getUid() { |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @return ElementInterface[] |
||
| 196 | */ |
||
| 197 | 31 | public function getElements() { |
|
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $name |
||
| 204 | * @return ElementInterface |
||
| 205 | * @throws \InvalidArgumentException |
||
| 206 | */ |
||
| 207 | 4 | public function getElement($name) { |
|
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Attach element to this form. Overwrite element with same name |
||
| 217 | * |
||
| 218 | * @param ElementInterface $element |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | 2 | public function setElement(ElementInterface $element) { |
|
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @param ElementInterface $element |
||
| 230 | * @return $this |
||
| 231 | * @throws \Exception |
||
| 232 | */ |
||
| 233 | 32 | public function addElement(ElementInterface $element) { |
|
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $name |
||
| 246 | * @param string|null $text |
||
| 247 | * @return \Fiv\Form\Element\Input |
||
| 248 | */ |
||
| 249 | 12 | public function input($name, $text = null) { |
|
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $name |
||
| 260 | * @param string|null $text |
||
| 261 | * @return \Fiv\Form\Element\Input |
||
| 262 | */ |
||
| 263 | public function password($name, $text = null) { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $name |
||
| 274 | * @param null $text |
||
| 275 | * @return Select |
||
| 276 | */ |
||
| 277 | public function select($name, $text = null) { |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $name |
||
| 288 | * @param string $text |
||
| 289 | * @return RadioList |
||
| 290 | */ |
||
| 291 | public function radioList($name, $text = null) { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $name |
||
| 302 | * @param null $text |
||
| 303 | * @return TextArea |
||
| 304 | */ |
||
| 305 | 5 | public function textarea($name, $text = null) { |
|
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * ``` |
||
| 316 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 317 | * ``` |
||
| 318 | * @param string $name |
||
| 319 | * @param null $value |
||
| 320 | * @return \Fiv\Form\Element\Input |
||
| 321 | */ |
||
| 322 | 3 | public function hidden($name, $value = null) { |
|
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * ``` |
||
| 334 | * $form->submit('register', 'зареєструватись'); |
||
| 335 | * ``` |
||
| 336 | * @param string $name |
||
| 337 | * @param null $value |
||
| 338 | * @return Submit |
||
| 339 | */ |
||
| 340 | 3 | public function submit($name, $value = null) { |
|
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * ``` |
||
| 351 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 352 | * ``` |
||
| 353 | * @param string $name |
||
| 354 | * @param string|null $label |
||
| 355 | * @return Checkbox |
||
| 356 | */ |
||
| 357 | 6 | public function checkbox($name, $label = null) { |
|
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $name |
||
| 368 | * @param null $text |
||
| 369 | * @return CheckboxList |
||
| 370 | */ |
||
| 371 | public function checkboxList($name, $text = null) { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Render full form |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 2 | public function render() { |
|
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * You can easy rewrite this method for custom design of your forms |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | 2 | protected function renderElements() { |
|
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 3 | public function renderStart() { |
|
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | 3 | public function renderEnd() { |
|
| 451 | } |