Failed Conditions
Push — rbac ( be68b4...52c28b )
by Michael
03:11
created

StatsTopCreators::main()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 127
Code Lines 107

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 107
c 1
b 0
f 0
dl 0
loc 127
ccs 0
cts 110
cp 0
rs 8
cc 2
nc 2
nop 0
crap 6

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
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages\Statistics;
10
11
use PDO;
12
use Waca\Tasks\InternalPageBase;
13
14
class StatsTopCreators extends InternalPageBase
15
{
16
    public function main()
17
    {
18
        $this->setHtmlTitle('Top Creators :: Statistics');
19
20
        // Retrieve all-time stats
21
        $queryAllTime = <<<SQL
22
SELECT
23
	/* StatsTopCreators::execute()/queryAllTime */
24
    COUNT(*) count,
25
    user.username username,
26
    user.status status,
27
    user.id userid
28
FROM log
29
LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action
30
INNER JOIN user ON user.id = log.user
31
WHERE emailtemplate.oncreated = '1'
32
   OR log.action = 'Closed custom-y'
33
34
GROUP BY log.user, user.username, user.status
35
ORDER BY COUNT(*) DESC;
36
SQL;
37
38
        // Retrieve all-time stats for active users only
39
        $queryAllTimeActive = <<<SQL
40
SELECT
41
	/* StatsTopCreators::execute()/queryAllTimeActive */
42
    COUNT(*) count,
43
    user.username username,
44
    user.status status,
45
    user.id userid
46
FROM log
47
LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action
48
INNER JOIN user ON user.id = log.user
49
WHERE
50
	(emailtemplate.oncreated = 1 OR log.action = 'Closed custom-y')
51
    AND user.status != 'Suspended'
52
GROUP BY user.username, user.id
53
ORDER BY COUNT(*) DESC;
54
SQL;
55
56
        // Retrieve today's stats (so far)
57
        $queryToday = <<<SQL
58
SELECT
59
	/* StatsTopCreators::execute()/top5out */
60
    COUNT(*) count,
61
    user.username username,
62
    user.status status,
63
    user.id userid
64
FROM log
65
INNER JOIN user ON user.id = log.user
66
LEFT JOIN emailtemplate ON CONCAT('Closed ', emailtemplate.id) = log.action
67
WHERE (emailtemplate.oncreated = '1' OR log.action = 'Closed custom-y')
68
  AND log.timestamp BETWEEN CURRENT_DATE() AND NOW()
69
GROUP BY log.user, user.username
70
ORDER BY COUNT(*) DESC;
71
SQL;
72
73
        // Retrieve Yesterday's stats
74
        $queryYesterday = <<<SQL
75
SELECT
76
	/* StatsTopCreators::execute()/top5yout */
77
    COUNT(*) count,
78
    user.username username,
79
    user.status status,
80
    user.id userid
81
FROM log
82
INNER JOIN user ON user.id = log.user
83
LEFT JOIN emailtemplate ON CONCAT('Closed ', emailtemplate.id) = log.action
84
WHERE (emailtemplate.oncreated = '1' OR log.action = 'Closed custom-y')
85
  AND log.timestamp BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY) AND CURRENT_DATE()
86
GROUP BY log.user, user.username
87
ORDER BY COUNT(*) DESC;
88
SQL;
89
90
        // Retrieve last 7 days
91
        $queryLast7Days = <<<SQL
92
SELECT
93
	/* StatsTopCreators::execute()/top5wout */
94
    COUNT(*) count,
95
    user.username username,
96
    user.status status,
97
    user.id userid
98
FROM log
99
INNER JOIN user ON user.id = log.user
100
LEFT JOIN emailtemplate ON CONCAT('Closed ', emailtemplate.id) = log.action
101
WHERE (emailtemplate.oncreated = '1' OR log.action = 'Closed custom-y')
102
  AND log.timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
103
GROUP BY log.user, user.username
104
ORDER BY COUNT(*) DESC;
105
SQL;
106
107
        // Retrieve last month's stats
108
        $queryLast28Days = <<<SQL
109
SELECT
110
	/* StatsTopCreators::execute()/top5mout */
111
    COUNT(*) count,
112
    user.username username,
113
    user.status status,
114
    user.id userid
115
FROM log
116
INNER JOIN user ON user.id = log.user
117
LEFT JOIN emailtemplate ON CONCAT('Closed ', emailtemplate.id) = log.action
118
WHERE (emailtemplate.oncreated = '1' OR log.action = 'Closed custom-y')
119
  AND log.timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 28 DAY) AND NOW()
120
GROUP BY log.user, user.username
121
ORDER BY COUNT(*) DESC;
122
SQL;
123
124
        // Put it all together
125
        $queries = array(
126
            'queryAllTime'       => $queryAllTime,
127
            'queryAllTimeActive' => $queryAllTimeActive,
128
            'queryToday'         => $queryToday,
129
            'queryYesterday'     => $queryYesterday,
130
            'queryLast7Days'     => $queryLast7Days,
131
            'queryLast28Days'    => $queryLast28Days,
132
        );
133
134
        $database = $this->getDatabase();
135
        foreach ($queries as $name => $sql) {
136
            $statement = $database->query($sql);
137
            $data = $statement->fetchAll(PDO::FETCH_ASSOC);
138
            $this->assign($name, $data);
139
        }
140
141
        $this->assign('statsPageTitle', 'Top Account Creators');
142
        $this->setTemplate('statistics/top-creators.tpl');
143
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
144
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
145