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 |
||
| 17 | class SelectElement extends RadioElement |
||
| 18 | { |
||
| 19 | /** @var string */ |
||
| 20 | protected $type = 'select'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * SelectElement constructor. |
||
| 24 | * |
||
| 25 | * @param string $name |
||
| 26 | * @param string $label |
||
| 27 | * @param mixed $value |
||
| 28 | */ |
||
| 29 | 9 | public function __construct($name = '', $label = '', $value = null) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well. |
||
| 38 | * |
||
| 39 | * @param boolean $getFulNodeName |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | 9 | public function getName($getFulNodeName = true) |
|
| 43 | { |
||
| 44 | 9 | $name = parent::getName($getFulNodeName); |
|
| 45 | |||
| 46 | if ($getFulNodeName |
||
| 47 | 9 | && count($this->options) > 1 |
|
| 48 | 9 | && !empty($this->attributes['multiple']) |
|
| 49 | 9 | ) { |
|
| 50 | 1 | $name .= '[]'; |
|
| 51 | 1 | } |
|
| 52 | |||
| 53 | 9 | return $name; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Sets element value. |
||
| 58 | * |
||
| 59 | * @param mixed $value |
||
| 60 | * @return SelectElement |
||
| 61 | */ |
||
| 62 | 2 | public function setValue($value) |
|
| 63 | { |
||
| 64 | 2 | if (!is_array($value)) { |
|
| 65 | 1 | $value = [$value]; |
|
| 66 | 1 | } |
|
| 67 | |||
| 68 | 2 | $valuesToSelect = $this->getValuesToSelect($value); |
|
| 69 | |||
| 70 | 2 | foreach ($this->options as $group => $options) { |
|
| 71 | 2 | foreach ($options as $index => $option) { |
|
| 72 | 2 | $this->options[$group][$index]['checked'] = in_array($option['value'], $valuesToSelect); |
|
| 73 | 2 | } |
|
| 74 | 2 | } |
|
| 75 | |||
| 76 | 2 | return $this; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Returns element value. |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | 1 | View Code Duplication | public function getValue() |
| 98 | |||
| 99 | /** |
||
| 100 | * Sets label-value option for the element. |
||
| 101 | * |
||
| 102 | * @param string $label |
||
| 103 | * @param string $value |
||
| 104 | * @param boolean $checked |
||
| 105 | * @param string $group |
||
| 106 | * @return SelectElement |
||
| 107 | */ |
||
| 108 | 9 | protected function setOption($label, $value, $checked, $group) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Checks if the Select box has groupped options. |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 1 | public function isGroupedSelect() |
|
| 135 | } |
||
| 136 |
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.