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 |
||
41 | class XoopsConfigHandler extends XoopsObjectHandler |
||
42 | { |
||
43 | /** |
||
44 | * holds reference to config item handler(DAO) class |
||
45 | * |
||
46 | * @var XoopsConfigItemHandler |
||
47 | */ |
||
48 | private $itemHandler; |
||
49 | |||
50 | /** |
||
51 | * holds reference to config option handler(DAO) class |
||
52 | * |
||
53 | * @var XoopsConfigOptionHandler |
||
54 | */ |
||
55 | private $optionHandler; |
||
56 | |||
57 | /** |
||
58 | * holds an array of cached references to config value arrays, |
||
59 | * indexed on module id and category id |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | private $cachedConfigs = array(); |
||
64 | |||
65 | /** |
||
66 | * Constructor |
||
67 | */ |
||
68 | 17 | public function __construct() |
|
73 | |||
74 | /** |
||
75 | * Create a config |
||
76 | * |
||
77 | * @return XoopsConfigItem |
||
78 | */ |
||
79 | 1 | public function createConfig() |
|
84 | |||
85 | /** |
||
86 | * Get a config |
||
87 | * |
||
88 | * @param int $id ID of the config |
||
89 | * @param bool $withoptions load the config's options now? |
||
90 | * |
||
91 | * @return XoopsConfigItem |
||
92 | */ |
||
93 | 2 | View Code Duplication | public function getConfig($id, $withoptions = false) |
102 | |||
103 | /** |
||
104 | * insert a new config in the database |
||
105 | * |
||
106 | * @param XoopsConfigItem $config configuration item |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | 2 | View Code Duplication | public function insertConfig(XoopsConfigItem $config) |
131 | |||
132 | /** |
||
133 | * Delete a config from the database |
||
134 | * |
||
135 | * @param XoopsConfigItem $config configuration item |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | 2 | View Code Duplication | public function deleteConfig(XoopsConfigItem $config) |
160 | |||
161 | /** |
||
162 | * get one or more Configs |
||
163 | * |
||
164 | * @param CriteriaElement|null $criteria criteria to match |
||
165 | * @param bool $id_as_key Use the configs' ID as keys? |
||
166 | * |
||
167 | * @return XoopsConfigItem[] |
||
168 | */ |
||
169 | 3 | View Code Duplication | public function getConfigs(CriteriaElement $criteria = null, $id_as_key = false) |
184 | |||
185 | /** |
||
186 | * Count some configs |
||
187 | * |
||
188 | * @param CriteriaElement|null $criteria criteria to match |
||
189 | * |
||
190 | * @return int |
||
191 | */ |
||
192 | 1 | public function getConfigCount(CriteriaElement $criteria = null) |
|
196 | |||
197 | /** |
||
198 | * Get configs from a certain module |
||
199 | * |
||
200 | * @param int $module ID of a module |
||
201 | * |
||
202 | * @return array of configuration values |
||
203 | */ |
||
204 | public function getConfigsByModule($module = 0) |
||
216 | |||
217 | /** |
||
218 | * Get configs from a certain category |
||
219 | * |
||
220 | * @param int $category ID of a category |
||
221 | * @param int $module ID of a module |
||
222 | * |
||
223 | * @return array of configuration values |
||
224 | * |
||
225 | * @deprecated Use getConfigsByModule instead |
||
226 | */ |
||
227 | 1 | View Code Duplication | public function getConfigsByCat($category, $module = 0) |
248 | |||
249 | /** |
||
250 | * Make a new XoopsConfigOption |
||
251 | * |
||
252 | * @return XoopsConfigOption |
||
253 | */ |
||
254 | 1 | public function createConfigOption() |
|
259 | |||
260 | /** |
||
261 | * Get a XoopsConfigOption |
||
262 | * |
||
263 | * @param int $id ID of the config option |
||
264 | * |
||
265 | * @return XoopsConfigOption |
||
266 | */ |
||
267 | 1 | public function getConfigOption($id) |
|
272 | |||
273 | /** |
||
274 | * Get one or more XoopsConfigOption objects |
||
275 | * |
||
276 | * @param CriteriaElement|null $criteria criteria to match |
||
277 | * @param bool $id_as_key Use IDs as keys in the array? |
||
278 | * |
||
279 | * @return XoopsConfigOption[] |
||
280 | */ |
||
281 | 4 | public function getConfigOptions(CriteriaElement $criteria = null, $id_as_key = false) |
|
285 | |||
286 | /** |
||
287 | * Count options |
||
288 | * |
||
289 | * @param CriteriaElement|null $criteria criteria to match |
||
290 | * |
||
291 | * @return int Count of options matching $criteria |
||
292 | */ |
||
293 | 1 | public function getConfigOptionsCount(CriteriaElement $criteria = null) |
|
297 | |||
298 | /** |
||
299 | * Get a list of configs |
||
300 | * |
||
301 | * @param int $conf_modid ID of the modules |
||
302 | * @param int $conf_catid ID of the category |
||
303 | * |
||
304 | * @return array Associative array of name=>value pairs. |
||
305 | */ |
||
306 | 1 | View Code Duplication | public function getConfigList($conf_modid, $conf_catid = 0) |
327 | } |
||
328 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.