| Conditions | 12 | 
| Paths | 67 | 
| Total Lines | 123 | 
| Code Lines | 62 | 
| 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 | ||
| 208 | public function getRevisions( | ||
| 209 | array $dbNames, | ||
| 210 | User $user, | ||
| 211 | $namespace = 'all', | ||
| 212 | $start = false, | ||
| 213 | $end = false, | ||
| 214 | int $limit = 31, // One extra to know whether there should be another page. | ||
| 215 | $offset = false | ||
| 216 |     ): array { | ||
| 217 | // Check cache. | ||
| 218 | $cacheKey = $this->getCacheKey(func_get_args(), 'gc_revisions'); | ||
| 219 |         if ($this->cache->hasItem($cacheKey)) { | ||
| 220 | return $this->cache->getItem($cacheKey)->get(); | ||
| 221 | } | ||
| 222 | |||
| 223 | // Just need any Connection to use the ->quote() method. | ||
| 224 |         $quoteConn = $this->getProjectsConnection('s1'); | ||
| 225 | $username = $quoteConn->quote($user->getUsername(), PDO::PARAM_STR); | ||
| 226 | |||
| 227 | // IP range handling. | ||
| 228 | $startIp = ''; | ||
| 229 | $endIp = ''; | ||
| 230 |         if ($user->isIpRange()) { | ||
| 231 | [$startIp, $endIp] = IPUtils::parseRange($user->getUsername()); | ||
| 232 | $startIp = $quoteConn->quote($startIp, PDO::PARAM_STR); | ||
| 233 | $endIp = $quoteConn->quote($endIp, PDO::PARAM_STR); | ||
| 234 | } | ||
| 235 | |||
| 236 | // Fetch actor IDs (for IP ranges, it strips trailing zeros and uses a LIKE query). | ||
| 237 | $actorIds = $this->getDbNamesAndActorIds($user, $dbNames); | ||
| 238 | |||
| 239 |         if (!$actorIds) { | ||
| 240 | return []; | ||
| 241 | } | ||
| 242 | |||
| 243 | $namespaceCond = 'all' === $namespace | ||
| 244 | ? '' | ||
| 245 | : 'AND page_namespace = '.(int)$namespace; | ||
| 246 | $revDateConditions = $this->getDateConditions($start, $end, $offset, 'revs.', 'rev_timestamp'); | ||
| 247 | |||
| 248 | // Assemble queries. | ||
| 249 | $queriesBySlice = []; | ||
| 250 | $projectRepo = $this->caProject->getRepository(); | ||
| 251 |         foreach ($dbNames as $dbName) { | ||
| 252 |             if (isset($actorIds[$dbName])) { | ||
| 253 | $revisionTable = $projectRepo->getTableName($dbName, 'revision'); | ||
| 254 | $pageTable = $projectRepo->getTableName($dbName, 'page'); | ||
| 255 | $commentTable = $projectRepo->getTableName($dbName, 'comment', 'revision'); | ||
| 256 | $actorTable = $projectRepo->getTableName($dbName, 'actor', 'revision'); | ||
| 257 | $tagTable = $projectRepo->getTableName($dbName, 'change_tag'); | ||
| 258 | $tagDefTable = $projectRepo->getTableName($dbName, 'change_tag_def'); | ||
| 259 | |||
| 260 |                 if ($user->isIpRange()) { | ||
| 261 | $ipcTable = $projectRepo->getTableName($dbName, 'ip_changes'); | ||
| 262 | $ipcJoin = "JOIN $ipcTable ON revs.rev_id = ipc_rev_id"; | ||
| 263 | $whereClause = "ipc_hex BETWEEN $startIp AND $endIp"; | ||
| 264 | $username = 'actor_name'; | ||
| 265 |                 } else { | ||
| 266 | $ipcJoin = ''; | ||
| 267 | $whereClause = 'revs.rev_actor = '.$actorIds[$dbName]; | ||
| 268 | } | ||
| 269 | |||
| 270 | $slice = $this->getDbList()[$dbName]; | ||
| 271 | $queriesBySlice[$slice][] = " | ||
| 272 | SELECT | ||
| 273 | '$dbName' AS dbName, | ||
| 274 | revs.rev_id AS id, | ||
| 275 | revs.rev_timestamp AS timestamp, | ||
| 276 | UNIX_TIMESTAMP(revs.rev_timestamp) AS unix_timestamp, | ||
| 277 | revs.rev_minor_edit AS minor, | ||
| 278 | revs.rev_deleted AS deleted, | ||
| 279 | revs.rev_len AS length, | ||
| 280 | (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change, | ||
| 281 | revs.rev_parent_id AS parent_id, | ||
| 282 | $username AS username, | ||
| 283 | page.page_title, | ||
| 284 | page.page_namespace, | ||
| 285 | comment_text AS comment, | ||
| 286 | ( | ||
| 287 | SELECT 1 | ||
| 288 | FROM $tagTable | ||
| 289 | WHERE ct_rev_id = revs.rev_id | ||
| 290 | AND ct_tag_id = ( | ||
| 291 | SELECT ctd_id | ||
| 292 | FROM $tagDefTable | ||
| 293 | WHERE ctd_name = 'mw-reverted' | ||
| 294 | ) | ||
| 295 | LIMIT 1 | ||
| 296 | ) AS reverted | ||
| 297 | FROM $revisionTable AS revs | ||
| 298 | $ipcJoin | ||
| 299 | JOIN $pageTable AS page ON (rev_page = page_id) | ||
| 300 | JOIN $actorTable ON (actor_id = revs.rev_actor) | ||
| 301 | LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) | ||
| 302 | LEFT OUTER JOIN $commentTable ON revs.rev_comment_id = comment_id | ||
| 303 | WHERE $whereClause | ||
| 304 | $namespaceCond | ||
| 305 | $revDateConditions"; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | // Re-assemble into UNIONed queries, executing as many per slice as possible. | ||
| 310 | $revisions = []; | ||
| 311 |         foreach ($queriesBySlice as $slice => $queries) { | ||
| 312 |             $sql = "SELECT * FROM ((\n" . join("\n) UNION (\n", $queries) . ")) a ORDER BY timestamp DESC LIMIT $limit"; | ||
| 313 | $revisions = array_merge($revisions, $this->executeProjectsQuery($slice, $sql)->fetchAll()); | ||
| 314 | } | ||
| 315 | |||
| 316 | // If there are more than $limit results, re-sort by timestamp. | ||
| 317 |         if (count($revisions) > $limit) { | ||
| 318 |             usort($revisions, function ($a, $b) { | ||
| 319 |                 if ($a['unix_timestamp'] === $b['unix_timestamp']) { | ||
| 320 | return 0; | ||
| 321 | } | ||
| 322 | return $a['unix_timestamp'] > $b['unix_timestamp'] ? -1 : 1; | ||
| 323 | }); | ||
| 324 | |||
| 325 | // Truncate size to $limit. | ||
| 326 | $revisions = array_slice($revisions, 0, $limit); | ||
| 327 | } | ||
| 328 | |||
| 329 | // Cache and return. | ||
| 330 | return $this->setCache($cacheKey, $revisions); | ||
| 331 | } | ||
| 333 |