Completed
Push — development ( c1f21f...3af93a )
by Thomas
22s
created

Recommendation::discardRecommendation()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 58
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 58
rs 8.7274

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
namespace OcLegacy\GeoCache;
7
8
class Recommendation
9
{
10
    /**
11
     * @param $logId
12
     */
13
    public static function discardRecommendation($logId)
14
    {
15
        $rsLog = sql(
16
            "SELECT `cache_logs`.`cache_id`,
17
                    `cache_logs`.`user_id`,
18
                    `cache_rating`.`rating_date` IS NOT NULL AS `is_rating_log`
19
             FROM `cache_logs`
20
             LEFT JOIN `cache_rating`
21
               ON `cache_rating`.`cache_id`=`cache_logs`.`cache_id`
22
               AND `cache_rating`.`user_id`=`cache_logs`.`user_id`
23
               AND `cache_rating`.`rating_date`=`cache_logs`.`date`
24
             WHERE `cache_logs`.`id`='&1'
25
               AND `cache_logs`.`type` IN (1,7)",
26
            $logId
27
        );
28
29
        if ($rLog = sql_fetch_assoc($rsLog)) {
30
            $rsFirstOtherFound = sql(
31
                "SELECT `date`
32
                 FROM `cache_logs`
33
                 WHERE `cache_id`='&1'
34
                   AND `user_id`='&2'
35
                   AND `id`<>'&3'
36
                   AND `type` IN (1,7)
37
                 ORDER BY `date`
38
                 LIMIT 1",
39
                $rLog['cache_id'],
40
                $rLog['user_id'],
41
                $logId
42
            );
43
            $rFirstOtherFound = sql_fetch_assoc($rsFirstOtherFound);
44
            sql_free_result($rsFirstOtherFound);
45
46
            if ($rFirstOtherFound && $rLog['is_rating_log']) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rFirstOtherFound 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...
47
                sql(
48
                    "UPDATE `cache_rating`
49
                     SET `rating_date`='&3'
50
                     WHERE `cache_id`='&1'
51
                     AND `user_id`='&2'",
52
                    $rLog['cache_id'],
53
                    $rLog['user_id'],
54
                    $rFirstOtherFound['date']
55
                );
56
                // This will trigger an cache_logs.last_modified update of the corresponding
57
                // log, so that XML interface will resend it with the updated
58
                // "recommendation" flag.
59
            } elseif (!$rFirstOtherFound) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rFirstOtherFound 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...
60
                // This is also called for $rLog['is_rating_log'] == false, so that
61
                // even a rating record with inconsistent date gets deleted.
62
                sql(
63
                    "DELETE FROM `cache_rating` WHERE `cache_id` = '&1' AND `user_id` = '&2'",
64
                    $rLog['cache_id'],
65
                    $rLog['user_id']
66
                );
67
            }
68
        }
69
        sql_free_result($rsLog);
70
    }
71
}
72