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 RadioElement extends AbstractElement implements MultiOptionElementInterface |
||
| 20 | { |
||
| 21 | /** @var string */ |
||
| 22 | protected $type = 'radio'; |
||
| 23 | /** @var array */ |
||
| 24 | protected $options = []; |
||
| 25 | /** @var array */ |
||
| 26 | protected $optionGroups = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * RadioElement constructor. |
||
| 30 | * |
||
| 31 | * @param string $name |
||
| 32 | * @param string $label |
||
| 33 | * @param mixed $value |
||
| 34 | */ |
||
| 35 | 12 | public function __construct($name = '', $label = '', $value = null) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Sets element value. |
||
| 44 | * |
||
| 45 | * @param mixed $value |
||
| 46 | * @return RadioElement |
||
| 47 | */ |
||
| 48 | 2 | public function setValue($value) |
|
| 49 | { |
||
| 50 | 2 | if (!is_array($value)) { |
|
| 51 | 2 | $value = [$value]; |
|
| 52 | 2 | } |
|
| 53 | |||
| 54 | 2 | $valuesToSelect = $this->getValuesToSelect($value); |
|
| 55 | |||
| 56 | // Go through the options and change the defaults. |
||
| 57 | 2 | foreach ($this->options as &$option) { |
|
| 58 | 2 | $option['checked'] = in_array($option['value'], $valuesToSelect); |
|
| 59 | 2 | } |
|
| 60 | |||
| 61 | 2 | return $this; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Collects the selected values for multi option element. |
||
| 66 | * |
||
| 67 | * @param $value |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | 4 | protected function getValuesToSelect($value) |
|
| 71 | { |
||
| 72 | 4 | $isAssociativeArray = array_keys($value) !== range(0, count($value) - 1); |
|
| 73 | 4 | $valuesToSelect = []; |
|
| 74 | |||
| 75 | // Go through the given data and collect the selected ones. |
||
| 76 | 4 | foreach ($value as $key => $data) { |
|
| 77 | 4 | if ($isAssociativeArray && $data == 1) { |
|
| 78 | 1 | $valuesToSelect[] = $key; |
|
| 79 | 4 | } elseif (!$isAssociativeArray) { |
|
| 80 | 4 | $valuesToSelect[] = $data; |
|
| 81 | 4 | } |
|
| 82 | 4 | } |
|
| 83 | |||
| 84 | 4 | return $valuesToSelect; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns element value. |
||
| 89 | * |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | 2 | View Code Duplication | public function getValue() |
|
|
|||
| 93 | { |
||
| 94 | 2 | $selectedValues = []; |
|
| 95 | |||
| 96 | 2 | foreach ($this->options as $option) { |
|
| 97 | 2 | if ($option['checked']) { |
|
| 98 | 2 | $selectedValues[] = $option['value']; |
|
| 99 | 2 | } |
|
| 100 | 2 | } |
|
| 101 | |||
| 102 | 2 | return $selectedValues; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set label-value options for the element. |
||
| 107 | * |
||
| 108 | * @param array $options |
||
| 109 | * @return RadioElement |
||
| 110 | */ |
||
| 111 | 11 | public function setOptions(array $options) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Sets label-value option for the element. |
||
| 128 | * |
||
| 129 | * @param string $label |
||
| 130 | * @param string $value |
||
| 131 | * @param boolean $checked |
||
| 132 | * @param string $group |
||
| 133 | * @return RadioElement |
||
| 134 | */ |
||
| 135 | 2 | protected function setOption($label, $value, $checked, $group) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Checks if the element has value options. |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 1 | public function hasOptions() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Gets element value options. |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | 1 | public function getOptions() |
|
| 166 | } |
||
| 167 |
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.