| Conditions | 13 |
| Paths | 324 |
| Total Lines | 150 |
| Code Lines | 79 |
| Lines | 13 |
| Ratio | 8.67 % |
| 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 |
||
| 103 | public function resultAction($project, $username, $start = null, $end = null) |
||
| 104 | { |
||
| 105 | // Pull the labs helper and check if enabled |
||
| 106 | $lh = $this->get("app.labs_helper"); |
||
| 107 | $lh->checkEnabled("autoedits"); |
||
| 108 | |||
| 109 | // Pull information about the project from the Labs Helper |
||
| 110 | $dbValues = $lh->databasePrepare($project, "AutomatedEdits"); |
||
| 111 | |||
| 112 | $dbName = $dbValues["dbName"]; |
||
| 113 | $wikiName = $dbValues["wikiName"]; |
||
| 114 | $url = $dbValues["url"]; |
||
| 115 | |||
| 116 | // Grab our database connection |
||
| 117 | $dbh = $this->get('doctrine')->getManager("replicas")->getConnection(); |
||
| 118 | |||
| 119 | // Variable parsing. |
||
| 120 | // Username needs to be uppercase first (yay Mediawiki), |
||
| 121 | // and we also need to handle undefined dates. |
||
| 122 | $username = ucfirst($username); |
||
| 123 | |||
| 124 | if ($start == null) { |
||
| 125 | $start = date("Y-m-d", strtotime("-1 month")); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($end == null) { |
||
| 129 | $end = date("Y-m-d"); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Validating the dates. If the dates are invalid, we'll redirect |
||
| 133 | // to the project and username view. |
||
| 134 | View Code Duplication | if (strtotime($start) === false || strtotime($end) === false) { |
|
| 135 | // Make sure to add the flash notice first. |
||
| 136 | $this->addFlash("notice", ["invalid_date"]); |
||
| 137 | |||
| 138 | // Then redirect us! |
||
| 139 | return $this->redirectToRoute( |
||
| 140 | "autoeditsResult", |
||
| 141 | [ |
||
| 142 | "project" => $project, |
||
| 143 | "username" => $username, |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Now, load the semi-automated edit types. |
||
| 149 | $AEBTypes = []; |
||
| 150 | $AEBTypes = $this->getParameter("automated_tools"); |
||
| 151 | |||
| 152 | // Create a collection of queries that we're going to run. |
||
| 153 | $queries = []; |
||
| 154 | |||
| 155 | $rev = $lh->getTable("revision", $dbName); |
||
| 156 | $arc = $lh->getTable("archive", $dbName); |
||
| 157 | |||
| 158 | $cond_begin = ( $start ) ? " AND rev_timestamp > :start " : null; |
||
| 159 | $cond_end = ( $end ) ? " AND rev_timestamp < :end ": null; |
||
| 160 | |||
| 161 | foreach ($AEBTypes as $toolname => $check) { |
||
| 162 | $toolname = $dbh->quote($toolname, \PDO::PARAM_STR); |
||
| 163 | $check = $dbh->quote($check, \PDO::PARAM_STR); |
||
| 164 | |||
| 165 | $queries[] .= " |
||
| 166 | SELECT $toolname as toolname, count(*) as count |
||
| 167 | FROM $rev |
||
| 168 | WHERE rev_user_text = :username |
||
| 169 | AND rev_comment REGEXP $check |
||
| 170 | $cond_begin |
||
| 171 | $cond_end |
||
| 172 | "; |
||
| 173 | } |
||
| 174 | |||
| 175 | // Next, add two simple queries for the live and deleted edits. |
||
| 176 | $queries[] = " |
||
| 177 | SELECT 'live' as toolname ,count(*) as count |
||
| 178 | from $rev |
||
| 179 | WHERE rev_user_text = :username |
||
| 180 | $cond_begin |
||
| 181 | $cond_end |
||
| 182 | "; |
||
| 183 | |||
| 184 | $cond_begin = str_replace("rev_timestamp", "ar_timestamp", $cond_begin); |
||
| 185 | $cond_end = str_replace("rev_timestamp", "ar_timestamp", $cond_end); |
||
| 186 | |||
| 187 | $queries[] = " |
||
| 188 | SELECT 'deleted' as toolname, count(*) as count |
||
| 189 | from $arc |
||
| 190 | WHERE ar_user_text = :username |
||
| 191 | $cond_begin |
||
| 192 | $cond_end |
||
| 193 | "; |
||
| 194 | |||
| 195 | // Create a big query and execute. |
||
| 196 | $stmt = implode(" UNION ", $queries); |
||
| 197 | |||
| 198 | $sth = $dbh->prepare($stmt); |
||
| 199 | |||
| 200 | $sth->bindParam("username", $username); |
||
| 201 | $sth->bindParam("start", $start); |
||
| 202 | $sth->bindParam("end", $end); |
||
| 203 | |||
| 204 | $sth->execute(); |
||
| 205 | |||
| 206 | // handling results |
||
| 207 | $results = []; |
||
| 208 | $total_semi = 0; |
||
| 209 | $total = 0; |
||
| 210 | |||
| 211 | while ($row = $sth->fetch()) { |
||
| 212 | // Different variables need to get set if the tool is |
||
| 213 | // the live edits or deleted edits. |
||
| 214 | // If it is neither and greater than 0, |
||
| 215 | // add them to the array we're rendering and to our running total |
||
| 216 | if ($row["toolname"] == "live") { |
||
| 217 | $total += $row["count"]; |
||
| 218 | } elseif ($row["toolname"] == "deleted") { |
||
| 219 | $total += $row["count"]; |
||
| 220 | } elseif ($row["count"] > 0) { |
||
| 221 | $results[$row["toolname"]] = $row["count"]; |
||
| 222 | $total_semi = $total_semi+$row["count"]; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | // Sort the array and do some simple math. |
||
| 227 | arsort($results); |
||
| 228 | |||
| 229 | if ($total != 0) { |
||
| 230 | $total_pct = ($total_semi / $total) * 100; |
||
| 231 | } else { |
||
| 232 | $total_pct = 0; |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | // Render the view with all variables set. |
||
| 237 | return $this->render('autoEdits/result.html.twig', [ |
||
| 238 | 'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'), |
||
| 239 | //"xtPageTitle" => "autoedits", |
||
|
1 ignored issue
–
show
|
|||
| 240 | 'xtPage' => "autoedits", |
||
| 241 | "username" => $username, |
||
| 242 | "url" => $url, |
||
| 243 | "wikiName" => $wikiName, |
||
| 244 | "semi_automated" => $results, |
||
| 245 | "start" => date("Y-m-d", strtotime($start)), |
||
| 246 | "end" => date("Y-m-d", strtotime($end)), |
||
| 247 | "total_semi" => $total_semi, |
||
| 248 | "total" => $total, |
||
| 249 | "total_pct" => $total_pct, |
||
| 250 | |||
| 251 | ]); |
||
| 252 | } |
||
| 253 | } |
||
| 254 |
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.