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 |
||
7 | class Checkboxes extends Field { |
||
8 | |||
9 | protected $default = []; |
||
10 | protected $options = []; |
||
11 | protected $values = []; |
||
12 | |||
13 | 11 | public function setOptions($options) |
|
22 | |||
23 | 7 | View Code Duplication | public function setChecked($keys = []) |
37 | |||
38 | 3 | View Code Duplication | public function setUnchecked($keys = []) |
52 | |||
53 | 9 | public function getValue() |
|
54 | { |
||
55 | 9 | $values = []; |
|
56 | 9 | View Code Duplication | foreach (array_keys($this->options) as $key) { |
57 | 9 | $values[$this->name.'_'.$key] = $this->values[$key]; |
|
58 | } |
||
59 | 9 | return $values; |
|
60 | } |
||
61 | |||
62 | 11 | public function getButtonName() |
|
66 | |||
67 | 7 | public function renderWith() |
|
83 | |||
84 | 11 | public function setValueFromDefault() |
|
90 | |||
91 | 1 | public function setValueFromModel($model) |
|
98 | |||
99 | public function setValueFromRequest($request) |
||
105 | |||
106 | 1 | public function fillModelWithValue($model) |
|
107 | 1 | { |
|
108 | View Code Duplication | foreach (array_keys($this->options) as $key) { |
|
109 | 1 | $model->{$this->name.'_'.$key} = $this->values[$key] ? 1 : 0; |
|
110 | } |
||
111 | 1 | } |
|
112 | |||
113 | 1 | public function validateRequired() |
|
123 | |||
124 | } |
||
125 |
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.