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) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $repID |
||
| 127 | * |
||
| 128 | * @return Response |
||
| 129 | * @throws \Oc\Repository\Exception\RecordNotFoundException |
||
| 130 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 131 | * @Route("/repCaches/{repID}", name="support_reported_cache") |
||
| 132 | */ |
||
| 133 | public function list_reported_cache_details(string $repID) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return array |
||
| 152 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 153 | */ |
||
| 154 | public function getReportedCaches() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param int $days |
||
| 162 | * |
||
| 163 | * @return Response |
||
| 164 | * @Route("/dbQueries1/{days}", name="support_db_queries_1") |
||
| 165 | */ |
||
| 166 | 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. |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param int $days |
||
| 183 | * |
||
| 184 | * @return Response |
||
| 185 | * @Route("/dbQueries2/{days}", name="support_db_queries_2") |
||
| 186 | */ |
||
| 187 | public function executeSQL_old_reg_date(int $days) // List user whose registration date is no older than x days. |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return Response |
||
| 202 | * @Route("/dbQueries4", name="support_db_queries_4") |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function executeSQL_caches_old_login_date( |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $what |
||
| 223 | * @param string $table |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function executeSQL_flexible(string $what, string $table) |
||
| 235 | } |
||
| 236 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: