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 | public $default = []; |
||
10 | public $options = []; |
||
11 | public $values = []; |
||
12 | |||
13 | 11 | public function setOptions($options) |
|
22 | |||
23 | 7 | View Code Duplication | public function setChecked($keys = []) |
|
|||
24 | { |
||
25 | 7 | if (count($keys)) { |
|
26 | 3 | foreach ($keys as $key) { |
|
27 | 3 | $this->default[$key] = true; |
|
28 | } |
||
29 | } else { |
||
30 | 4 | $this->defaults = []; |
|
31 | 4 | foreach (array_keys($this->options) as $key) { |
|
32 | 4 | $this->default[$key] = true; |
|
33 | } |
||
34 | } |
||
35 | 7 | return $this; |
|
36 | } |
||
37 | |||
38 | 3 | View Code Duplication | public function setUnchecked($keys = []) |
39 | { |
||
40 | 3 | if (count($keys)) { |
|
41 | 1 | foreach ($keys as $key) { |
|
42 | 1 | $this->default[$key] = false; |
|
43 | } |
||
44 | } else { |
||
45 | 2 | $this->defaults = []; |
|
46 | 2 | foreach (array_keys($this->options) as $key) { |
|
47 | 2 | $this->default[$key] = false; |
|
48 | } |
||
49 | } |
||
50 | 3 | return $this; |
|
51 | } |
||
52 | |||
53 | 9 | public function getValue() |
|
54 | { |
||
55 | 9 | $values = []; |
|
56 | 9 | 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() |
|
63 | { |
||
64 | 11 | // |
|
65 | } |
||
66 | |||
67 | 7 | public function renderWith() |
|
68 | { |
||
69 | 7 | $properties = []; |
|
70 | |||
71 | 7 | $options = []; |
|
72 | 7 | foreach ($this->options as $name => $label) { |
|
73 | 7 | $options[] = [ |
|
74 | 7 | 'name' => $this->name.'_'.$name, |
|
75 | 7 | 'label'=>$label, |
|
76 | 7 | 'checked' => $this->values[$name] ? true : false |
|
77 | ]; |
||
78 | } |
||
79 | 7 | $properties['options'] = $options; |
|
80 | |||
81 | return $properties; |
||
82 | 11 | } |
|
83 | |||
84 | 11 | public function setValueFromDefault() |
|
85 | 11 | { |
|
86 | foreach (array_keys($this->options) as $key) { |
||
87 | 11 | if (isset($this->default[$key])) $this->values[$key] = $this->default[$key] ? true : false; |
|
88 | } |
||
89 | 1 | } |
|
90 | |||
91 | 1 | public function setValueFromModel($model) |
|
92 | 1 | { |
|
93 | foreach (array_keys($this->options) as $key) { |
||
94 | if (property_exists($model, $this->name.'_'.$key)) $this->values[$key] = $model->{$this->name.'_'.$key} ? true : false; |
||
95 | 1 | } |
|
96 | |||
97 | } |
||
98 | |||
99 | public function setValueFromRequest($request) |
||
100 | { |
||
101 | foreach (array_keys($this->options) as $key) { |
||
102 | $this->values[$key] = $request->get($this->name.'_'.$key) ? true : false; |
||
103 | } |
||
104 | 1 | } |
|
105 | |||
106 | 1 | public function fillModelWithValue($model) |
|
107 | 1 | { |
|
108 | foreach (array_keys($this->options) as $key) { |
||
109 | 1 | if (property_exists($model, $this->name.'_'.$key)) $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.