Conditions | 3 |
Paths | 3 |
Total Lines | 79 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function getRevisionCounts($userId) |
||
70 | { |
||
71 | // Prepare the query and execute |
||
72 | $archiveTable = $this->labsHelper->getTable('archive'); |
||
73 | $revisionTable = $this->labsHelper->getTable('revision'); |
||
74 | $resultQuery = $this->replicas->prepare(" |
||
75 | (SELECT 'deleted' as source, COUNT(ar_id) AS value FROM $archiveTable |
||
76 | WHERE ar_user = :userId) |
||
77 | UNION |
||
78 | (SELECT 'live' as source, COUNT(rev_id) AS value FROM $revisionTable |
||
79 | WHERE rev_user = :userId) |
||
80 | UNION |
||
81 | (SELECT 'first' as source, rev_timestamp FROM $revisionTable |
||
82 | WHERE rev_user = :userId ORDER BY rev_timestamp ASC LIMIT 1) |
||
83 | UNION |
||
84 | (SELECT 'last' as source, rev_timestamp FROM $revisionTable |
||
85 | WHERE rev_user = :userId ORDER BY rev_timestamp DESC LIMIT 1) |
||
86 | UNION |
||
87 | (SELECT '24h' as source, COUNT(rev_id) as value FROM $revisionTable |
||
88 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(),INTERVAL 24 HOUR)) |
||
89 | UNION |
||
90 | (SELECT '7d' as source, COUNT(rev_id) as value FROM $revisionTable |
||
91 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(),INTERVAL 7 DAY)) |
||
92 | UNION |
||
93 | (SELECT '30d' as source, COUNT(rev_id) as value FROM $revisionTable |
||
94 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(),INTERVAL 30 DAY)) |
||
95 | UNION |
||
96 | (SELECT '365d' as source, COUNT(rev_id) as value FROM $revisionTable |
||
97 | WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(),INTERVAL 365 DAY)) |
||
98 | UNION |
||
99 | (SELECT 'small' AS source, COUNT(rev_id) AS value FROM $revisionTable |
||
100 | WHERE rev_user = :userId AND rev_len < 20) |
||
101 | UNION |
||
102 | (SELECT 'large' AS source, COUNT(rev_id) AS value FROM $revisionTable |
||
103 | WHERE rev_user = :userId AND rev_len > 1000) |
||
104 | UNION |
||
105 | (SELECT 'with_comments' AS source, COUNT(rev_id) AS value FROM $revisionTable |
||
106 | WHERE rev_user = :userId AND rev_comment = '') |
||
107 | UNION |
||
108 | (SELECT 'minor_edits' AS source, COUNT(rev_id) AS value FROM $revisionTable |
||
109 | WHERE rev_user = :userId AND rev_minor_edit = 1) |
||
110 | "); |
||
111 | $resultQuery->bindParam("userId", $userId); |
||
112 | $resultQuery->execute(); |
||
113 | $results = $resultQuery->fetchAll(); |
||
114 | |||
115 | // Unknown user - This is a dirty hack that should be fixed. |
||
116 | if (count($results) < 8) { |
||
117 | throw new Exception("Unable to get all revision counts for user $userId"); |
||
118 | } |
||
119 | |||
120 | $revisionCounts = array_combine( |
||
121 | array_map(function ($e) { |
||
122 | return $e['source']; |
||
123 | }, $results), |
||
124 | array_map(function ($e) { |
||
125 | return $e['value']; |
||
126 | }, $results) |
||
127 | ); |
||
128 | |||
129 | // Count the number of days, accounting for when there's only one edit. |
||
130 | $editingTimeInSeconds = ceil($revisionCounts['last'] - $revisionCounts['first']); |
||
131 | $revisionCounts['days'] = $editingTimeInSeconds ? $editingTimeInSeconds/(60*60*24) : 1; |
||
132 | |||
133 | // Format the first and last dates. |
||
134 | $revisionCounts['first'] = date('Y-m-d H:i', strtotime($revisionCounts['first'])); |
||
135 | $revisionCounts['last'] = date('Y-m-d H:i', strtotime($revisionCounts['last'])); |
||
136 | |||
137 | // Sum deleted and live to make the total. |
||
138 | $revisionCounts['total'] = $revisionCounts['deleted'] + $revisionCounts['live']; |
||
139 | |||
140 | // Calculate the average number of live edits per day. |
||
141 | $revisionCounts['avg_per_day'] = round( |
||
142 | $revisionCounts['live'] / $revisionCounts['days'], |
||
143 | 3 |
||
144 | ); |
||
145 | |||
146 | return $revisionCounts; |
||
147 | } |
||
148 | |||
277 |