Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 26 | class TopEditsController extends XtoolsController |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get the tool's shortname. |
||
| 31 | * @return string |
||
| 32 | * @codeCoverageIgnore |
||
| 33 | */ |
||
| 34 | public function getToolShortname() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Display the form. |
||
| 41 | * @Route("/topedits", name="topedits") |
||
| 42 | * @Route("/topedits", name="TopEdits") |
||
| 43 | * @Route("/topedits/", name="topEditsSlash") |
||
| 44 | * @Route("/topedits/index.php", name="TopEditsIndex") |
||
| 45 | * @Route("/topedits/{project}", name="TopEditsProject") |
||
| 46 | * @param Request $request |
||
| 47 | * @return Response |
||
| 48 | */ |
||
| 49 | View Code Duplication | public function indexAction(Request $request) |
|
|
|
|||
| 50 | { |
||
| 51 | $params = $this->parseQueryParams($request); |
||
| 52 | |||
| 53 | // Redirect if at minimum project and username are provided. |
||
| 54 | if (isset($params['project']) && isset($params['username'])) { |
||
| 55 | return $this->redirectToRoute('TopEditsResults', $params); |
||
| 56 | } |
||
| 57 | |||
| 58 | // Convert the given project (or default project) into a Project instance. |
||
| 59 | $params['project'] = $this->getProjectFromQuery($params); |
||
| 60 | |||
| 61 | return $this->render('topedits/index.html.twig', array_merge([ |
||
| 62 | 'xtPageTitle' => 'tool-topedits', |
||
| 63 | 'xtSubtitle' => 'tool-topedits-desc', |
||
| 64 | 'xtPage' => 'topedits', |
||
| 65 | |||
| 66 | // Defaults that will get overriden if in $params. |
||
| 67 | 'namespace' => 0, |
||
| 68 | 'article' => '', |
||
| 69 | ], $params)); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Display the results. |
||
| 74 | * @Route("/topedits/{project}/{username}/{namespace}/{article}", name="TopEditsResults", |
||
| 75 | * requirements={"article"=".+"}) |
||
| 76 | * @param Request $request The HTTP request. |
||
| 77 | * @param int $namespace |
||
| 78 | * @param string $article |
||
| 79 | * @return RedirectResponse|Response |
||
| 80 | * @codeCoverageIgnore |
||
| 81 | */ |
||
| 82 | public function resultAction(Request $request, $namespace = 0, $article = '') |
||
| 83 | { |
||
| 84 | // Second parameter causes it return a Redirect to the index if the user has too many edits. |
||
| 85 | $ret = $this->validateProjectAndUser($request, 'topedits'); |
||
| 86 | if ($ret instanceof RedirectResponse) { |
||
| 87 | return $ret; |
||
| 88 | } else { |
||
| 89 | list($projectData, $user) = $ret; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($article === '') { |
||
| 93 | return $this->namespaceTopEdits($request, $user, $projectData, $namespace); |
||
| 94 | } else { |
||
| 95 | return $this->singlePageTopEdits($user, $projectData, $namespace, $article); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * List top edits by this user for all pages in a particular namespace. |
||
| 101 | * @param Request $request The HTTP request. |
||
| 102 | * @param User $user The User. |
||
| 103 | * @param Project $project The project. |
||
| 104 | * @param integer|string $namespace The namespace ID or 'all' |
||
| 105 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 106 | * @codeCoverageIgnore |
||
| 107 | */ |
||
| 108 | public function namespaceTopEdits(Request $request, User $user, Project $project, $namespace) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * List top edits by this user for a particular page. |
||
| 157 | * @param User $user The user. |
||
| 158 | * @param Project $project The project. |
||
| 159 | * @param int $namespaceId The ID of the namespace of the page. |
||
| 160 | * @param string $pageName The title (without namespace) of the page. |
||
| 161 | * @return RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 162 | * @codeCoverageIgnore |
||
| 163 | */ |
||
| 164 | protected function singlePageTopEdits(User $user, Project $project, $namespaceId, $pageName) |
||
| 204 | } |
||
| 205 |
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.