| Conditions | 4 |
| Paths | 5 |
| Total Lines | 53 |
| Code Lines | 27 |
| 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 |
||
| 81 | public function getTopEditsAllNamespaces(Project $project, User $user, $limit = 10) |
||
| 82 | { |
||
| 83 | // Set up cache. |
||
| 84 | $cacheKey = $this->getCacheKey(func_get_args(), 'topedits_all'); |
||
| 85 | if ($this->cache->hasItem($cacheKey)) { |
||
| 86 | return $this->cache->getItem($cacheKey)->get(); |
||
| 87 | } |
||
| 88 | |||
| 89 | $pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
||
| 90 | $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
||
| 91 | $hasPageAssessments = $this->isLabs() && $project->hasPageAssessments(); |
||
| 92 | $pageAssessmentsTable = $this->getTableName($project->getDatabaseName(), 'page_assessments'); |
||
| 93 | $paSelect = $hasPageAssessments |
||
| 94 | ? ", ( |
||
| 95 | SELECT pa_class |
||
| 96 | FROM $pageAssessmentsTable |
||
| 97 | WHERE pa_page_id = e.page_id |
||
| 98 | LIMIT 1 |
||
| 99 | ) AS pa_class" |
||
| 100 | : ', NULL as pa_class'; |
||
| 101 | |||
| 102 | $sql = "SELECT c.page_namespace, e.page_title, c.count $paSelect |
||
| 103 | FROM |
||
| 104 | ( |
||
| 105 | SELECT b.page_namespace, b.rev_page, b.count |
||
| 106 | ,@rn := if(@ns = b.page_namespace, @rn + 1, 1) AS row_number |
||
| 107 | ,@ns := b.page_namespace AS dummy |
||
| 108 | FROM |
||
| 109 | ( |
||
| 110 | SELECT page_namespace, rev_page, count(rev_page) AS count |
||
| 111 | FROM $revisionTable |
||
| 112 | JOIN $pageTable ON page_id = rev_page |
||
| 113 | WHERE rev_user_text = :username |
||
| 114 | GROUP BY page_namespace, rev_page |
||
| 115 | ) AS b |
||
| 116 | JOIN (SELECT @ns := NULL, @rn := 0) AS vars |
||
| 117 | ORDER BY b.page_namespace ASC, b.count DESC |
||
| 118 | ) AS c |
||
| 119 | JOIN $pageTable e ON e.page_id = c.rev_page |
||
| 120 | WHERE c.row_number < $limit"; |
||
| 121 | $resultQuery = $this->getProjectsConnection()->prepare($sql); |
||
| 122 | $username = $user->getUsername(); |
||
| 123 | $resultQuery->bindParam('username', $username); |
||
| 124 | $resultQuery->execute(); |
||
| 125 | $results = $resultQuery->fetchAll(); |
||
| 126 | |||
| 127 | // Cache for 10 minutes, and return. |
||
| 128 | $cacheItem = $this->cache->getItem($cacheKey) |
||
| 129 | ->set($results) |
||
| 130 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 131 | $this->cache->save($cacheItem); |
||
| 132 | |||
| 133 | return $results; |
||
| 134 | } |
||
| 150 |