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 |
||
| 15 | class Form extends Element\Html { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var null|string |
||
| 19 | */ |
||
| 20 | protected $uid = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var boolean|null |
||
| 24 | */ |
||
| 25 | protected $validationResult = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $errorList = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array|null |
||
| 34 | */ |
||
| 35 | protected $data = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $isSubmitted = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Element\BaseElement[] |
||
| 44 | */ |
||
| 45 | protected $elements = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Default form attributes |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $attributes = [ |
||
| 53 | 'method' => 'post', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * @param $name |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | 9 | public function setName($name) { |
|
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @param RequestContext $requestContext |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 1 | public function handleRequestContext(RequestContext $requestContext) { |
|
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @return array |
||
| 90 | * @throws \Exception |
||
| 91 | */ |
||
| 92 | 2 | public function getData() { |
|
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * @param array|\Iterator $data |
||
| 105 | * @throws \Exception |
||
| 106 | * @deprecated |
||
| 107 | */ |
||
| 108 | 18 | public function setData($data) { |
|
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 5 | public function getMethod() { |
|
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $method |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | 3 | public function setMethod($method) { |
|
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * |
||
| 168 | */ |
||
| 169 | 28 | protected function cleanValidationFlag() { |
|
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Check if form is submitted and all elements are valid |
||
| 177 | * |
||
| 178 | * @return boolean |
||
| 179 | */ |
||
| 180 | 12 | public function isValid() { |
|
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 2 | public function getErrors() { |
|
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $error |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 1 | protected function addError($error) { |
|
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if form is submitted |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 15 | public function isSubmitted() { |
|
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Return unique id of form |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 22 | public function getUid() { |
|
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * @return Element\BaseElement[] |
||
| 249 | */ |
||
| 250 | 8 | public function getElements() { |
|
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $name |
||
| 257 | * @return Element\BaseElement |
||
| 258 | * @throws \InvalidArgumentException |
||
| 259 | */ |
||
| 260 | 3 | public function getElement($name) { |
|
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $name |
||
| 270 | * @param string|null $text |
||
| 271 | * @return \Fiv\Form\Element\Input |
||
| 272 | */ |
||
| 273 | 13 | public function input($name, $text = null) { |
|
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $name |
||
| 284 | * @param string|null $text |
||
| 285 | * @return \Fiv\Form\Element\Input |
||
| 286 | */ |
||
| 287 | public function password($name, $text = null) { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $name |
||
| 298 | * @param null $text |
||
| 299 | * @return Select |
||
| 300 | */ |
||
| 301 | public function select($name, $text = null) { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $name |
||
| 312 | * @param string $text |
||
| 313 | * @return RadioList |
||
| 314 | */ |
||
| 315 | public function radioList($name, $text = null) { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $name |
||
| 326 | * @param null $text |
||
| 327 | * @return TextArea |
||
| 328 | */ |
||
| 329 | 5 | public function textarea($name, $text = null) { |
|
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * ``` |
||
| 340 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 341 | * ``` |
||
| 342 | * @param string $name |
||
| 343 | * @param null $value |
||
| 344 | * @return \Fiv\Form\Element\Input |
||
| 345 | */ |
||
| 346 | 3 | public function hidden($name, $value = null) { |
|
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * ``` |
||
| 358 | * $form->submit('register', 'зареєструватись'); |
||
| 359 | * ``` |
||
| 360 | * @param string $name |
||
| 361 | * @param null $value |
||
| 362 | * @return Submit |
||
| 363 | */ |
||
| 364 | 1 | public function submit($name, $value = null) { |
|
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * ``` |
||
| 375 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 376 | * ``` |
||
| 377 | * @param string $name |
||
| 378 | * @param string|null $label |
||
| 379 | * @return Checkbox |
||
| 380 | */ |
||
| 381 | 2 | public function checkbox($name, $label = null) { |
|
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $name |
||
| 392 | * @param null $text |
||
| 393 | * @return CheckboxList |
||
| 394 | */ |
||
| 395 | public function checkboxList($name, $text = null) { |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * Attach element to this form. Overwrite element with same name |
||
| 406 | * |
||
| 407 | * @param Element\BaseElement $element |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | 2 | public function setElement(Element\BaseElement $element) { |
|
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * @param Element\BaseElement $element |
||
| 419 | * @return $this |
||
| 420 | * @throws \Exception |
||
| 421 | */ |
||
| 422 | 25 | public function addElement(Element\BaseElement $element) { |
|
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * Render full form |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | 2 | public function render() { |
|
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * You can easy rewrite this method for custom design of your forms |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | 2 | protected function renderElements() { |
|
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 3 | public function renderStart() { |
|
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | 3 | public function renderEnd() { |
|
| 504 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: