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 |
||
| 17 | class Form extends Element\Html { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var null |
||
| 21 | */ |
||
| 22 | protected $uid = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var mixed |
||
| 26 | */ |
||
| 27 | protected $validResultCache = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array|null |
||
| 31 | */ |
||
| 32 | protected $data = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Element\Base[] |
||
| 36 | */ |
||
| 37 | protected $elements = []; |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * @param $name |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | 4 | public function setName($name) { |
|
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * @return array |
||
| 53 | * @throws \Exception |
||
| 54 | */ |
||
| 55 | 9 | public function getData() { |
|
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @param array|\Iterator $data |
||
| 66 | * @throws \Exception |
||
| 67 | */ |
||
| 68 | 9 | public function setData($data) { |
|
| 69 | 9 | if ($data instanceof \Iterator) { |
|
| 70 | $data = iterator_to_array($data); |
||
| 71 | } |
||
| 72 | |||
| 73 | 9 | if (!is_array($data)) { |
|
| 74 | 1 | throw new \Exception('Data should be an array'); |
|
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | 8 | $this->flushCacheFlags(); |
|
| 79 | |||
| 80 | 8 | foreach ($this->elements as $element) { |
|
| 81 | 7 | $name = $element->getName(); |
|
| 82 | |||
| 83 | 7 | if ($element instanceof Checkbox) { |
|
| 84 | $data[$name] = isset($data[$name]) ? 1 : 0; |
||
| 85 | 7 | } elseif ($element instanceof CheckboxList) { |
|
| 86 | $data[$name] = isset($data[$name]) ? $data[$name] : array(); |
||
| 87 | } |
||
| 88 | |||
| 89 | 7 | if (array_key_exists($name, $data)) { |
|
| 90 | 6 | $element->setValue($data[$name]); |
|
| 91 | 7 | $data[$name] = $element->getValue(); |
|
| 92 | } |
||
| 93 | |||
| 94 | } |
||
| 95 | |||
| 96 | 8 | $this->data = $data; |
|
| 97 | 8 | } |
|
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * @deprecated |
||
| 102 | * @todo remove method |
||
| 103 | */ |
||
| 104 | public function prepare() { |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 1 | public function getMethod() { |
|
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * |
||
| 123 | */ |
||
| 124 | 13 | protected function flushCacheFlags() { |
|
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * Check if form is submitted and all elements are valid |
||
| 131 | * |
||
| 132 | * @return bool|null |
||
| 133 | */ |
||
| 134 | 7 | public function isValid() { |
|
| 135 | 7 | if ($this->validResultCache !== null) { |
|
| 136 | return $this->validResultCache; |
||
| 137 | } |
||
| 138 | |||
| 139 | 7 | if (!$this->isSubmitted()) { |
|
| 140 | 1 | return false; |
|
| 141 | } |
||
| 142 | |||
| 143 | 6 | $this->validResultCache = true; |
|
| 144 | 6 | foreach ($this->elements as $element) { |
|
| 145 | 6 | if (!$element->validate()) { |
|
| 146 | 6 | $this->validResultCache = false; |
|
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | 6 | return $this->validResultCache; |
|
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Check if form is submitted |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | 9 | public function isSubmitted() { |
|
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Return unique id of form |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 9 | public function getUid() { |
|
| 172 | 9 | if (empty($this->uid)) { |
|
| 173 | 9 | $this->uid = md5(get_called_class()); |
|
|
|
|||
| 174 | } |
||
| 175 | |||
| 176 | 9 | return $this->uid; |
|
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * @return \Fiv\Form\Element\Base[] |
||
| 182 | */ |
||
| 183 | 1 | public function getElements() { |
|
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $name |
||
| 190 | * @param $text |
||
| 191 | * @return \Fiv\Form\Element\Input |
||
| 192 | */ |
||
| 193 | 6 | public function input($name, $text = null) { |
|
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $name |
||
| 205 | * @param $text |
||
| 206 | * @return \Fiv\Form\Element\Input |
||
| 207 | */ |
||
| 208 | public function password($name, $text = null) { |
||
| 209 | $input = new Element\Password(); |
||
| 210 | $input->setName($name); |
||
| 211 | $input->setText($text); |
||
| 212 | $this->setElement($input); |
||
| 213 | return $input; |
||
| 214 | } |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Connect element to block and to form |
||
| 219 | * |
||
| 220 | * @param Element\Base $element |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | 12 | protected function setElement(\Fiv\Form\Element\Base $element) { |
|
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @param $name |
||
| 232 | * @param null $text |
||
| 233 | * @return Select |
||
| 234 | */ |
||
| 235 | public function select($name, $text = null) { |
||
| 236 | $select = new Select(); |
||
| 237 | $select->setName($name); |
||
| 238 | $select->setText($text); |
||
| 239 | $this->setElement($select); |
||
| 240 | return $select; |
||
| 241 | } |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param $name |
||
| 246 | * @param string $text |
||
| 247 | * @return RadioList |
||
| 248 | */ |
||
| 249 | public function radioList($name, $text = null) { |
||
| 250 | $radio = new RadioList(); |
||
| 251 | $radio->setName($name); |
||
| 252 | $radio->setText($text); |
||
| 253 | $this->setElement($radio); |
||
| 254 | return $radio; |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $name |
||
| 260 | * @param null $text |
||
| 261 | * @return TextArea |
||
| 262 | */ |
||
| 263 | 3 | public function textarea($name, $text = null) { |
|
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * ``` |
||
| 274 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
| 275 | * ``` |
||
| 276 | * @param $name |
||
| 277 | * @param null $value |
||
| 278 | * @return \Fiv\Form\Element\Input |
||
| 279 | */ |
||
| 280 | 2 | public function hidden($name, $value = null) { |
|
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * ``` |
||
| 292 | * $form->submit('register', 'зареєструватись'); |
||
| 293 | * ``` |
||
| 294 | * @param $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 | 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 | * Render full form |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function render() { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * You can easy rewrite this method for custom design of your forms |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | protected function renderElements() { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function renderStart() { |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function renderEnd() { |
||
| 388 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..