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 |
||
| 19 | class SelectElement extends RadioElement |
||
| 20 | { |
||
| 21 | /** @var string */ |
||
| 22 | protected $type = 'select'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well. |
||
| 26 | * |
||
| 27 | * @param boolean $getFulNodeName |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | 9 | public function getName($getFulNodeName = true) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Sets element value. |
||
| 46 | * |
||
| 47 | * @param mixed $value |
||
| 48 | * @return SelectElement |
||
| 49 | */ |
||
| 50 | 2 | public function setValue($value) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Returns element value. |
||
| 69 | * |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | 1 | View Code Duplication | public function getValue() |
| 86 | |||
| 87 | /** |
||
| 88 | * Set label-value options for the element. |
||
| 89 | * |
||
| 90 | * @param array $options |
||
| 91 | * @return RadioElement |
||
| 92 | */ |
||
| 93 | 9 | public function setOptions(array $options) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Sets label-value option for the element. |
||
| 111 | * |
||
| 112 | * @param string $label |
||
| 113 | * @param string $value |
||
| 114 | * @param boolean $checked |
||
| 115 | * @param string $group |
||
| 116 | * @param array $attributes |
||
| 117 | * @return SelectElement |
||
| 118 | */ |
||
| 119 | 9 | protected function setOption($label, $value, $checked, $group, array $attributes = []) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Checks if the Select box has groupped options. |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 1 | public function isGroupedSelect() |
|
| 147 | } |
||
| 148 |
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.