| Conditions | 12 |
| Paths | 223 |
| Total Lines | 112 |
| Code Lines | 74 |
| Lines | 18 |
| Ratio | 16.07 % |
| 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 |
||
| 76 | public function resultAction(Request $request, $namespace = '0', $redirects = 'noredirects') |
||
| 77 | { |
||
| 78 | $ret = $this->validateProjectAndUser($request, 'topedits'); |
||
| 79 | if ($ret instanceof RedirectResponse) { |
||
| 80 | return $ret; |
||
| 81 | } else { |
||
| 82 | list($projectData, $user) = $ret; |
||
| 83 | } |
||
| 84 | |||
| 85 | // what columns to show in namespace totals table |
||
| 86 | $summaryColumns = ['namespace']; |
||
| 87 | if ($redirects == 'onlyredirects') { |
||
| 88 | // don't show redundant pages column if only getting data on redirects |
||
| 89 | $summaryColumns[] = 'redirects'; |
||
| 90 | } elseif ($redirects == 'noredirects') { |
||
| 91 | // don't show redundant redirects column if only getting data on non-redirects |
||
| 92 | $summaryColumns[] = 'pages'; |
||
| 93 | } else { |
||
| 94 | // order is important here |
||
| 95 | $summaryColumns[] = 'pages'; |
||
| 96 | $summaryColumns[] = 'redirects'; |
||
| 97 | } |
||
| 98 | $summaryColumns[] = 'deleted'; // always show deleted column |
||
| 99 | |||
| 100 | $result = $user->getRepository()->getPagesCreated($projectData, $user, $namespace, $redirects); |
||
| 101 | |||
| 102 | $hasPageAssessments = $projectData->hasPageAssessments(); |
||
| 103 | $pagesByNamespaceByDate = []; |
||
| 104 | $pageTitles = []; |
||
| 105 | $countsByNamespace = []; |
||
| 106 | $total = 0; |
||
| 107 | $redirectTotal = 0; |
||
| 108 | $deletedTotal = 0; |
||
| 109 | |||
| 110 | foreach ($result as $row) { |
||
| 111 | $datetime = DateTime::createFromFormat('YmdHis', $row['rev_timestamp']); |
||
| 112 | $datetimeKey = $datetime->format('Ymdhi'); |
||
| 113 | $datetimeHuman = $datetime->format('Y-m-d H:i'); |
||
| 114 | |||
| 115 | $pageData = array_merge($row, [ |
||
| 116 | 'raw_time' => $row['rev_timestamp'], |
||
| 117 | 'human_time' => $datetimeHuman, |
||
| 118 | 'page_title' => str_replace('_', ' ', $row['page_title']) |
||
| 119 | ]); |
||
| 120 | |||
| 121 | if ($hasPageAssessments) { |
||
| 122 | $pageData['badge'] = $projectData->getAssessmentBadgeURL($pageData['pa_class']); |
||
| 123 | } |
||
| 124 | |||
| 125 | $pagesByNamespaceByDate[$row['namespace']][$datetimeKey][] = $pageData; |
||
| 126 | |||
| 127 | $pageTitles[] = $row['page_title']; |
||
| 128 | |||
| 129 | // Totals |
||
| 130 | if (isset($countsByNamespace[$row['namespace']]['total'])) { |
||
| 131 | $countsByNamespace[$row['namespace']]['total']++; |
||
| 132 | } else { |
||
| 133 | $countsByNamespace[$row['namespace']]['total'] = 1; |
||
| 134 | $countsByNamespace[$row['namespace']]['redirect'] = 0; |
||
| 135 | $countsByNamespace[$row['namespace']]['deleted'] = 0; |
||
| 136 | } |
||
| 137 | $total++; |
||
| 138 | |||
| 139 | View Code Duplication | if ($row['page_is_redirect']) { |
|
| 140 | $redirectTotal++; |
||
| 141 | // Redirects |
||
| 142 | if (isset($countsByNamespace[$row['namespace']]['redirect'])) { |
||
| 143 | $countsByNamespace[$row['namespace']]['redirect']++; |
||
| 144 | } else { |
||
| 145 | $countsByNamespace[$row['namespace']]['redirect'] = 1; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | View Code Duplication | if ($row['type'] === 'arc') { |
|
| 150 | $deletedTotal++; |
||
| 151 | // Deleted |
||
| 152 | if (isset($countsByNamespace[$row['namespace']]['deleted'])) { |
||
| 153 | $countsByNamespace[$row['namespace']]['deleted']++; |
||
| 154 | } else { |
||
| 155 | $countsByNamespace[$row['namespace']]['deleted'] = 1; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | ksort($pagesByNamespaceByDate); |
||
| 161 | ksort($countsByNamespace); |
||
| 162 | |||
| 163 | foreach (array_keys($pagesByNamespaceByDate) as $key) { |
||
| 164 | krsort($pagesByNamespaceByDate[$key]); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Retrieve the namespaces |
||
| 168 | $namespaces = $projectData->getNamespaces(); |
||
| 169 | |||
| 170 | // Assign the values and display the template |
||
| 171 | return $this->render('pages/result.html.twig', [ |
||
| 172 | 'xtPage' => 'pages', |
||
| 173 | 'xtTitle' => $user->getUsername(), |
||
| 174 | 'project' => $projectData, |
||
| 175 | 'user' => $user, |
||
| 176 | 'namespace' => $namespace, |
||
| 177 | 'redirect' => $redirects, |
||
| 178 | 'summaryColumns' => $summaryColumns, |
||
| 179 | 'namespaces' => $namespaces, |
||
| 180 | 'pages' => $pagesByNamespaceByDate, |
||
| 181 | 'count' => $countsByNamespace, |
||
| 182 | 'total' => $total, |
||
| 183 | 'redirectTotal' => $redirectTotal, |
||
| 184 | 'deletedTotal' => $deletedTotal, |
||
| 185 | 'hasPageAssessments' => $hasPageAssessments, |
||
| 186 | ]); |
||
| 187 | } |
||
| 188 | } |
||
| 189 |
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.