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 |
||
| 22 | class SupportController extends AbstractController |
||
| 23 | { |
||
| 24 | /** @var Connection */ |
||
| 25 | private $connection; |
||
| 26 | |||
| 27 | /** @var CacheReportsRepository */ |
||
| 28 | private $cacheReportsRepository; |
||
| 29 | |||
| 30 | /** @var CacheStatusModifiedRepository */ |
||
| 31 | private $cacheStatusModifiedRepository; |
||
| 32 | |||
| 33 | /** @var CacheStatusRepository */ |
||
| 34 | private $cacheStatusRepository; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * SupportController constructor. |
||
| 38 | * |
||
| 39 | * @param Connection $connection |
||
| 40 | * @param CacheReportsRepository $cacheReportsRepository |
||
| 41 | * @param CacheStatusModifiedRepository $cacheStatusModifiedRepository |
||
| 42 | * @param CacheStatusRepository $cacheStatusRepository |
||
| 43 | */ |
||
| 44 | public function __construct( |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return Response |
||
| 58 | * @Route("/support", name="support_index") |
||
| 59 | */ |
||
| 60 | public function index() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return Response |
||
| 68 | * @Route("/supportSearch", name="support_search") |
||
| 69 | */ |
||
| 70 | public function serchCachesAndUser() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return Response |
||
| 78 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 79 | * @Route("/reportedCaches", name="support_reported_caches") |
||
| 80 | */ |
||
| 81 | public function listReportedCaches() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param Request $request |
||
| 91 | * |
||
| 92 | * @return Response |
||
| 93 | * @Route("/dbQueries", name="support_db_queries") |
||
| 94 | */ |
||
| 95 | public function listDbQueries(Request $request) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $repID |
||
| 126 | * |
||
| 127 | * @return Response |
||
| 128 | * @throws \Oc\Repository\Exception\RecordNotFoundException |
||
| 129 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 130 | * @Route("/repCaches/{repID}", name="support_reported_cache") |
||
| 131 | */ |
||
| 132 | public function list_reported_cache_details(string $repID) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return array |
||
| 151 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 152 | */ |
||
| 153 | public function getReportedCaches() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param int $days |
||
| 161 | * |
||
| 162 | * @return Response |
||
| 163 | * @Route("/dbQueries1/{days}", name="support_db_queries_1") |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function executeSQL_caches_old_reg_date(int $days = 31) // List caches from users whose registration date is not older than x days. |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param int $days |
||
| 182 | * |
||
| 183 | * @return Response |
||
| 184 | * @Route("/dbQueries2/{days}", name="support_db_queries_2") |
||
| 185 | */ |
||
| 186 | public function executeSQL_old_reg_date(int $days) // List user whose registration date is no older than x days. |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return Response |
||
| 201 | * @Route("/dbQueries4", name="support_db_queries_4") |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function executeSQL_caches_old_login_date( |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $what |
||
| 222 | * @param string $table |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function executeSQL_flexible(string $what, string $table) |
||
| 234 | } |
||
| 235 |
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.