Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractForm 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 AbstractForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class AbstractForm |
||
| 23 | { |
||
| 24 | use MagicMethodElementsFormTrait; |
||
| 25 | use HasElementsTrait; |
||
| 26 | use NewElementsMethods; |
||
| 27 | use HasDisplayGroupsTrait; |
||
| 28 | use MessagesTrait; |
||
| 29 | use HasErrorsTrait; |
||
| 30 | use HasAttributesTrait; |
||
| 31 | use HasClassTrait; |
||
| 32 | |||
| 33 | const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
||
| 34 | const ENCTYPE_MULTIPART = 'multipart/form-data'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $methods = ['delete', 'get', 'post', 'put']; |
||
| 40 | |||
| 41 | protected $_options = []; |
||
| 42 | protected $_buttons; |
||
| 43 | |||
| 44 | protected $_decorators = []; |
||
| 45 | protected $_renderer; |
||
| 46 | protected $_cache; |
||
| 47 | |||
| 48 | protected $controllerView = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * AbstractForm constructor. |
||
| 52 | */ |
||
| 53 | 11 | public function __construct() |
|
| 58 | |||
| 59 | 11 | public function init() |
|
| 63 | |||
| 64 | 11 | protected function initAction() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $action |
||
| 73 | * @return AbstractForm |
||
| 74 | */ |
||
| 75 | public function setAction($action) |
||
| 79 | |||
| 80 | 11 | public function postInit() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @param $name |
||
| 86 | * @return ElementAbstract|null |
||
| 87 | */ |
||
| 88 | 1 | public function __get($name) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param $name |
||
| 100 | * @param bool $label |
||
| 101 | * @param string $type |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | public function addButton($name, $label = false, $type = 'button') |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param $name |
||
| 113 | * @param bool $label |
||
| 114 | * @param string $type |
||
| 115 | * @return ButtonAbstract |
||
| 116 | */ |
||
| 117 | protected function newButton($name, $label = false, $type = 'button') |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param $name |
||
| 130 | * @return ElementAbstract |
||
| 131 | */ |
||
| 132 | public function getButton($name) |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * @return ButtonAbstract[] |
||
| 144 | */ |
||
| 145 | public function getButtons() |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * @param $key |
||
| 153 | * @param $value |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function setOption($key, $value) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $key |
||
| 166 | * @return mixed|null |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function getOption($key) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param $method |
||
| 180 | * @return AbstractForm |
||
| 181 | */ |
||
| 182 | public function setMethod($method) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function execute() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function submited() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function processRequest() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public function validate() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param $request |
||
| 245 | */ |
||
| 246 | protected function getDataFromRequest($request) |
||
| 272 | |||
| 273 | public function processValidation() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isValid() |
||
| 290 | |||
| 291 | public function process() |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * @param $type |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | 3 | public function setRendererType($type) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $class |
||
| 309 | */ |
||
| 310 | protected function setRendererClass($class) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param AbstractRenderer $renderer |
||
| 320 | */ |
||
| 321 | 3 | public function setRenderer($renderer) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $type |
||
| 328 | * @return AbstractRenderer |
||
| 329 | */ |
||
| 330 | 3 | public function getNewRenderer($type = 'basic') |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param $key |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | 1 | public function getCache($key) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $key |
||
| 351 | * @param $value |
||
| 352 | */ |
||
| 353 | 1 | public function setCache($key, $value) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @param $key |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | public function isCache($key) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function getName() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return null|string |
||
| 377 | */ |
||
| 378 | public function __toString() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function render() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return AbstractRenderer |
||
| 399 | */ |
||
| 400 | 3 | public function getRenderer() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @return View|null |
||
| 411 | */ |
||
| 412 | public function getControllerView() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | protected function getData() |
||
| 436 | } |
||
| 437 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: