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|null |
||
| 29 | */ |
||
| 30 | protected $data = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $isSubmitted = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Element\BaseElement[] |
||
| 39 | */ |
||
| 40 | protected $elements = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Default form attributes |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $attributes = [ |
||
| 48 | 'method' => 'post', |
||
| 49 | ]; |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * @param $name |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | 8 | public function setName($name) { |
|
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * @return array |
||
| 65 | * @throws \Exception |
||
| 66 | */ |
||
| 67 | 1 | public function getData() { |
|
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * @param array|\Iterator $data |
||
| 78 | * @throws \Exception |
||
| 79 | */ |
||
| 80 | 14 | public function setData($data) { |
|
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 4 | public function getMethod() { |
|
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $method |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | 2 | public function setMethod($method) { |
|
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * |
||
| 139 | */ |
||
| 140 | 22 | protected function cleanValidationFlag() { |
|
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Check if form is submitted and all elements are valid |
||
| 147 | * |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | 9 | public function isValid() { |
|
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if form is submitted |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 11 | public function isSubmitted() { |
|
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Return unique id of form |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | 17 | public function getUid() { |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @return Element\BaseElement[] |
||
| 196 | */ |
||
| 197 | 4 | public function getElements() { |
|
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $name |
||
| 204 | * @param string|null $text |
||
| 205 | * @return \Fiv\Form\Element\Input |
||
| 206 | */ |
||
| 207 | 8 | public function input($name, $text = null) { |
|
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $name |
||
| 218 | * @param string|null $text |
||
| 219 | * @return \Fiv\Form\Element\Input |
||
| 220 | */ |
||
| 221 | public function password($name, $text = null) { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $name |
||
| 232 | * @param null $text |
||
| 233 | * @return Select |
||
| 234 | */ |
||
| 235 | public function select($name, $text = null) { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $name |
||
| 246 | * @param string $text |
||
| 247 | * @return RadioList |
||
| 248 | */ |
||
| 249 | public function radioList($name, $text = null) { |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $name |
||
| 260 | * @param null $text |
||
| 261 | * @return TextArea |
||
| 262 | */ |
||
| 263 | 4 | public function textarea($name, $text = null) { |
|
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * ``` |
||
| 274 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 275 | * ``` |
||
| 276 | * @param string $name |
||
| 277 | * @param null $value |
||
| 278 | * @return \Fiv\Form\Element\Input |
||
| 279 | */ |
||
| 280 | 3 | public function hidden($name, $value = null) { |
|
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * ``` |
||
| 292 | * $form->submit('register', 'зареєструватись'); |
||
| 293 | * ``` |
||
| 294 | * @param string $name |
||
| 295 | * @param null $value |
||
| 296 | * @return Submit |
||
| 297 | */ |
||
| 298 | 1 | public function submit($name, $value = null) { |
|
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * ``` |
||
| 309 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 310 | * ``` |
||
| 311 | * @param string $name |
||
| 312 | * @param string|null $label |
||
| 313 | * @return Checkbox |
||
| 314 | */ |
||
| 315 | 2 | public function checkbox($name, $label = null) { |
|
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $name |
||
| 326 | * @param null $text |
||
| 327 | * @return CheckboxList |
||
| 328 | */ |
||
| 329 | public function checkboxList($name, $text = null) { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * Attach element to this form. Overwrite element with same name |
||
| 340 | * |
||
| 341 | * @param Element\BaseElement $element |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | 2 | public function setElement(Element\BaseElement $element) { |
|
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * @param Element\BaseElement $element |
||
| 353 | * @return $this |
||
| 354 | * @throws \Exception |
||
| 355 | */ |
||
| 356 | 19 | public function addElement(Element\BaseElement $element) { |
|
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Render full form |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 2 | public function render() { |
|
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * You can easy rewrite this method for custom design of your forms |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | 2 | protected function renderElements() { |
|
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | 3 | public function renderStart() { |
|
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | 3 | public function renderEnd() { |
|
| 438 | |||
| 439 | } |
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: