| Conditions | 14 |
| Paths | 78 |
| Total Lines | 131 |
| Code Lines | 84 |
| Lines | 18 |
| Ratio | 13.74 % |
| 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 |
||
| 91 | public function resultAction($project, $username) |
||
| 92 | { |
||
| 93 | $conn = $this->getDoctrine()->getManager('replicas')->getConnection(); |
||
|
|
|||
| 94 | |||
| 95 | $projectData = ProjectRepository::getProject($project, $this->container); |
||
| 96 | $projectRepo = $projectData->getRepository(); |
||
| 97 | $userData = new User($username); |
||
| 98 | $pagesRepo = new PagesRepository(); |
||
| 99 | $pagesRepo->setContainer($this->container); |
||
|
1 ignored issue
–
show
|
|||
| 100 | |||
| 101 | $dbName = $projectData->getDatabaseName(); |
||
| 102 | |||
| 103 | $rfxParam = $this->getParameter('rfx'); |
||
| 104 | |||
| 105 | View Code Duplication | if (!$projectData->exists() || $rfxParam == null) { |
|
| 106 | $this->addFlash('notice', ['invalid-project', $project]); |
||
| 107 | return $this->redirectToRoute('rfxvote'); |
||
| 108 | } |
||
| 109 | |||
| 110 | $namespaces = $projectData->getNamespaces(); |
||
| 111 | |||
| 112 | if (!isset($rfxParam[$projectData->getDomain()])) { |
||
| 113 | $this->addFlash('notice', ['invalid-project-cant-use', $project]); |
||
| 114 | return $this->redirectToRoute('rfxvote'); |
||
| 115 | } |
||
| 116 | |||
| 117 | $pageTypes = $rfxParam[$projectData->getDomain()]['pages']; |
||
| 118 | $namespace |
||
| 119 | = $rfxParam[$projectData->getDomain()]['rfx_namespace'] !== null |
||
| 120 | ? $rfxParam[$projectData->getDomain()]['rfx_namespace'] : 4; |
||
| 121 | |||
| 122 | $finalData = []; |
||
| 123 | |||
| 124 | // We should probably figure out a better way to do this... |
||
| 125 | $ignoredPages = ''; |
||
| 126 | |||
| 127 | View Code Duplication | if (isset($rfxParam[$projectData->getDomain()]['excluded_title'])) { |
|
| 128 | $titlesExcluded |
||
| 129 | = $rfxParam[$projectData->getDomain()]['excluded_title']; |
||
| 130 | foreach ($titlesExcluded as $ignoredPage) { |
||
| 131 | $ignoredPages .= "AND p.page_title != \"$ignoredPage\"\r\n"; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | View Code Duplication | if (isset($rfxParam[$projectData->getDomain()]['excluded_regex'])) { |
|
| 136 | $titlesExcluded |
||
| 137 | = $rfxParam[$projectData->getDomain()]['excluded_regex']; |
||
| 138 | foreach ($titlesExcluded as $ignoredPage) { |
||
| 139 | $ignoredPages .= "AND p.page_title NOT LIKE \"%$ignoredPage%\"\r\n"; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | foreach ($pageTypes as $type) { |
||
| 144 | $type = explode(':', $type, 2)[1]; |
||
| 145 | |||
| 146 | $type = str_replace(' ', '_', $type); |
||
| 147 | |||
| 148 | $pageTable = $projectRepo->getTableName($dbName, 'page'); |
||
| 149 | $revisionTable |
||
| 150 | = $projectRepo->getTableName($dbName, 'revision'); |
||
| 151 | |||
| 152 | $sql = "SELECT DISTINCT p.page_namespace, p.page_title |
||
| 153 | FROM $pageTable p |
||
| 154 | RIGHT JOIN $revisionTable r on p.page_id=r.rev_page |
||
| 155 | WHERE p.page_namespace = :namespace |
||
| 156 | AND r.rev_user_text = :username |
||
| 157 | And p.page_title LIKE \"$type/%\" |
||
| 158 | AND p.page_title NOT LIKE \"%$type/$username%\" |
||
| 159 | $ignoredPages"; |
||
| 160 | |||
| 161 | $sth = $conn->prepare($sql); |
||
| 162 | $sth->bindParam('namespace', $namespace); |
||
| 163 | $sth->bindParam('username', $username); |
||
| 164 | |||
| 165 | $sth->execute(); |
||
| 166 | |||
| 167 | $titles = []; |
||
| 168 | |||
| 169 | while ($row = $sth->fetch()) { |
||
| 170 | $titles[] = $namespaces[$row['page_namespace']] . |
||
| 171 | ':' .$row['page_title']; |
||
| 172 | } |
||
| 173 | |||
| 174 | // Chunking... it's possible to make a URI too long |
||
| 175 | $titleArray = array_chunk($titles, 20); |
||
| 176 | |||
| 177 | foreach ($titleArray as $titlesWorked) { |
||
| 178 | $pageData = $pagesRepo->getPagesWikitext($projectData, $titlesWorked); |
||
| 179 | |||
| 180 | foreach ($pageData as $title => $text) { |
||
| 181 | $type = str_replace('_', ' ', $type); |
||
| 182 | $rfx = new RFX( |
||
| 183 | $text, |
||
| 184 | $rfxParam[$projectData->getDomain()]['sections'], |
||
| 185 | $namespaces[2], |
||
| 186 | $rfxParam[$projectData->getDomain()]['date_regexp'], |
||
| 187 | $username |
||
| 188 | ); |
||
| 189 | $section = $rfx->getUserSectionFound(); |
||
| 190 | if ($section == '') { |
||
| 191 | // Skip over ones where the user didn't !vote. |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | // Todo: i18n-ize this |
||
| 195 | $finalData[$type][$section][$title]['Support'] |
||
| 196 | = sizeof($rfx->getSection('support')); |
||
| 197 | $finalData[$type][$section][$title]['Oppose'] |
||
| 198 | = sizeof($rfx->getSection('oppose')); |
||
| 199 | $finalData[$type][$section][$title]['Neutral'] |
||
| 200 | = sizeof($rfx->getSection('neutral')); |
||
| 201 | $finalData[$type][$section][$title]['Date'] |
||
| 202 | = $rfx->getEndDate(); |
||
| 203 | $finalData[$type][$section][$title]['name'] |
||
| 204 | = explode('/', $title)[1]; |
||
| 205 | |||
| 206 | unset($rfx); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return $this->render( |
||
| 212 | 'rfxVoteCalculator/result.html.twig', |
||
| 213 | [ |
||
| 214 | 'xtPage' => 'rfxvote', |
||
| 215 | 'xtTitle' => $username, |
||
| 216 | 'user' => $userData, |
||
| 217 | 'project' => $projectData, |
||
| 218 | 'data'=> $finalData, |
||
| 219 | ] |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | } |
||
| 223 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: