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 |
||
21 | class AuthenticatedController extends AbstractController |
||
22 | { |
||
23 | /** |
||
24 | * @var SessionService |
||
25 | */ |
||
26 | protected $sessionService; |
||
27 | |||
28 | /** |
||
29 | * @var PaypalService |
||
30 | */ |
||
31 | protected $paypalService; |
||
32 | |||
33 | /** |
||
34 | * DefaultController constructor. |
||
35 | * @param SessionService $sessionService |
||
36 | * @param PaypalService $paypalService |
||
37 | */ |
||
38 | public function __construct(SessionService $sessionService, PaypalService $paypalService) |
||
43 | |||
44 | /** |
||
45 | * @Route("/logged-in/account", name="account") |
||
46 | * |
||
47 | * @return Response | RedirectResponse |
||
48 | */ |
||
49 | public function account() |
||
73 | |||
74 | /** |
||
75 | * @Route("/logged-in/payments", name="payments") |
||
76 | * |
||
77 | * @return Response | RedirectResponse |
||
78 | */ |
||
79 | public function payments() |
||
90 | |||
91 | /** |
||
92 | * @Route("/logged-in/payments/capture/{paymentId}", name="payments-capture") |
||
93 | * @param string $paymentId |
||
94 | * @return Response | RedirectResponse |
||
95 | */ |
||
96 | View Code Duplication | public function paymentsCapture(string $paymentId) |
|
108 | |||
109 | /** |
||
110 | * @Route("/logged-in/payouts", name="payouts") |
||
111 | * |
||
112 | * @return Response | RedirectResponse |
||
113 | */ |
||
114 | public function payouts() |
||
121 | |||
122 | /** |
||
123 | * @Route("/logged-in/payouts/create", name="payouts-create") |
||
124 | * |
||
125 | * @return Response | RedirectResponse |
||
126 | */ |
||
127 | public function payoutsCreate() |
||
148 | |||
149 | /** |
||
150 | * @Route("/logged-in/payouts/{statusId}", name="payouts-refresh") |
||
151 | * @param string $statusId |
||
152 | * @return Response | RedirectResponse |
||
153 | */ |
||
154 | View Code Duplication | public function payoutsRefresh(string $statusId) |
|
166 | |||
167 | /** |
||
168 | * @Route("/logged-in/invoices", name="invoices") |
||
169 | * |
||
170 | * @return Response | RedirectResponse |
||
171 | */ |
||
172 | public function invoices() |
||
179 | |||
180 | /** |
||
181 | * @Route("/logged-in/invoices/create", name="invoices-create") |
||
182 | * |
||
183 | * @return Response | RedirectResponse |
||
184 | */ |
||
185 | public function invoicesCreate() |
||
208 | |||
209 | /** |
||
210 | * @Route("/logged-in/invoices/{invoiceId}", name="invoices-refresh") |
||
211 | * @param string $invoiceId |
||
212 | * @return Response | RedirectResponse |
||
213 | */ |
||
214 | View Code Duplication | public function invoicesRefresh(string $invoiceId) |
|
226 | |||
227 | /** |
||
228 | * @Route("/logged-in/subscriptions", name="subscriptions") |
||
229 | * |
||
230 | * @return Response | RedirectResponse |
||
231 | */ |
||
232 | public function subscriptions() |
||
239 | } |
||
240 |
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.