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 Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Container extends Element implements |
||
| 32 | DisableElementsCapableInterface, |
||
| 33 | FormParentInterface, |
||
| 34 | \IteratorAggregate, |
||
| 35 | \Countable |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Available/Loaded forms or specification. |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $forms = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Active formulars keys. |
||
| 45 | * |
||
| 46 | * Formulars which key is herein are included in the iterator. |
||
| 47 | * @see getIterator() |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $activeForms = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The form element manager. |
||
| 54 | * @var \Zend\Form\FormElementManager |
||
| 55 | */ |
||
| 56 | protected $formElementManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Entity to bind to the formulars. |
||
| 60 | * |
||
| 61 | * @var \Core\Entity\EntityInterface[] |
||
| 62 | */ |
||
| 63 | protected $entities; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Parameters to pass to the formulars. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $params = array(); |
||
| 71 | |||
| 72 | protected $parent; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param ServiceLocatorInterface $formElementManager |
||
| 76 | * @return Container |
||
| 77 | */ |
||
| 78 | public function setFormElementManager(ServiceLocatorInterface $formElementManager) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gets an iterator to iterate over the enabled formulars. |
||
| 86 | * |
||
| 87 | * @return \ArrayIterator |
||
| 88 | * @see IteratorAggregate::getIterator() |
||
| 89 | */ |
||
| 90 | public function getIterator() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Gets the count of enabled formulars |
||
| 107 | * |
||
| 108 | * @return int |
||
| 109 | * @see Countable::count() |
||
| 110 | */ |
||
| 111 | public function count() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param bool $flag |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setIsDisableCapable($flag) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isDisableCapable() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param bool $flag |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function setIsDisableElementsCapable($flag) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isDisableElementsCapable() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param array $map |
||
| 158 | */ |
||
| 159 | public function disableElements(array $map) |
||
| 188 | |||
| 189 | public function setOptions($options) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets formular parameters. |
||
| 202 | * |
||
| 203 | * @param array $params |
||
| 204 | * @return \Core\Form\Container |
||
| 205 | */ |
||
| 206 | public function setParams(array $params) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Gets the formular parameters. |
||
| 223 | * |
||
| 224 | * @return array: |
||
| 225 | */ |
||
| 226 | public function getParams() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Sets a formular parameter. |
||
| 233 | * |
||
| 234 | * @param string $key |
||
| 235 | * @param mixed $value |
||
| 236 | * @return \Core\Form\Container |
||
| 237 | */ |
||
| 238 | public function setParam($key, $value) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Gets the value of a formular parameter. |
||
| 255 | * |
||
| 256 | * Returns the provided <b>$default</b> value or null, if parameter does |
||
| 257 | * not exist. |
||
| 258 | * |
||
| 259 | * @param string $key |
||
| 260 | * @param mixed $default |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | public function getParam($key, $default = null) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets a specific formular. |
||
| 270 | * |
||
| 271 | * This formular will be created upon the first retrievement. |
||
| 272 | * If created, the formular gets passed the formular parameters set in this container. |
||
| 273 | * |
||
| 274 | * @param string $key |
||
| 275 | * @param bool $asInstance if set to false, the specification array is returned, and no instance created. |
||
| 276 | * |
||
| 277 | * @return null|\Core\Form\Container|\Zend\Form\FormInterface |
||
| 278 | * @since 0,25 added $asInstance parameter |
||
| 279 | */ |
||
| 280 | public function getForm($key, $asInstance = true) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Sets a form or form specification. |
||
| 359 | * |
||
| 360 | * if <b>$spec</b> is a string, it is used as form type, name is set to <b>$key</b> |
||
| 361 | * |
||
| 362 | * @param string $key |
||
| 363 | * @param string|array $spec |
||
| 364 | * @param boolean $enabled Should the formular be enabled or not |
||
| 365 | * |
||
| 366 | * @return self |
||
| 367 | */ |
||
| 368 | public function setForm($key, $spec, $enabled = true) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Sets formulars or specifications. |
||
| 400 | * |
||
| 401 | * <b>$forms</b> must be in the format: |
||
| 402 | * <pre> |
||
| 403 | * 'name' => [spec] |
||
| 404 | * </pre> |
||
| 405 | * |
||
| 406 | * <b>$spec</b> must be compatible with {@link setForm}. |
||
| 407 | * Additionally you can include a key 'enabled' in the spec, which will override |
||
| 408 | * <b>$enabled</b> only for the current formular. |
||
| 409 | * |
||
| 410 | * @param array $forms |
||
| 411 | * @param boolean $enabled |
||
| 412 | * @return \Core\Form\Container |
||
| 413 | */ |
||
| 414 | public function setForms(array $forms, $enabled = true) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Enables a formular. |
||
| 430 | * |
||
| 431 | * Enabled formulars are included in the {@link getIterator()} |
||
| 432 | * |
||
| 433 | * Traverses in child containers through .dot-Notation. |
||
| 434 | * |
||
| 435 | * @param string $key |
||
| 436 | * @return \Core\Form\Container |
||
| 437 | */ |
||
| 438 | public function enableForm($key = null) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Disables a formular. |
||
| 467 | * |
||
| 468 | * @param string $key |
||
| 469 | * |
||
| 470 | * @return self |
||
| 471 | */ |
||
| 472 | public function disableForm($key = null) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Sets the entity for formular binding. |
||
| 504 | * |
||
| 505 | * @param EntityInterface $entity |
||
| 506 | * @return self |
||
| 507 | */ |
||
| 508 | public function setEntity(EntityInterface $entity, $key='*') |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Gets the entity. |
||
| 523 | * |
||
| 524 | * @return \Core\Entity\EntityInterface |
||
| 525 | */ |
||
| 526 | public function getEntity($key='*') |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Maps entity property to forms or child containers. |
||
| 533 | * |
||
| 534 | * @param \Zend\Form\FormInterface $form |
||
| 535 | * @param \Core\Entity\EntityInterface $entity |
||
| 536 | * @param string $property |
||
| 537 | * @return void |
||
| 538 | */ |
||
| 539 | protected function mapEntity($form, $entity, $property) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Return isValid |
||
| 563 | * |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | public function isValid() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * if fieldsets there is get method to have access to any element by name |
||
| 578 | * this method is similar |
||
| 579 | * get('form') gets a form |
||
| 580 | * get('element') gets an element, if an element has the same name as a form, the form get's first access |
||
| 581 | * get('form.element') gets an element of a form, this is more efficent because it doesn't expand all forms in the container, |
||
| 582 | * but just the one adressed |
||
| 583 | * @param $key string |
||
| 584 | * @return null|\Zend\Form\ElementInterface |
||
| 585 | */ |
||
| 586 | public function get($key) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param $data |
||
| 629 | * @return $this |
||
| 630 | */ |
||
| 631 | public function setData($data) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param $parent |
||
| 658 | * @return $this |
||
| 659 | */ |
||
| 660 | public function setParent($parent) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @return mixed |
||
| 668 | */ |
||
| 669 | public function getParent() |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * @return bool |
||
| 677 | */ |
||
| 678 | public function hasParent() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param Renderer $renderer |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function renderPre(Renderer $renderer) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @param Renderer $renderer |
||
| 694 | * @return string |
||
| 695 | */ |
||
| 696 | public function renderPost(Renderer $renderer) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * get the actual active Form |
||
| 703 | * @param bool $setDefault |
||
| 704 | * @return mixed|null |
||
| 705 | */ |
||
| 706 | public function getActiveFormActual($setDefault = true) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * get the form before the actual active |
||
| 721 | * @return null |
||
| 722 | */ |
||
| 723 | View Code Duplication | public function getActiveFormPrevious() |
|
| 737 | |||
| 738 | |||
| 739 | /** |
||
| 740 | * Gets the form after the actual active |
||
| 741 | * @return null |
||
| 742 | */ |
||
| 743 | View Code Duplication | public function getActiveFormNext() |
|
| 757 | |||
| 758 | /** |
||
| 759 | * @param $key |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | public function getActionFor($key) |
||
| 779 | } |
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..