| Conditions | 14 |
| Paths | 93 |
| Total Lines | 142 |
| Code Lines | 86 |
| 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 |
||
| 63 | public function resultAction(): Response |
||
| 64 | { |
||
| 65 | /** @var Connection $conn */ |
||
| 66 | $conn = $this->getDoctrine()->getManager('replicas')->getConnection(); |
||
|
|
|||
| 67 | |||
| 68 | $projectRepo = $this->project->getRepository(); |
||
| 69 | $pageRepo = new PageRepository(); |
||
| 70 | $pageRepo->setContainer($this->container); |
||
| 71 | |||
| 72 | $dbName = $this->project->getDatabaseName(); |
||
| 73 | |||
| 74 | $rfxParam = $this->getParameter('rfx'); |
||
| 75 | |||
| 76 | $namespaces = $this->project->getNamespaces(); |
||
| 77 | |||
| 78 | if (!isset($rfxParam[$this->project->getDomain()])) { |
||
| 79 | $this->addFlashMessage('notice', 'invalid-project-cant-use', [$this->project->getDomain()]); |
||
| 80 | return $this->redirectToRoute('RfXVoteCalculator'); |
||
| 81 | } |
||
| 82 | |||
| 83 | $pageTypes = $rfxParam[$this->project->getDomain()]['pages']; |
||
| 84 | $namespace = $rfxParam[$this->project->getDomain()]['rfx_namespace'] ?? 4; |
||
| 85 | |||
| 86 | $finalData = []; |
||
| 87 | |||
| 88 | // We should probably figure out a better way to do this... |
||
| 89 | $ignoredPages = ''; |
||
| 90 | |||
| 91 | if (isset($rfxParam[$this->project->getDomain()]['excluded_title'])) { |
||
| 92 | $titlesExcluded |
||
| 93 | = $rfxParam[$this->project->getDomain()]['excluded_title']; |
||
| 94 | foreach ($titlesExcluded as $ignoredPage) { |
||
| 95 | $ignoredPages .= "AND p.page_title != \"$ignoredPage\"\r\n"; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (isset($rfxParam[$this->project->getDomain()]['excluded_regex'])) { |
||
| 100 | $titlesExcluded |
||
| 101 | = $rfxParam[$this->project->getDomain()]['excluded_regex']; |
||
| 102 | foreach ($titlesExcluded as $ignoredPage) { |
||
| 103 | $ignoredPages .= "AND p.page_title NOT LIKE \"%$ignoredPage%\"\r\n"; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Contains the total number of !votes the user made, keyed by the RfX |
||
| 109 | * type and then the vote type. |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | $totals = []; |
||
| 113 | |||
| 114 | foreach ($pageTypes as $type) { |
||
| 115 | $type = explode(':', $type, 2)[1]; |
||
| 116 | |||
| 117 | $type = str_replace(' ', '_', $type); |
||
| 118 | |||
| 119 | $pageTable = $projectRepo->getTableName($dbName, 'page'); |
||
| 120 | $revisionTable = $projectRepo->getTableName($dbName, 'revision'); |
||
| 121 | $username = $this->user->getUsername(); |
||
| 122 | |||
| 123 | $sql = "SELECT DISTINCT p.page_namespace, p.page_title |
||
| 124 | FROM $pageTable p |
||
| 125 | RIGHT JOIN $revisionTable r on p.page_id=r.rev_page |
||
| 126 | WHERE p.page_namespace = :namespace |
||
| 127 | AND r.rev_user_text = :username |
||
| 128 | And p.page_title LIKE \"$type/%\" |
||
| 129 | AND p.page_title NOT LIKE \"%$type/$username%\" |
||
| 130 | $ignoredPages"; |
||
| 131 | |||
| 132 | $sth = $conn->prepare($sql); |
||
| 133 | $sth->bindParam('namespace', $namespace); |
||
| 134 | $sth->bindParam('username', $username); |
||
| 135 | |||
| 136 | $sth->execute(); |
||
| 137 | |||
| 138 | $titles = []; |
||
| 139 | |||
| 140 | while ($row = $sth->fetch()) { |
||
| 141 | $titles[] = $namespaces[$row['page_namespace']] . |
||
| 142 | ':' .$row['page_title']; |
||
| 143 | } |
||
| 144 | |||
| 145 | // Chunking... it's possible to make a URI too long |
||
| 146 | $titleArray = array_chunk($titles, 20); |
||
| 147 | |||
| 148 | foreach ($titleArray as $titlesWorked) { |
||
| 149 | $pageData = $pageRepo->getPagesWikitext($this->project, $titlesWorked); |
||
| 150 | |||
| 151 | foreach ($pageData as $title => $text) { |
||
| 152 | $type = str_replace('_', ' ', $type); |
||
| 153 | $rfx = new RFX( |
||
| 154 | $text, |
||
| 155 | $rfxParam[$this->project->getDomain()]['sections'], |
||
| 156 | $namespaces[2], |
||
| 157 | $rfxParam[$this->project->getDomain()]['date_regexp'], |
||
| 158 | $username |
||
| 159 | ); |
||
| 160 | $section = $rfx->getUserSectionFound(); |
||
| 161 | |||
| 162 | if ('' == $section) { |
||
| 163 | // Skip over ones where the user didn't !vote. |
||
| 164 | continue; |
||
| 165 | } |
||
| 166 | |||
| 167 | if (!isset($totals[$type])) { |
||
| 168 | $totals[$type] = []; |
||
| 169 | } |
||
| 170 | if (!isset($totals[$type][$section])) { |
||
| 171 | $totals[$type][$section] = 0; |
||
| 172 | } |
||
| 173 | if (!isset($totals[$type]['total'])) { |
||
| 174 | $totals[$type]['total'] = 0; |
||
| 175 | } |
||
| 176 | $totals[$type][$section] += 1; |
||
| 177 | $totals[$type]['total'] += 1; |
||
| 178 | |||
| 179 | // Todo: i18n-ize this |
||
| 180 | $finalData[$type][$section][$title]['Support'] |
||
| 181 | = sizeof($rfx->getSection('support')); |
||
| 182 | $finalData[$type][$section][$title]['Oppose'] |
||
| 183 | = sizeof($rfx->getSection('oppose')); |
||
| 184 | $finalData[$type][$section][$title]['Neutral'] |
||
| 185 | = sizeof($rfx->getSection('neutral')); |
||
| 186 | $finalData[$type][$section][$title]['Date'] |
||
| 187 | = $rfx->getEnd(); |
||
| 188 | $finalData[$type][$section][$title]['name'] |
||
| 189 | = explode('/', $title)[1]; |
||
| 190 | |||
| 191 | unset($rfx); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | return $this->render( |
||
| 197 | 'rfxVoteCalculator/result.html.twig', |
||
| 198 | [ |
||
| 199 | 'xtPage' => 'RfXVoteCalculator', |
||
| 200 | 'xtTitle' => $this->user->getUsername(), |
||
| 201 | 'user' => $this->user, |
||
| 202 | 'project' => $this->project, |
||
| 203 | 'data'=> $finalData, |
||
| 204 | 'totals' => $totals, |
||
| 205 | ] |
||
| 209 |