Conditions | 7 |
Paths | 13 |
Total Lines | 83 |
Code Lines | 54 |
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 |
||
133 | protected function namespaceTopEdits(User $user, Project $project, $namespaceId) |
||
134 | { |
||
135 | // Make sure they've opted in to see this data. |
||
136 | if (!$project->userHasOptedIn($user)) { |
||
137 | $optedInPage = $project |
||
1 ignored issue
–
show
|
|||
138 | ->getRepository() |
||
139 | ->getPage($project, $project->userOptInPage($user)); |
||
140 | return $this->render('topedits/result_namespace.html.twig', [ |
||
141 | 'xtPage' => 'topedits', |
||
142 | 'project' => $project, |
||
143 | 'user' => $user, |
||
144 | 'namespace' => $namespaceId, |
||
145 | 'edits' => [], |
||
146 | 'content_title' => '', |
||
147 | 'opted_in_page' => $optedInPage, |
||
148 | ]); |
||
149 | } |
||
150 | |||
151 | // Get list of namespaces. |
||
152 | $namespaces = $project->getNamespaces(); |
||
153 | |||
154 | // Get the basic data about the pages edited by this user. |
||
155 | $params = ['username'=>$user->getUsername()]; |
||
156 | $nsClause = ''; |
||
157 | $namespaceMsg = 'all-namespaces'; |
||
158 | if (is_numeric($namespaceId)) { |
||
159 | $nsClause = 'AND page_namespace = :namespace'; |
||
160 | $params['namespace'] = $namespaceId; |
||
161 | $namespaceMsg = str_replace(' ', '_', strtolower($namespaces[$namespaceId])); |
||
162 | } |
||
163 | $revTable = $this->lh->getTable('revision', $project->getDatabaseName()); |
||
164 | $pageTable = $this->lh->getTable('page', $project->getDatabaseName()); |
||
165 | $query = "SELECT page_namespace, page_title, page_is_redirect, COUNT(page_title) AS count |
||
166 | FROM $pageTable JOIN $revTable ON page_id = rev_page |
||
167 | WHERE rev_user_text = :username $nsClause |
||
168 | GROUP BY page_namespace, page_title |
||
169 | ORDER BY count DESC |
||
170 | LIMIT 100"; |
||
171 | $conn = $this->getDoctrine()->getManager('replicas')->getConnection(); |
||
172 | $editData = $conn->executeQuery($query, $params)->fetchAll(); |
||
173 | |||
174 | // Inform user if no revisions found. |
||
175 | if (count($editData) === 0) { |
||
176 | $this->addFlash("notice", ["no-contribs"]); |
||
177 | } |
||
178 | |||
179 | // Get page info about these 100 pages, so we can use their display title. |
||
180 | $titles = array_map(function ($e) use ($namespaces) { |
||
181 | // If non-mainspace, prepend namespace to the titles. |
||
182 | $ns = $e['page_namespace']; |
||
183 | $nsTitle = $ns > 0 ? $namespaces[$e['page_namespace']] . ':' : ''; |
||
184 | return $nsTitle . $e['page_title']; |
||
185 | }, $editData); |
||
186 | /** @var ApiHelper $apiHelper */ |
||
187 | $apiHelper = $this->get('app.api_helper'); |
||
188 | $displayTitles = $apiHelper->displayTitles($project->getDomain(), $titles); |
||
189 | |||
190 | // Create page repo to be used in page objects |
||
191 | $pageRepo = new PagesRepository(); |
||
192 | $pageRepo->setContainer($this->container); |
||
1 ignored issue
–
show
|
|||
193 | |||
194 | // Put all together, and return the view. |
||
195 | $edits = []; |
||
196 | foreach ($editData as $editDatum) { |
||
197 | // If non-mainspace, prepend namespace to the titles. |
||
198 | $ns = $editDatum['page_namespace']; |
||
199 | $nsTitle = $ns > 0 ? $namespaces[$editDatum['page_namespace']] . ':' : ''; |
||
200 | $pageTitle = $nsTitle . $editDatum['page_title']; |
||
201 | $editDatum['displaytitle'] = $displayTitles[$pageTitle]; |
||
202 | // $editDatum['page_title'] is retained without the namespace |
||
203 | // so we can link to TopEdits for that page |
||
204 | $editDatum['page_title_ns'] = $pageTitle; |
||
205 | $edits[] = $editDatum; |
||
206 | } |
||
207 | return $this->render('topedits/result_namespace.html.twig', [ |
||
208 | 'xtPage' => 'topedits', |
||
209 | 'project' => $project, |
||
210 | 'user' => $user, |
||
211 | 'namespace' => $namespaceId, |
||
212 | 'edits' => $edits, |
||
213 | 'content_title' => $namespaceMsg, |
||
214 | ]); |
||
215 | } |
||
216 | |||
270 |
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.