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 |
||
| 27 | class EditController extends Controller |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Main controller action |
||
| 31 | * |
||
| 32 | * @param Request $request |
||
| 33 | * @param $source |
||
| 34 | * @param $rateType |
||
| 35 | * @param $currencyCode |
||
| 36 | * @param \DateTime $date |
||
|
|
|||
| 37 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 38 | */ |
||
| 39 | 6 | public function indexAction(Request $request) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Handle form submission. |
||
| 75 | * |
||
| 76 | * @param Form $form |
||
| 77 | * @param Request $request |
||
| 78 | * @param RateInterface $rate |
||
| 79 | * |
||
| 80 | * @return bool TRUE if successful |
||
| 81 | */ |
||
| 82 | 4 | protected function handleForm(Form $form, Request $request, RateInterface $exchangeRate) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Get FQCN of FormType form. |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 4 | protected function getFormType() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Get form. |
||
| 115 | * |
||
| 116 | * @return Form |
||
| 117 | */ |
||
| 118 | 4 | protected function getForm(RateInterface $rate) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Save rate. |
||
| 125 | * |
||
| 126 | * @param Rate $rate |
||
| 127 | * @return TRUE if successful. |
||
| 128 | */ |
||
| 129 | 2 | View Code Duplication | protected function save(Rate $rate) |
| 140 | |||
| 141 | /** |
||
| 142 | * Redirect after success. |
||
| 143 | * |
||
| 144 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 145 | */ |
||
| 146 | 1 | protected function redirectAfterSuccess() |
|
| 150 | } |
||
| 151 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.