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 |
||
| 33 | class Container extends Element implements |
||
| 34 | DisableElementsCapableInterface, |
||
| 35 | ServiceLocatorAwareInterface, |
||
| 36 | FormParentInterface, |
||
| 37 | \IteratorAggregate, |
||
| 38 | \Countable |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Available/Loaded forms or specification. |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $forms = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Active formulars keys. |
||
| 48 | * |
||
| 49 | * Formulars which key is herein are included in the iterator. |
||
| 50 | * @see getIterator() |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $activeForms = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The form element manager. |
||
| 57 | * @var \Zend\Form\FormElementManager |
||
| 58 | */ |
||
| 59 | protected $formElementManager; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Entity to bind to the formulars. |
||
| 63 | * |
||
| 64 | * @var \Core\Entity\EntityInterface[] |
||
| 65 | */ |
||
| 66 | protected $entities; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Parameters to pass to the formulars. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $params = array(); |
||
| 74 | |||
| 75 | protected $parent; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritDoc} |
||
| 79 | * @return Container |
||
| 80 | * @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator() |
||
| 81 | */ |
||
| 82 | public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets the FormElementManager |
||
| 90 | * |
||
| 91 | * @return \Zend\Form\FormElementManager |
||
| 92 | */ |
||
| 93 | public function getServiceLocator() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Gets an iterator to iterate over the enabled formulars. |
||
| 100 | * |
||
| 101 | * @return \ArrayIterator |
||
| 102 | * @see IteratorAggregate::getIterator() |
||
| 103 | */ |
||
| 104 | public function getIterator() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Gets the count of enabled formulars |
||
| 121 | * |
||
| 122 | * @return int |
||
| 123 | * @see Countable::count() |
||
| 124 | */ |
||
| 125 | public function count() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param bool $flag |
||
| 132 | * @return $this |
||
| 133 | */ |
||
| 134 | public function setIsDisableCapable($flag) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function isDisableCapable() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param bool $flag |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function setIsDisableElementsCapable($flag) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function isDisableElementsCapable() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param array $map |
||
| 172 | */ |
||
| 173 | public function disableElements(array $map) |
||
| 202 | |||
| 203 | public function setOptions($options) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Sets formular parameters. |
||
| 216 | * |
||
| 217 | * @param array $params |
||
| 218 | * @return \Core\Form\Container |
||
| 219 | */ |
||
| 220 | public function setParams(array $params) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Gets the formular parameters. |
||
| 237 | * |
||
| 238 | * @return array: |
||
| 239 | */ |
||
| 240 | public function getParams() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Sets a formular parameter. |
||
| 247 | * |
||
| 248 | * @param string $key |
||
| 249 | * @param mixed $value |
||
| 250 | * @return \Core\Form\Container |
||
| 251 | */ |
||
| 252 | public function setParam($key, $value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Gets the value of a formular parameter. |
||
| 269 | * |
||
| 270 | * Returns the provided <b>$default</b> value or null, if parameter does |
||
| 271 | * not exist. |
||
| 272 | * |
||
| 273 | * @param string $key |
||
| 274 | * @param mixed $default |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function getParam($key, $default = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets a specific formular. |
||
| 284 | * |
||
| 285 | * This formular will be created upon the first retrievement. |
||
| 286 | * If created, the formular gets passed the formular parameters set in this container. |
||
| 287 | * |
||
| 288 | * @param string $key |
||
| 289 | * @param bool $asInstance if set to false, the specification array is returned, and no instance created. |
||
| 290 | * |
||
| 291 | * @return null|\Core\Form\Container|\Zend\Form\FormInterface |
||
| 292 | * @since 0,25 added $asInstance parameter |
||
| 293 | */ |
||
| 294 | public function getForm($key, $asInstance = true) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Sets a form or form specification. |
||
| 373 | * |
||
| 374 | * if <b>$spec</b> is a string, it is used as form type, name is set to <b>$key</b> |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * @param string|array $spec |
||
| 378 | * @param boolean $enabled Should the formular be enabled or not |
||
| 379 | * |
||
| 380 | * @return self |
||
| 381 | */ |
||
| 382 | public function setForm($key, $spec, $enabled = true) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Sets formulars or specifications. |
||
| 408 | * |
||
| 409 | * <b>$forms</b> must be in the format: |
||
| 410 | * <pre> |
||
| 411 | * 'name' => [spec] |
||
| 412 | * </pre> |
||
| 413 | * |
||
| 414 | * <b>$spec</b> must be compatible with {@link setForm}. |
||
| 415 | * Additionally you can include a key 'enabled' in the spec, which will override |
||
| 416 | * <b>$enabled</b> only for the current formular. |
||
| 417 | * |
||
| 418 | * @param array $forms |
||
| 419 | * @param boolean $enabled |
||
| 420 | * @return \Core\Form\Container |
||
| 421 | */ |
||
| 422 | public function setForms(array $forms, $enabled = true) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Enables a formular. |
||
| 438 | * |
||
| 439 | * Enabled formulars are included in the {@link getIterator()} |
||
| 440 | * |
||
| 441 | * Traverses in child containers through .dot-Notation. |
||
| 442 | * |
||
| 443 | * @param string $key |
||
| 444 | * @return \Core\Form\Container |
||
| 445 | */ |
||
| 446 | public function enableForm($key = null) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Disables a formular. |
||
| 475 | * |
||
| 476 | * @param string $key |
||
| 477 | * |
||
| 478 | * @return self |
||
| 479 | */ |
||
| 480 | public function disableForm($key = null) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Sets the entity for formular binding. |
||
| 512 | * |
||
| 513 | * @param EntityInterface $entity |
||
| 514 | * @return self |
||
| 515 | */ |
||
| 516 | public function setEntity(EntityInterface $entity, $key='*') |
||
| 527 | |||
| 528 | |||
| 529 | /** |
||
| 530 | * Gets the entity. |
||
| 531 | * |
||
| 532 | * @return \Core\Entity\EntityInterface |
||
| 533 | */ |
||
| 534 | public function getEntity($key='*') |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Maps entity property to forms or child containers. |
||
| 541 | * |
||
| 542 | * @param \Zend\Form\FormInterface $form |
||
| 543 | * @param \Core\Entity\EntityInterface $entity |
||
| 544 | * @param string $property |
||
| 545 | * @return void |
||
| 546 | */ |
||
| 547 | protected function mapEntity($form, $entity, $property) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Return isValid |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | public function isValid() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * if fieldsets there is get method to have access to any element by name |
||
| 586 | * this method is similar |
||
| 587 | * get('form') gets a form |
||
| 588 | * get('element') gets an element, if an element has the same name as a form, the form get's first access |
||
| 589 | * get('form.element') gets an element of a form, this is more efficent because it doesn't expand all forms in the container, |
||
| 590 | * but just the one adressed |
||
| 591 | * @param $key string |
||
| 592 | * @return null|\Zend\Form\ElementInterface |
||
| 593 | */ |
||
| 594 | public function get($key) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param $data |
||
| 637 | * @return $this |
||
| 638 | */ |
||
| 639 | public function setData($data) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param $parent |
||
| 666 | * @return $this |
||
| 667 | */ |
||
| 668 | public function setParent($parent) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @return mixed |
||
| 676 | */ |
||
| 677 | public function getParent() |
||
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * @return bool |
||
| 685 | */ |
||
| 686 | public function hasParent() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @param Renderer $renderer |
||
| 693 | * @return string |
||
| 694 | */ |
||
| 695 | public function renderPre(Renderer $renderer) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @param Renderer $renderer |
||
| 702 | * @return string |
||
| 703 | */ |
||
| 704 | public function renderPost(Renderer $renderer) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * get the actual active Form |
||
| 711 | * @param bool $setDefault |
||
| 712 | * @return mixed|null |
||
| 713 | */ |
||
| 714 | public function getActiveFormActual($setDefault = true) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * get the form before the actual active |
||
| 729 | * @return null |
||
| 730 | */ |
||
| 731 | View Code Duplication | public function getActiveFormPrevious() |
|
| 745 | |||
| 746 | |||
| 747 | /** |
||
| 748 | * Gets the form after the actual active |
||
| 749 | * @return null |
||
| 750 | */ |
||
| 751 | View Code Duplication | public function getActiveFormNext() |
|
| 765 | |||
| 766 | /** |
||
| 767 | * @param $key |
||
| 768 | * @return string |
||
| 769 | */ |
||
| 770 | public function getActionFor($key) |
||
| 787 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.