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:
| 1 | <?php |
||
| 25 | class MultiGroupWidget extends AdminWidget implements |
||
| 26 | FormInterface, |
||
| 27 | LayoutAwareInterface, |
||
| 28 | ObjectContainerInterface |
||
| 29 | { |
||
| 30 | use FormTrait; |
||
| 31 | use LayoutAwareTrait; |
||
| 32 | use ObjectContainerTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The cache of snake-cased words. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected static $snakeCache = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The cache of camel-cased words. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected static $camelCache = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $controllerIdent; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var FactoryInterface |
||
| 55 | */ |
||
| 56 | protected $widgetFactory; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var boolean |
||
| 60 | */ |
||
| 61 | protected $isMergingData; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array|mixed |
||
| 65 | */ |
||
| 66 | protected $formGroups; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var FormWidget |
||
| 70 | */ |
||
| 71 | protected $form; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Comparison function used by {@see uasort()}. |
||
| 75 | * |
||
| 76 | * @param PrioritizableInterface $a Sortable entity A. |
||
| 77 | * @param PrioritizableInterface $b Sortable entity B. |
||
| 78 | * @return integer Sorting value: -1 or 1. |
||
| 79 | */ |
||
| 80 | View Code Duplication | protected function sortItemsByPriority( |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param Container $container The DI container. |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | protected function setDependencies(Container $container) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function defaultGroupType() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set the object's form groups. |
||
| 118 | * |
||
| 119 | * @param array $groups A collection of group structures. |
||
| 120 | * @return FormInterface Chainable |
||
| 121 | */ |
||
| 122 | public function setGroups(array $groups) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set an widget factory. |
||
| 149 | * |
||
| 150 | * @param FactoryInterface $factory The factory to create widgets. |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | protected function setWidgetFactory(FactoryInterface $factory) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Retrieve the widget factory. |
||
| 162 | * |
||
| 163 | * @throws RuntimeException If the widget factory was not previously set. |
||
| 164 | * @return FactoryInterface |
||
| 165 | */ |
||
| 166 | public function widgetFactory() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return FormWidget |
||
| 180 | */ |
||
| 181 | public function form() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param FormInterface $form Form for MultiGroupWidget. |
||
| 188 | * @return self |
||
| 189 | */ |
||
| 190 | public function setForm(FormInterface $form) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | public function formGroups() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param mixed $formGroups FormGroups for MultiGroupWidget. |
||
| 207 | * @return self |
||
| 208 | */ |
||
| 209 | public function setFormGroups($formGroups) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * So that the formTrait can access the current From widget. |
||
| 218 | * |
||
| 219 | * @return FormWidget |
||
| 220 | */ |
||
| 221 | protected function formWidget() |
||
| 225 | } |
||
| 226 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.