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 | public function setName($name) { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * @param FormData $data |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | public function handle(FormData $data) { |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getMethod() { |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $method |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function setMethod($method) { |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * |
||
| 112 | */ |
||
| 113 | protected function cleanValidationFlag() { |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Check if form is submitted and all elements are valid |
||
| 121 | * |
||
| 122 | * @return boolean |
||
| 123 | */ |
||
| 124 | public function isValid() { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function getErrors() { |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $error |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | protected function addError($error) { |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if form is submitted |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function isSubmitted() { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Return unique id of form |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getUid() { |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @return DataElementInterface[] |
||
| 208 | */ |
||
| 209 | public function getElements() { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $name |
||
| 216 | * @return DataElementInterface |
||
| 217 | * @throws \InvalidArgumentException |
||
| 218 | */ |
||
| 219 | 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 | public function setElement(DataElementInterface $element) { |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param DataElementInterface $element |
||
| 242 | * @return $this |
||
| 243 | * @throws \Exception |
||
| 244 | */ |
||
| 245 | 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 | 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 | * ``` |
||
| 333 | * $form->submit('register', 'зареєструватись'); |
||
| 334 | * ``` |
||
| 335 | * @param string $name |
||
| 336 | * @param null $value |
||
| 337 | * @return Submit |
||
| 338 | */ |
||
| 339 | public function submit($name, $value = null) { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * ``` |
||
| 350 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 351 | * ``` |
||
| 352 | * @param string $name |
||
| 353 | * @param string|null $label |
||
| 354 | * @return Checkbox |
||
| 355 | */ |
||
| 356 | public function checkbox($name, $label = null) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $name |
||
| 366 | * @param null $text |
||
| 367 | * @return CheckboxList |
||
| 368 | */ |
||
| 369 | public function checkboxList($name, $text = null) { |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Render full form |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function render() { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * You can easy rewrite this method for custom design of your forms |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | protected function renderElements() { |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function renderStart() { |
||
| 456 | |||
| 457 | |||
| 458 | /** |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function renderEnd() { |
||
| 464 | } |