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 PlaceAutocompleteRequest extends AbstractPlaceAutocompleteRequest |
||
18 | { |
||
19 | /** |
||
20 | * @var string[] |
||
21 | */ |
||
22 | private $types = []; |
||
23 | |||
24 | /** |
||
25 | * @var string[] |
||
26 | */ |
||
27 | private $components = []; |
||
28 | |||
29 | /** |
||
30 | * @return bool |
||
31 | */ |
||
32 | 28 | public function hasTypes() |
|
36 | |||
37 | /** |
||
38 | * @return string[] |
||
39 | */ |
||
40 | 20 | public function getTypes() |
|
44 | |||
45 | /** |
||
46 | * @param string[] $types |
||
47 | */ |
||
48 | 12 | public function setTypes(array $types) |
|
53 | |||
54 | /** |
||
55 | * @param string[] $types |
||
56 | */ |
||
57 | 12 | public function addTypes(array $types) |
|
63 | |||
64 | /** |
||
65 | * @param string $type |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | 20 | public function hasType($type) |
|
73 | |||
74 | /** |
||
75 | * @param string $type |
||
76 | */ |
||
77 | 20 | public function addType($type) |
|
83 | |||
84 | /** |
||
85 | * @param string $type |
||
86 | */ |
||
87 | 4 | public function removeType($type) |
|
92 | |||
93 | /** |
||
94 | * @return bool |
||
95 | */ |
||
96 | 28 | public function hasComponents() |
|
100 | |||
101 | /** |
||
102 | * @return string[] |
||
103 | */ |
||
104 | 20 | public function getComponents() |
|
108 | |||
109 | /** |
||
110 | * @param string[] $components |
||
111 | */ |
||
112 | 12 | public function setComponents(array $components) |
|
117 | |||
118 | /** |
||
119 | * @param string[] $components |
||
120 | */ |
||
121 | 12 | public function addComponents(array $components) |
|
127 | |||
128 | /** |
||
129 | * @param string $type |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | 12 | public function hasComponent($type) |
|
137 | |||
138 | /** |
||
139 | * @param string $type |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 12 | public function getComponent($type) |
|
147 | |||
148 | /** |
||
149 | * @param string $type |
||
150 | * @param string $value |
||
151 | */ |
||
152 | 20 | public function setComponent($type, $value) |
|
156 | |||
157 | /** |
||
158 | * @param string $type |
||
159 | */ |
||
160 | 4 | public function removeComponent($type) |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function buildContext() |
||
169 | { |
||
170 | return 'autocomplete'; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | 8 | public function buildQuery() |
|
192 | } |
||
193 |
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.