Completed
Push — next ( 5f2bc0...cef70f )
by Thomas
25s queued 12s
created

cachelog   F

Complexity

Total Complexity 73

Size/Duplication

Total Lines 449
Duplicated Lines 17.15 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 77
loc 449
rs 2.56
c 0
b 0
f 0
wmc 73
lcom 1
cbo 2

43 Methods

Rating   Name   Duplication   Size   Complexity  
C validateDate() 0 30 12
A logIdFromUUID() 0 6 1
A fromUUID() 0 9 2
A createNew() 20 20 3
A createNewFromCache() 19 19 3
A __construct() 0 29 2
A exist() 0 4 1
A getLogId() 0 4 1
A getUserId() 0 4 1
A setUserId() 0 4 1
A getCacheId() 0 4 1
A setCacheId() 0 4 1
A getType() 0 4 1
A setType() 0 11 3
A getOcTeamComment() 0 4 1
A setOcTeamComment() 0 4 1
A getDate() 0 4 1
A setDate() 0 4 1
A getNeedsMaintenance() 0 4 1
A setNeedsMaintenance() 0 4 1
A getListingOutdated() 0 4 1
A setListingOutdated() 0 4 1
A getText() 0 4 1
A setText() 0 4 1
A getTextHtml() 0 4 1
A setTextHtml() 0 4 2
A getTextHtmlEdit() 0 4 1
A setTextHtmlEdit() 0 4 2
A getUUID() 0 4 1
A getLastModified() 0 4 1
A getDateCreated() 0 4 1
A getNode() 0 4 1
A setNode() 0 4 1
A getOwnerNotified() 0 4 1
A setOwnerNotified() 0 4 2
A getAnyChanged() 0 4 1
A save() 0 21 4
A updatePictureStat() 0 9 1
A allowView() 26 26 3
A allowEdit() 11 11 2
A getValidLogTypes() 0 12 2
A isDuplicate() 0 20 1
A isMasslogging() 0 45 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 *
5
 *
6
 *   get/set has to be committed with save
7
 *   add/remove etc. is executed instantly
8
 ***************************************************************************/
9
10
require_once __DIR__ . '/logtypes.inc.php';
11
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);
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
53
            return false;
54
        }
55
        if ($cache->allowLog() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The function sql_slave_exclude() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
312
    {
313
        global $login;
314
315
        $login->verify();
316
        $allow = sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_array() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
Documentation introduced by
$rs is of type object<mysqli_result>, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
405
        sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
406
407
        if ($rLastLog) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rLastLog of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
408
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_array() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
Documentation introduced by
$rs is of type object<mysqli_result>, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
421
            $masslogs = $r['masslogs'];
422
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

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.

Loading history...
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)
431
    {
432
        $dateOk = false;
433
434
        if (($hour . $minute === '' || is_numeric($hour))
435
            && ($minute === '' || is_numeric($minute))
436
        ) {
437
            $dateOk = checkdate($month, $day, $year)
438
                && ($year >= 2000)
439
                && ($hour >= 0)
440
                && ($hour <= 23)
441
                && ($minute >= 0)
442
                && ($minute <= 59);
443
            if ($dateOk && $submit) {
444
                $dateOk = (
445
                    mktime(
446
                        (int) $hour,
447
                        (int) $minute,
448
                        0,
449
                        $month,
450
                        $day,
451
                        $year
452
                    )
453
                    < time()
454
                );
455
            }
456
        }
457
458
        return $dateOk;
459
    }
460
}
461