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 |
||
45 | class AnalyticsExportController extends AbstractController |
||
46 | { |
||
47 | /** @var Cache */ |
||
48 | protected $cacheProvider; |
||
49 | |||
50 | /** @var RepositoryInterface */ |
||
51 | protected $analyticsReportRepository; |
||
52 | |||
53 | /** @var Filesystem */ |
||
54 | protected $filesystem; |
||
55 | |||
56 | /** @var CsvReportFileLocationResolver */ |
||
57 | protected $csvReportFileLocationResolver; |
||
58 | |||
59 | /** @var CachedTenantContextInterface */ |
||
60 | protected $cachedTenantContext; |
||
61 | |||
62 | public function __construct( |
||
75 | |||
76 | /** |
||
77 | * @Operation( |
||
78 | * tags={"export"}, |
||
79 | * summary="Export analytics data", |
||
80 | * @SWG\Parameter( |
||
81 | * name="start", |
||
82 | * in="query", |
||
83 | * description="Export start date, e.g. 20150101", |
||
84 | * required=false, |
||
85 | * type="string" |
||
86 | * ), |
||
87 | * @SWG\Parameter( |
||
88 | * name="end", |
||
89 | * in="query", |
||
90 | * description="Export end date, e.g. 20160101", |
||
91 | * required=false, |
||
92 | * type="string" |
||
93 | * ), |
||
94 | * @SWG\Response( |
||
95 | * response="200", |
||
96 | * description="Returned on success." |
||
97 | * ) |
||
98 | * ) |
||
99 | * |
||
100 | * @Route("/api/{version}/export/analytics", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_core_analytics_export_post") |
||
101 | * |
||
102 | * @return SingleResourceResponse |
||
103 | * |
||
104 | * @throws \Exception |
||
105 | */ |
||
106 | public function post(Request $request): SingleResourceResponse |
||
128 | |||
129 | /** |
||
130 | * @Operation( |
||
131 | * tags={"export"}, |
||
132 | * summary="Lists analytics reports", |
||
133 | * @SWG\Parameter( |
||
134 | * name="sorting", |
||
135 | * in="query", |
||
136 | * description="example: [createdAt]=asc|desc", |
||
137 | * required=false, |
||
138 | * type="string" |
||
139 | * ), |
||
140 | * @SWG\Response( |
||
141 | * response="200", |
||
142 | * description="Returned on success.", |
||
143 | * @SWG\Schema( |
||
144 | * type="array", |
||
145 | * @SWG\Items(ref=@Model(type=\SWP\Bundle\CoreRoute\Model\AnalyticsReport::class, groups={"api"})) |
||
146 | * ) |
||
147 | * ) |
||
148 | * ) |
||
149 | * |
||
150 | * @Route("/api/{version}/export/analytics", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_core_list_analytics_reports") |
||
151 | */ |
||
152 | View Code Duplication | public function listAction(Request $request) |
|
|
|||
153 | { |
||
154 | $redirectRoutes = $this->analyticsReportRepository->getPaginatedByCriteria( |
||
155 | new Criteria(), |
||
156 | $request->query->get('sorting', []), |
||
157 | new PaginationData($request) |
||
158 | ); |
||
159 | |||
160 | return new ResourcesListResponse($redirectRoutes); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @Route("/analytics/export/{fileName}", methods={"GET"}, options={"expose"=true}, requirements={"mediaId"=".+"}, name="swp_export_analytics_download") |
||
165 | */ |
||
166 | public function downloadFile(string $fileName): Response |
||
198 | } |
||
199 |
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.