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:
Complex classes like cachelog often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use cachelog, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class cachelog |
||
13 | { |
||
14 | const LOGTYPE_FOUND = 1; |
||
15 | const LOGTYPE_NOTFOUND = 2; |
||
16 | const LOGTYPE_NOTE = 3; |
||
17 | const LOGTYPE_ATTENDED = 7; |
||
18 | const LOGTYPE_WILLATTEND = 8; |
||
19 | const LOGTYPE_ARCHIVED = 9; |
||
20 | const LOGTYPE_ACTIVE = 10; |
||
21 | const LOGTYPE_DISABLED = 11; |
||
22 | const LOGTYPE_LOCKED = 13; |
||
23 | const LOGTYPE_LOCKED_INVISIBLE = 14; |
||
24 | |||
25 | public $nLogId = 0; |
||
26 | |||
27 | public $reCacheLog; |
||
28 | |||
29 | public static function logIdFromUUID($uuid) |
||
30 | { |
||
31 | $cacheid = sql_value("SELECT `id` FROM `cache_logs` WHERE `uuid`='&1'", 0, $uuid); |
||
|
|||
32 | |||
33 | return $cacheid; |
||
34 | } |
||
35 | |||
36 | public static function fromUUID($uuid) |
||
37 | { |
||
38 | $logid = self::logIdFromUUID($uuid); |
||
39 | if ($logid == 0) { |
||
40 | return null; |
||
41 | } |
||
42 | |||
43 | return new self($logid); |
||
44 | } |
||
45 | |||
46 | View Code Duplication | public static function createNew($nCacheId, $nUserId) |
|
47 | { |
||
48 | global $opt; |
||
49 | |||
50 | // check if user is allowed to log this cache! |
||
51 | $cache = new cache($nCacheId); |
||
52 | if ($cache->exist() == false) { |
||
53 | return false; |
||
54 | } |
||
55 | if ($cache->allowLog() == false) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | $oCacheLog = new self(ID_NEW); |
||
60 | $oCacheLog->setUserId($nUserId); |
||
61 | $oCacheLog->setCacheId($nCacheId); |
||
62 | $oCacheLog->setNode($opt['logic']['node']['id']); |
||
63 | |||
64 | return $oCacheLog; |
||
65 | } |
||
66 | |||
67 | View Code Duplication | public static function createNewFromCache($oCache, $nUserId) |
|
68 | { |
||
69 | global $opt; |
||
70 | |||
71 | // check if user is allowed to log this cache! |
||
72 | if ($oCache->exist() == false) { |
||
73 | return false; |
||
74 | } |
||
75 | if ($oCache->allowLog() == false) { |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | $oCacheLog = new self(ID_NEW); |
||
80 | $oCacheLog->setUserId($nUserId); |
||
81 | $oCacheLog->setCacheId($oCache->getCacheId()); |
||
82 | $oCacheLog->setNode($opt['logic']['node']['id']); |
||
83 | |||
84 | return $oCacheLog; |
||
85 | } |
||
86 | |||
87 | public function __construct($nNewLogId = ID_NEW) |
||
88 | { |
||
89 | $this->reCacheLog = new rowEditor('cache_logs'); |
||
90 | $this->reCacheLog->addPKInt('id', null, false, RE_INSERT_AUTOINCREMENT); |
||
91 | $this->reCacheLog->addString('uuid', '', false, RE_INSERT_AUTOUUID); |
||
92 | $this->reCacheLog->addInt('node', 0, false); |
||
93 | $this->reCacheLog->addDate('date_created', time(), true, RE_INSERT_IGNORE); |
||
94 | $this->reCacheLog->addDate('last_modified', time(), true, RE_INSERT_IGNORE); |
||
95 | $this->reCacheLog->addInt('cache_id', 0, false); |
||
96 | $this->reCacheLog->addInt('user_id', 0, false); |
||
97 | $this->reCacheLog->addInt('type', 0, false); |
||
98 | $this->reCacheLog->addInt('oc_team_comment', 0, false); |
||
99 | $this->reCacheLog->addDate('date', time(), false); |
||
100 | $this->reCacheLog->addInt('needs_maintenance', 0, false); |
||
101 | $this->reCacheLog->addInt('listing_outdated', 0, false); |
||
102 | $this->reCacheLog->addString('text', '', false); |
||
103 | $this->reCacheLog->addInt('text_html', 1, false); |
||
104 | $this->reCacheLog->addInt('text_htmledit', 1, false); |
||
105 | $this->reCacheLog->addInt('owner_notified', 0, false); |
||
106 | $this->reCacheLog->addInt('picture', 0, false); |
||
107 | |||
108 | $this->nLogId = $nNewLogId + 0; |
||
109 | |||
110 | if ($nNewLogId == ID_NEW) { |
||
111 | $this->reCacheLog->addNew(null); |
||
112 | } else { |
||
113 | $this->reCacheLog->load($this->nLogId); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public function exist() |
||
118 | { |
||
119 | return $this->reCacheLog->exist(); |
||
120 | } |
||
121 | |||
122 | public function getLogId() |
||
123 | { |
||
124 | return $this->nLogId; |
||
125 | } |
||
126 | |||
127 | public function getUserId() |
||
128 | { |
||
129 | return $this->reCacheLog->getValue('user_id'); |
||
130 | } |
||
131 | |||
132 | public function setUserId($value) |
||
133 | { |
||
134 | return $this->reCacheLog->setValue('user_id', $value); |
||
135 | } |
||
136 | |||
137 | public function getCacheId() |
||
138 | { |
||
139 | return $this->reCacheLog->getValue('cache_id'); |
||
140 | } |
||
141 | |||
142 | public function setCacheId($value) |
||
143 | { |
||
144 | return $this->reCacheLog->setValue('cache_id', $value); |
||
145 | } |
||
146 | |||
147 | public function getType() |
||
148 | { |
||
149 | return $this->reCacheLog->getValue('type'); |
||
150 | } |
||
151 | |||
152 | public function setType($value, $force = false) |
||
153 | { |
||
154 | if (!$force) { |
||
155 | $nValidLogTypes = $this->getValidLogTypes(); |
||
156 | if (array_search($value, $nValidLogTypes) === false) { |
||
157 | return false; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $this->reCacheLog->setValue('type', $value); |
||
162 | } |
||
163 | |||
164 | public function getOcTeamComment() |
||
165 | { |
||
166 | return $this->reCacheLog->getValue('oc_team_comment'); |
||
167 | } |
||
168 | |||
169 | public function setOcTeamComment($value) |
||
170 | { |
||
171 | return $this->reCacheLog->setValue('oc_team_comment', $value); |
||
172 | } |
||
173 | |||
174 | public function getDate() |
||
175 | { |
||
176 | return $this->reCacheLog->getValue('date'); |
||
177 | } |
||
178 | |||
179 | public function setDate($value) |
||
180 | { |
||
181 | return $this->reCacheLog->setValue('date', $value); |
||
182 | } |
||
183 | |||
184 | public function getNeedsMaintenance() |
||
185 | { |
||
186 | return $this->reCacheLog->getValue('needs_maintenance'); |
||
187 | } |
||
188 | |||
189 | public function setNeedsMaintenance($value) |
||
190 | { |
||
191 | return $this->reCacheLog->setValue('needs_maintenance', $value); |
||
192 | } |
||
193 | |||
194 | public function getListingOutdated() |
||
195 | { |
||
196 | return $this->reCacheLog->getValue('listing_outdated'); |
||
197 | } |
||
198 | |||
199 | public function setListingOutdated($value) |
||
200 | { |
||
201 | return $this->reCacheLog->setValue('listing_outdated', $value); |
||
202 | } |
||
203 | |||
204 | public function getText() |
||
205 | { |
||
206 | return $this->reCacheLog->getValue('text'); |
||
207 | } |
||
208 | |||
209 | public function setText($value) |
||
210 | { |
||
211 | return $this->reCacheLog->setValue('text', $value); |
||
212 | } |
||
213 | |||
214 | public function getTextHtml() |
||
215 | { |
||
216 | return $this->reCacheLog->getValue('text_html'); |
||
217 | } |
||
218 | |||
219 | public function setTextHtml($value) |
||
220 | { |
||
221 | return $this->reCacheLog->setValue('text_html', $value ? 1 : 0); |
||
222 | } |
||
223 | |||
224 | public function getTextHtmlEdit() |
||
225 | { |
||
226 | return $this->reCacheLog->getValue('text_html'); |
||
227 | } |
||
228 | |||
229 | public function setTextHtmlEdit($value) |
||
230 | { |
||
231 | return $this->reCacheLog->setValue('text_htmledit', $value ? 1 : 0); |
||
232 | } |
||
233 | |||
234 | public function getUUID() |
||
235 | { |
||
236 | return $this->reCacheLog->getValue('uuid'); |
||
237 | } |
||
238 | |||
239 | public function getLastModified() |
||
240 | { |
||
241 | return $this->reCacheLog->getValue('last_modified'); |
||
242 | } |
||
243 | |||
244 | public function getDateCreated() |
||
245 | { |
||
246 | return $this->reCacheLog->getValue('date_created'); |
||
247 | } |
||
248 | |||
249 | public function getNode() |
||
250 | { |
||
251 | return $this->reCacheLog->getValue('node'); |
||
252 | } |
||
253 | |||
254 | public function setNode($value) |
||
255 | { |
||
256 | return $this->reCacheLog->setValue('node', $value); |
||
257 | } |
||
258 | |||
259 | public function getOwnerNotified() |
||
260 | { |
||
261 | return $this->reCacheLog->getValue('owner_notified') != 0; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param int $value |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function setOwnerNotified($value) |
||
269 | { |
||
270 | return $this->reCacheLog->setValue('owner_notified', $value ? 1 : 0); |
||
271 | } |
||
272 | |||
273 | public function getAnyChanged() |
||
274 | { |
||
275 | return $this->reCacheLog->getAnyChanged(); |
||
276 | } |
||
277 | |||
278 | // return if successfull (with insert) |
||
279 | public function save() |
||
280 | { |
||
281 | // additional safeguard against setting unallowd log flags |
||
282 | if (!sql_value( |
||
283 | "SELECT `maintenance_logs` FROM `log_types` WHERE `id`='&1'", |
||
284 | false, |
||
285 | $this->getType() |
||
286 | ) |
||
287 | ) { |
||
288 | $this->setNeedsMaintenance(false); |
||
289 | $this->setListingOutdated(false); |
||
290 | } |
||
291 | |||
292 | sql_slave_exclude(); |
||
293 | $saved = $this->reCacheLog->save(); |
||
294 | if ($saved && $this->nLogId == ID_NEW) { |
||
295 | $this->nLogId = $this->reCacheLog->getValue('id'); |
||
296 | } |
||
297 | |||
298 | return $saved; |
||
299 | } |
||
300 | |||
301 | public function updatePictureStat(): void |
||
302 | { |
||
303 | sql( |
||
304 | "UPDATE `cache_logs` SET `picture` = |
||
305 | (SELECT COUNT(*) FROM `pictures` WHERE `object_type`=1 AND `object_id`='&1') |
||
306 | WHERE `id`= '&1'", |
||
307 | $this->getLogId() |
||
308 | ); |
||
309 | } |
||
310 | |||
311 | View Code Duplication | public function allowView() |
|
312 | { |
||
313 | global $login; |
||
314 | |||
315 | $login->verify(); |
||
316 | $allow = sql_value( |
||
317 | "SELECT `cache_status`.`allow_user_view` |
||
318 | FROM `caches` |
||
319 | INNER JOIN `cache_status` |
||
320 | ON `caches`.`status`=`cache_status`.`id` |
||
321 | WHERE `caches`.`cache_id`='&1'", |
||
322 | 0, |
||
323 | $this->getCacheId() |
||
324 | ); |
||
325 | if ($allow == 1) { |
||
326 | return true; |
||
327 | } elseif ($login->userid == sql_value( |
||
328 | "SELECT `user_id` FROM `caches` WHERE `cache_id`='&1'", |
||
329 | 0, |
||
330 | $this->getCacheId() |
||
331 | )) { |
||
332 | return true; |
||
333 | } |
||
334 | |||
335 | return false; |
||
336 | } |
||
337 | |||
338 | View Code Duplication | public function allowEdit() |
|
339 | { |
||
340 | global $login; |
||
341 | |||
342 | $login->verify(); |
||
343 | if ($this->getUserId() == $login->userid) { |
||
344 | return true; |
||
345 | } |
||
346 | |||
347 | return false; |
||
348 | } |
||
349 | |||
350 | public function getValidLogTypes() |
||
351 | { |
||
352 | $cache = new cache($this->getCacheId()); |
||
353 | if ($cache->exist() == false) { |
||
354 | return []; |
||
355 | } |
||
356 | // if ($cache->allowLog() == false) |
||
357 | // return array(); |
||
358 | // Logic Error - log types are still valid when no NEW logs are allowed for the cache. |
||
359 | // (Would e.g. block admin logs and log-type restoring for locked caches.) |
||
360 | return get_cache_log_types($this->getCacheId(), $this->getType()); // depends on userid |
||
361 | } |
||
362 | |||
363 | public static function isDuplicate($cacheId, $userId, $logType, $logDate, $logText) |
||
364 | { |
||
365 | // get info if exact the same values are already in database |
||
366 | return sql_value( |
||
367 | "SELECT COUNT(`id`) |
||
368 | FROM `cache_logs` |
||
369 | WHERE |
||
370 | `cache_id`='&1' |
||
371 | AND `user_id`='&2' |
||
372 | AND `type`='&3' |
||
373 | AND `date`='&4' |
||
374 | AND `text`='&5'", |
||
375 | 0, |
||
376 | $cacheId, |
||
377 | $userId, |
||
378 | $logType, |
||
379 | $logDate, |
||
380 | $logText |
||
381 | ) != 0; |
||
382 | } |
||
383 | |||
384 | public static function isMasslogging($userId) |
||
385 | { |
||
386 | global $opt; |
||
387 | |||
388 | // check for probably wrong-dated mass logs |
||
389 | |||
390 | $rs = sql( |
||
391 | " |
||
392 | SELECT `date`, `text` |
||
393 | FROM `cache_logs` |
||
394 | WHERE `id`= ( |
||
395 | SELECT `id` |
||
396 | FROM `cache_logs` |
||
397 | WHERE `user_id`='&1' |
||
398 | ORDER BY `date_created` DESC, |
||
399 | `id` DESC |
||
400 | LIMIT 1)", |
||
401 | $userId |
||
402 | ); |
||
403 | |||
404 | $rLastLog = sql_fetch_array($rs); |
||
405 | sql_free_result($rs); |
||
406 | |||
407 | if ($rLastLog) { |
||
408 | $rs = sql( |
||
409 | " |
||
410 | SELECT COUNT(*) AS `masslogs` |
||
411 | FROM `cache_logs` |
||
412 | WHERE `user_id`='&1' |
||
413 | AND `date`='&2' |
||
414 | AND `text`='&3'", |
||
415 | $userId, |
||
416 | $rLastLog['date'], |
||
417 | $rLastLog['text'] |
||
418 | ); |
||
419 | |||
420 | $r = sql_fetch_array($rs); |
||
421 | $masslogs = $r['masslogs']; |
||
422 | sql_free_result($rs); |
||
423 | } else { |
||
424 | $masslogs = 0; |
||
425 | } |
||
426 | |||
427 | return ($masslogs > $opt['logic']['masslog']['count']); |
||
428 | } |
||
429 | |||
430 | public static function validateDate($year, $month, $day, $hour, $minute, $submit) |
||
460 | } |
||
461 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.