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() { | 
            |
| 99 | |||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * @param array|\Iterator $data  | 
            ||
| 103 | * @throws \Exception  | 
            ||
| 104 | * @deprecated  | 
            ||
| 105 | */  | 
            ||
| 106 | 18 |     public function setData($data) { | 
            |
| 139 | |||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * @return string  | 
            ||
| 143 | */  | 
            ||
| 144 | 5 |     public function getMethod() { | 
            |
| 151 | |||
| 152 | |||
| 153 | /**  | 
            ||
| 154 | * @param string $method  | 
            ||
| 155 | * @return $this  | 
            ||
| 156 | */  | 
            ||
| 157 | 3 |     public function setMethod($method) { | 
            |
| 162 | |||
| 163 | |||
| 164 | /**  | 
            ||
| 165 | *  | 
            ||
| 166 | */  | 
            ||
| 167 | 28 |     protected function cleanValidationFlag() { | 
            |
| 171 | |||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * Check if form is submitted and all elements are valid  | 
            ||
| 175 | *  | 
            ||
| 176 | * @return boolean  | 
            ||
| 177 | */  | 
            ||
| 178 | 12 |     public function isValid() { | 
            |
| 197 | |||
| 198 | |||
| 199 | /**  | 
            ||
| 200 | * @return array  | 
            ||
| 201 | */  | 
            ||
| 202 | 2 |     public function getErrors() { | 
            |
| 205 | |||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * @param string $error  | 
            ||
| 209 | * @return $this  | 
            ||
| 210 | */  | 
            ||
| 211 | 1 |     protected function addError($error) { | 
            |
| 219 | |||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Check if form is submitted  | 
            ||
| 223 | *  | 
            ||
| 224 | * @return bool  | 
            ||
| 225 | */  | 
            ||
| 226 | 15 |     public function isSubmitted() { | 
            |
| 229 | |||
| 230 | |||
| 231 | /**  | 
            ||
| 232 | * Return unique id of form  | 
            ||
| 233 | *  | 
            ||
| 234 | * @return string  | 
            ||
| 235 | */  | 
            ||
| 236 | 22 |     public function getUid() { | 
            |
| 243 | |||
| 244 | |||
| 245 | /**  | 
            ||
| 246 | * @return Element\BaseElement[]  | 
            ||
| 247 | */  | 
            ||
| 248 | 8 |     public function getElements() { | 
            |
| 251 | |||
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * @param string $name  | 
            ||
| 255 | * @return Element\BaseElement  | 
            ||
| 256 | * @throws \InvalidArgumentException  | 
            ||
| 257 | */  | 
            ||
| 258 | 3 |     public function getElement($name) { | 
            |
| 264 | |||
| 265 | |||
| 266 | /**  | 
            ||
| 267 | * @param string $name  | 
            ||
| 268 | * @param string|null $text  | 
            ||
| 269 | * @return \Fiv\Form\Element\Input  | 
            ||
| 270 | */  | 
            ||
| 271 | 13 |     public function input($name, $text = null) { | 
            |
| 278 | |||
| 279 | |||
| 280 | /**  | 
            ||
| 281 | * @param string $name  | 
            ||
| 282 | * @param string|null $text  | 
            ||
| 283 | * @return \Fiv\Form\Element\Input  | 
            ||
| 284 | */  | 
            ||
| 285 |     public function password($name, $text = null) { | 
            ||
| 292 | |||
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * @param string $name  | 
            ||
| 296 | * @param null $text  | 
            ||
| 297 | * @return Select  | 
            ||
| 298 | */  | 
            ||
| 299 |     public function select($name, $text = null) { | 
            ||
| 306 | |||
| 307 | |||
| 308 | /**  | 
            ||
| 309 | * @param string $name  | 
            ||
| 310 | * @param string $text  | 
            ||
| 311 | * @return RadioList  | 
            ||
| 312 | */  | 
            ||
| 313 |     public function radioList($name, $text = null) { | 
            ||
| 320 | |||
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * @param string $name  | 
            ||
| 324 | * @param null $text  | 
            ||
| 325 | * @return TextArea  | 
            ||
| 326 | */  | 
            ||
| 327 | 5 |     public function textarea($name, $text = null) { | 
            |
| 334 | |||
| 335 | |||
| 336 | /**  | 
            ||
| 337 | * ```  | 
            ||
| 338 |      * $form->hidden('key', md5($this-user->id . HASH_A); | 
            ||
| 339 | * ```  | 
            ||
| 340 | * @param string $name  | 
            ||
| 341 | * @param null $value  | 
            ||
| 342 | * @return \Fiv\Form\Element\Input  | 
            ||
| 343 | */  | 
            ||
| 344 | 3 |     public function hidden($name, $value = null) { | 
            |
| 352 | |||
| 353 | |||
| 354 | /**  | 
            ||
| 355 | * ```  | 
            ||
| 356 |      * $form->submit('register', 'зареєструватись'); | 
            ||
| 357 | * ```  | 
            ||
| 358 | * @param string $name  | 
            ||
| 359 | * @param null $value  | 
            ||
| 360 | * @return Submit  | 
            ||
| 361 | */  | 
            ||
| 362 | 1 |     public function submit($name, $value = null) { | 
            |
| 369 | |||
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * ```  | 
            ||
| 373 |      * $form->checkbox('subscribe', 'Підписка на новини'); | 
            ||
| 374 | * ```  | 
            ||
| 375 | * @param string $name  | 
            ||
| 376 | * @param string|null $label  | 
            ||
| 377 | * @return Checkbox  | 
            ||
| 378 | */  | 
            ||
| 379 | 2 |     public function checkbox($name, $label = null) { | 
            |
| 386 | |||
| 387 | |||
| 388 | /**  | 
            ||
| 389 | * @param string $name  | 
            ||
| 390 | * @param null $text  | 
            ||
| 391 | * @return CheckboxList  | 
            ||
| 392 | */  | 
            ||
| 393 |     public function checkboxList($name, $text = null) { | 
            ||
| 400 | |||
| 401 | |||
| 402 | /**  | 
            ||
| 403 | * Attach element to this form. Overwrite element with same name  | 
            ||
| 404 | *  | 
            ||
| 405 | * @param Element\BaseElement $element  | 
            ||
| 406 | * @return $this  | 
            ||
| 407 | */  | 
            ||
| 408 | 2 |     public function setElement(Element\BaseElement $element) { | 
            |
| 413 | |||
| 414 | |||
| 415 | /**  | 
            ||
| 416 | * @param Element\BaseElement $element  | 
            ||
| 417 | * @return $this  | 
            ||
| 418 | * @throws \Exception  | 
            ||
| 419 | */  | 
            ||
| 420 | 25 |     public function addElement(Element\BaseElement $element) { | 
            |
| 429 | |||
| 430 | |||
| 431 | /**  | 
            ||
| 432 | * Render full form  | 
            ||
| 433 | *  | 
            ||
| 434 | * @return string  | 
            ||
| 435 | */  | 
            ||
| 436 | 2 |     public function render() { | 
            |
| 439 | |||
| 440 | |||
| 441 | /**  | 
            ||
| 442 | * You can easy rewrite this method for custom design of your forms  | 
            ||
| 443 | *  | 
            ||
| 444 | * @return string  | 
            ||
| 445 | */  | 
            ||
| 446 | 2 |     protected function renderElements() { | 
            |
| 463 | |||
| 464 | |||
| 465 | /**  | 
            ||
| 466 | * @return string  | 
            ||
| 467 | */  | 
            ||
| 468 | 3 |     public function renderStart() { | 
            |
| 494 | |||
| 495 | |||
| 496 | /**  | 
            ||
| 497 | * @return string  | 
            ||
| 498 | */  | 
            ||
| 499 | 3 |     public function renderEnd() { | 
            |
| 502 | }  | 
            
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: