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 |
||
| 10 | class RequestParametersExtractor |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Extract parameters for the load method from the Request. |
||
| 14 | * |
||
| 15 | * @param Request $request |
||
| 16 | * @param ActionConfiguration $configuration |
||
| 17 | * @param FormInterface|null $form |
||
| 18 | * |
||
| 19 | * @return array |
||
| 20 | */ |
||
| 21 | public function extract(Request $request, ActionConfiguration $configuration, FormInterface $form = null) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Get the criteria values. |
||
| 40 | * |
||
| 41 | * @param Request $request |
||
| 42 | * @param ActionConfiguration $configuration |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | private function getCriteriaValues(Request $request, ActionConfiguration $configuration) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get the sort and order values. |
||
| 64 | * |
||
| 65 | * @param Request $request |
||
| 66 | * @param ActionConfiguration $configuration |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | private function getOrderValues(Request $request, ActionConfiguration $configuration) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the pagination values. |
||
| 90 | * |
||
| 91 | * @param Request $request |
||
| 92 | * @param ActionConfiguration $configuration |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | private function getPaginationValues(Request $request, ActionConfiguration $configuration) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Handle the value of the filter form. |
||
| 120 | * |
||
| 121 | * @param Request $request |
||
| 122 | * @param FormInterface|null $form |
||
| 123 | * |
||
| 124 | * @return array|mixed |
||
| 125 | */ |
||
| 126 | private function getFilterValues(Request $request, FormInterface $form = null) |
||
| 158 | } |
||
| 159 |
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.