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 Element\BaseElement[] |
||
| 34 | */ |
||
| 35 | protected $elements = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Default form attributes |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $attributes = [ |
||
| 43 | 'method' => 'post', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @param $name |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | 5 | public function setName($name) { |
|
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * @return array |
||
| 60 | * @throws \Exception |
||
| 61 | */ |
||
| 62 | 10 | public function getData() { |
|
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * @param array|\Iterator $data |
||
| 73 | * @throws \Exception |
||
| 74 | */ |
||
| 75 | 10 | public function setData($data) { |
|
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 2 | public function getMethod() { |
|
| 111 | 2 | if (!empty($this->attributes['method'])) { |
|
| 112 | 2 | return strtolower($this->attributes['method']); |
|
| 113 | } |
||
| 114 | |||
| 115 | 1 | return null; |
|
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $method |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | 2 | public function setMethod($method) { |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * |
||
| 132 | */ |
||
| 133 | 16 | protected function cleanValidationFlag() { |
|
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Check if form is submitted and all elements are valid |
||
| 140 | * |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | 8 | public function isValid() { |
|
| 144 | 8 | if ($this->validationResult !== null) { |
|
| 145 | 1 | return $this->validationResult; |
|
| 146 | } |
||
| 147 | |||
| 148 | 8 | if (!$this->isSubmitted()) { |
|
| 149 | 1 | return false; |
|
| 150 | } |
||
| 151 | |||
| 152 | 7 | $this->validationResult = true; |
|
| 153 | 7 | foreach ($this->elements as $element) { |
|
| 154 | 7 | if (!$element->validate()) { |
|
| 155 | 7 | $this->validationResult = false; |
|
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | 7 | return $this->validationResult; |
|
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Check if form is submitted |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 10 | public function isSubmitted() { |
|
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Return unique id of form |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 11 | public function getUid() { |
|
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * @return Element\BaseElement[] |
||
| 191 | */ |
||
| 192 | 3 | public function getElements() { |
|
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $name |
||
| 199 | * @param string|null $text |
||
| 200 | * @return \Fiv\Form\Element\Input |
||
| 201 | */ |
||
| 202 | 6 | public function input($name, $text = null) { |
|
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $name |
||
| 213 | * @param string|null $text |
||
| 214 | * @return \Fiv\Form\Element\Input |
||
| 215 | */ |
||
| 216 | public function password($name, $text = null) { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $name |
||
| 227 | * @param null $text |
||
| 228 | * @return Select |
||
| 229 | */ |
||
| 230 | public function select($name, $text = null) { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $name |
||
| 241 | * @param string $text |
||
| 242 | * @return RadioList |
||
| 243 | */ |
||
| 244 | public function radioList($name, $text = null) { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $name |
||
| 255 | * @param null $text |
||
| 256 | * @return TextArea |
||
| 257 | */ |
||
| 258 | 3 | public function textarea($name, $text = null) { |
|
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * ``` |
||
| 269 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 270 | * ``` |
||
| 271 | * @param string $name |
||
| 272 | * @param null $value |
||
| 273 | * @return \Fiv\Form\Element\Input |
||
| 274 | */ |
||
| 275 | 2 | public function hidden($name, $value = null) { |
|
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * ``` |
||
| 287 | * $form->submit('register', 'зареєструватись'); |
||
| 288 | * ``` |
||
| 289 | * @param string $name |
||
| 290 | * @param null $value |
||
| 291 | * @return Submit |
||
| 292 | */ |
||
| 293 | 1 | public function submit($name, $value = null) { |
|
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * ``` |
||
| 304 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
| 305 | * ``` |
||
| 306 | * @param string $name |
||
| 307 | * @param string|null $label |
||
| 308 | * @return Checkbox |
||
| 309 | */ |
||
| 310 | public function checkbox($name, $label = null) { |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $name |
||
| 321 | * @param null $text |
||
| 322 | * @return CheckboxList |
||
| 323 | */ |
||
| 324 | public function checkboxList($name, $text = null) { |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Attach element to this form. Overwrite element with same name |
||
| 335 | * |
||
| 336 | * @param Element\BaseElement $element |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | 2 | public function setElement(Element\BaseElement $element) { |
|
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * @param Element\BaseElement $element |
||
| 348 | * @return $this |
||
| 349 | * @throws \Exception |
||
| 350 | */ |
||
| 351 | 13 | public function addElement(Element\BaseElement $element) { |
|
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Render full form |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | 1 | public function render() { |
|
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * You can easy rewrite this method for custom design of your forms |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 1 | protected function renderElements() { |
|
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | 1 | public function renderStart() { |
|
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | 1 | public function renderEnd() { |
|
| 417 | |||
| 418 | } |