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 |
||
35 | class ConfigService { |
||
36 | |||
37 | const APP_NAVIGATION = 'app_navigation'; |
||
38 | const SEARCH_PLATFORM = 'search_platform'; |
||
39 | const PROVIDER_INDEXED = 'provider_indexed'; |
||
40 | const CRON_LAST_ERR_RESET = 'cron_err_reset'; |
||
41 | |||
42 | /** @var array */ |
||
43 | public $defaults = [ |
||
44 | self::SEARCH_PLATFORM => '', |
||
45 | self::APP_NAVIGATION => '0', |
||
46 | self::PROVIDER_INDEXED => '', |
||
47 | self::CRON_LAST_ERR_RESET => '0' |
||
48 | ]; |
||
49 | |||
50 | /** @var IConfig */ |
||
51 | private $config; |
||
52 | |||
53 | /** @var string */ |
||
54 | private $userId; |
||
55 | |||
56 | /** @var MiscService */ |
||
57 | private $miscService; |
||
58 | |||
59 | |||
60 | /** |
||
61 | * ConfigService constructor. |
||
62 | * |
||
63 | * @param IConfig $config |
||
64 | * @param string $userId |
||
65 | * @param MiscService $miscService |
||
66 | */ |
||
67 | public function __construct( |
||
74 | |||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getConfig() { |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @param array $save |
||
93 | */ |
||
94 | public function setConfig($save) { |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Get a version of an app |
||
107 | * |
||
108 | * @param string $key |
||
|
|||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getAppVersion($appId) { |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Get a value by key |
||
119 | * |
||
120 | * @param string $key |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | View Code Duplication | public function getAppValue($key) { |
|
132 | |||
133 | /** |
||
134 | * Set a value by key |
||
135 | * |
||
136 | * @param string $key |
||
137 | * @param string $value |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function setAppValue($key, $value) { |
||
144 | |||
145 | /** |
||
146 | * remove a key |
||
147 | * |
||
148 | * @param string $key |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function deleteAppValue($key) { |
||
155 | |||
156 | /** |
||
157 | * Get a user value by key |
||
158 | * |
||
159 | * @param string $key |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | View Code Duplication | public function getUserValue($key) { |
|
173 | |||
174 | /** |
||
175 | * Set a user value by key |
||
176 | * |
||
177 | * @param string $key |
||
178 | * @param string $value |
||
179 | * |
||
180 | * @return string |
||
181 | * @throws PreConditionNotMetException |
||
182 | */ |
||
183 | public function setUserValue($key, $value) { |
||
186 | |||
187 | /** |
||
188 | * Get a user value by key and user |
||
189 | * |
||
190 | * @param string $userId |
||
191 | * @param string $key |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getValueForUser($userId, $key) { |
||
198 | |||
199 | /** |
||
200 | * Set a user value by key |
||
201 | * |
||
202 | * @param string $userId |
||
203 | * @param string $key |
||
204 | * @param string $value |
||
205 | * |
||
206 | * @return string |
||
207 | * @throws PreConditionNotMetException |
||
208 | */ |
||
209 | public function setValueForUser($userId, $key, $value) { |
||
212 | |||
213 | |||
214 | /** |
||
215 | * @param string $providerId |
||
216 | * @param string $options |
||
217 | * @param string $value |
||
218 | */ |
||
219 | public function setProviderOptions($providerId, $options, $value) { |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param string $options |
||
233 | */ |
||
234 | public function resetProviderOptions($options) { |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @param string $providerId |
||
241 | * @param string $options |
||
242 | * |
||
243 | * @return string |
||
244 | * @throws ProviderOptionsDoesNotExistException |
||
245 | */ |
||
246 | public function getProviderOptions($providerId, $options) { |
||
258 | |||
259 | |||
260 | /** |
||
261 | * return the cloud version. |
||
262 | * if $complete is true, return a string x.y.z |
||
263 | * |
||
264 | * @param boolean $complete |
||
265 | * |
||
266 | * @return string|integer |
||
267 | */ |
||
268 | public function getCloudVersion($complete = false) { |
||
277 | } |
||
278 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.