Conditions | 18 |
Paths | 2166 |
Total Lines | 177 |
Code Lines | 97 |
Lines | 4 |
Ratio | 2.26 % |
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 |
||
123 | public function resultAction($project, $username, $start = null, $end = null) |
||
124 | { |
||
125 | // Pull the labs helper and check if enabled |
||
126 | $lh = $this->get('app.labs_helper'); |
||
127 | |||
128 | // Pull information about the project |
||
129 | $projectData = ProjectRepository::getProject($project, $this->container); |
||
130 | |||
131 | View Code Duplication | if (!$projectData->exists()) { |
|
132 | $this->addFlash('notice', ['invalid-project', $project]); |
||
133 | return $this->redirectToRoute('autoedits'); |
||
134 | } |
||
135 | |||
136 | $dbName = $projectData->getDatabaseName(); |
||
137 | $projectUrl = $projectData->getUrl(); |
||
138 | |||
139 | // Grab our database connection |
||
140 | $dbh = $this->get('doctrine')->getManager('replicas')->getConnection(); |
||
141 | |||
142 | // Validating the dates. If the dates are invalid, we'll redirect |
||
143 | // to the project and username view. |
||
144 | $invalidDates = ( |
||
145 | (isset($start) && strtotime($start) === false) || |
||
146 | (isset($end) && strtotime($end) === false) |
||
147 | ); |
||
148 | if ($invalidDates) { |
||
149 | // Make sure to add the flash notice first. |
||
150 | $this->addFlash('notice', ['invalid-date']); |
||
151 | |||
152 | // Then redirect us! |
||
153 | return $this->redirectToRoute( |
||
154 | 'autoeditsResult', |
||
155 | [ |
||
156 | 'project' => $project, |
||
157 | 'username' => $username, |
||
158 | ] |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | $user = new User($username); |
||
163 | $username = $user->getUsername(); // use normalized user name |
||
164 | |||
165 | // Now, load the semi-automated edit types. |
||
166 | $AEBTypes = $this->getParameter('automated_tools'); |
||
167 | |||
168 | // Create a collection of queries that we're going to run. |
||
169 | $queries = []; |
||
170 | |||
171 | $revisionTable = $lh->getTable('revision', $dbName); |
||
172 | $archiveTable = $lh->getTable('archive', $dbName); |
||
173 | |||
174 | $condBegin = $start ? " AND rev_timestamp > :start " : null; |
||
175 | $condEnd = $end ? " AND rev_timestamp < :end ": null; |
||
176 | |||
177 | $regexes = []; |
||
178 | |||
179 | foreach ($AEBTypes as $toolname => $values) { |
||
180 | $toolname = $dbh->quote($toolname, \PDO::PARAM_STR); |
||
181 | $regexes[] = $values['regex']; |
||
182 | $regex = $dbh->quote($values['regex'], \PDO::PARAM_STR); |
||
183 | |||
184 | $queries[] .= " |
||
185 | SELECT $toolname AS toolname, COUNT(*) AS count |
||
186 | FROM $revisionTable |
||
187 | WHERE rev_user_text = :username |
||
188 | AND rev_comment REGEXP $regex |
||
189 | $condBegin |
||
190 | $condEnd |
||
191 | "; |
||
192 | } |
||
193 | |||
194 | // Query to get combined (semi)automated using for all edits |
||
195 | // (some automated edits overlap) |
||
196 | $allAETools = $dbh->quote(implode('|', $regexes), \PDO::PARAM_STR); |
||
197 | $queries[] = " |
||
198 | SELECT 'total_live' AS toolname, COUNT(*) AS count |
||
199 | FROM $revisionTable |
||
200 | WHERE rev_user_text = :username |
||
201 | AND rev_comment REGEXP $allAETools |
||
202 | $condBegin |
||
203 | $condEnd |
||
204 | "; |
||
205 | |||
206 | // Next, add two simple queries for the live and deleted edits. |
||
207 | $queries[] = " |
||
208 | SELECT 'live' AS toolname, COUNT(*) AS count |
||
209 | FROM $revisionTable |
||
210 | WHERE rev_user_text = :username |
||
211 | $condBegin |
||
212 | $condEnd |
||
213 | "; |
||
214 | |||
215 | $condBegin = str_replace('rev_timestamp', 'ar_timestamp', $condBegin); |
||
216 | $condEnd = str_replace('rev_timestamp', 'ar_timestamp', $condEnd); |
||
217 | |||
218 | $queries[] = " |
||
219 | SELECT 'deleted' AS toolname, COUNT(*) AS count |
||
220 | FROM $archiveTable |
||
221 | WHERE ar_user_text = :username |
||
222 | $condBegin |
||
223 | $condEnd |
||
224 | "; |
||
225 | |||
226 | // Create a big query and execute. |
||
227 | $stmt = implode(' UNION ', $queries); |
||
228 | |||
229 | $sth = $dbh->prepare($stmt); |
||
230 | |||
231 | $sth->bindParam('username', $username); |
||
232 | $sth->bindParam('start', $start); |
||
233 | $sth->bindParam('end', $end); |
||
234 | |||
235 | $sth->execute(); |
||
236 | |||
237 | // handling results |
||
238 | $results = []; |
||
239 | $total_semi = 0; |
||
240 | $total = 0; |
||
241 | |||
242 | while ($row = $sth->fetch()) { |
||
243 | // Different variables need to get set if the tool is |
||
244 | // the live edits or deleted edits. |
||
245 | // If it is neither and greater than 0, |
||
246 | // add them to the array we're rendering and to our running total |
||
247 | $tool = $row['toolname']; |
||
248 | if ($tool === 'live') { |
||
249 | $total += $row['count']; |
||
250 | } elseif ($tool === 'deleted') { |
||
251 | $total += $row['count']; |
||
252 | } elseif ($tool === 'total_live') { |
||
253 | $total_semi = $row['count']; |
||
254 | } elseif ($row['count'] > 0) { |
||
255 | $results[$tool] = [ |
||
256 | 'link' => $AEBTypes[$tool]['link'], |
||
257 | 'count' => $row['count'], |
||
258 | ]; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | // Inform user if no revisions found. |
||
263 | if ($total === 0) { |
||
264 | $this->addFlash('notice', ['no-contribs']); |
||
265 | return $this->redirectToRoute('autoedits'); |
||
266 | } |
||
267 | |||
268 | // Sort the array and do some simple math. |
||
269 | uasort($results, function ($a, $b) { |
||
270 | return $b['count'] - $a['count']; |
||
271 | }); |
||
272 | |||
273 | if ($total != 0) { |
||
274 | $total_pct = ($total_semi / $total) * 100; |
||
275 | } else { |
||
276 | $total_pct = 0; |
||
277 | } |
||
278 | |||
279 | $ret = [ |
||
280 | 'xtPage' => 'autoedits', |
||
281 | 'xtTitle' => $username, |
||
282 | 'user' => $user, |
||
283 | 'project' => $projectData, |
||
284 | 'semi_automated' => $results, |
||
285 | 'total_semi' => $total_semi, |
||
286 | 'total' => $total, |
||
287 | 'total_pct' => $total_pct, |
||
288 | ]; |
||
289 | |||
290 | if (isset($start)) { |
||
291 | $ret['start'] = $start; |
||
292 | } |
||
293 | if (isset($end)) { |
||
294 | $ret['end'] = $end; |
||
295 | } |
||
296 | |||
297 | // Render the view with all variables set. |
||
298 | return $this->render('autoEdits/result.html.twig', $ret); |
||
299 | } |
||
300 | } |
||
301 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.