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 |
||
25 | class CallbackController extends Controller |
||
26 | { |
||
27 | use DiactorosTrait; |
||
28 | |||
29 | /** |
||
30 | * @Method("POST") |
||
31 | * @Route("/form", name="loevgaard_dandomain_altapay_callback_form") |
||
32 | * |
||
33 | * @LogHttpTransaction() |
||
34 | * |
||
35 | * @param Request $request |
||
36 | * @throws CallbackException |
||
37 | * @throws PaymentException |
||
38 | * |
||
39 | * @return Response |
||
40 | */ |
||
41 | View Code Duplication | public function formAction(Request $request) |
|
|
|||
42 | { |
||
43 | $payment = $this->handleCallback($request); |
||
44 | $siteSettings = $this->getSiteSettings($payment); |
||
45 | |||
46 | return $this->render('@LoevgaardDandomainAltapay/callback/form.html.twig', [ |
||
47 | 'payment' => $payment, |
||
48 | 'siteSettings' => $siteSettings, |
||
49 | ]); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @Method("POST") |
||
54 | * @Route("/ok", name="loevgaard_dandomain_altapay_callback_ok") |
||
55 | * |
||
56 | * @LogHttpTransaction() |
||
57 | * |
||
58 | * @param Request $request |
||
59 | * @throws CallbackException |
||
60 | * @throws PaymentException |
||
61 | * @return RedirectResponse |
||
62 | */ |
||
63 | public function okAction(Request $request) |
||
74 | |||
75 | /** |
||
76 | * @Method("POST") |
||
77 | * @Route("/fail", name="loevgaard_dandomain_altapay_callback_fail") |
||
78 | * |
||
79 | * @LogHttpTransaction() |
||
80 | * |
||
81 | * @param Request $request |
||
82 | * @throws CallbackException |
||
83 | * @throws PaymentException |
||
84 | * @return Response |
||
85 | */ |
||
86 | public function failAction(Request $request) |
||
102 | |||
103 | /** |
||
104 | * @Method("POST") |
||
105 | * @Route("/redirect", name="loevgaard_dandomain_altapay_callback_redirect") |
||
106 | * |
||
107 | * @LogHttpTransaction() |
||
108 | * |
||
109 | * @param Request $request |
||
110 | * @throws CallbackException |
||
111 | * @throws PaymentException |
||
112 | * @return Response |
||
113 | */ |
||
114 | View Code Duplication | public function redirectAction(Request $request) |
|
124 | |||
125 | /** |
||
126 | * @Method("POST") |
||
127 | * @Route("/open", name="loevgaard_dandomain_altapay_callback_open") |
||
128 | * |
||
129 | * @LogHttpTransaction() |
||
130 | * |
||
131 | * @param Request $request |
||
132 | * @throws CallbackException |
||
133 | * @throws PaymentException |
||
134 | * @return Response |
||
135 | */ |
||
136 | View Code Duplication | public function openAction(Request $request) |
|
146 | |||
147 | /** |
||
148 | * @Method("POST") |
||
149 | * @Route("/notification", name="loevgaard_dandomain_altapay_callback_notification") |
||
150 | * |
||
151 | * @LogHttpTransaction() |
||
152 | * |
||
153 | * @param Request $request |
||
154 | * @throws CallbackException |
||
155 | * @throws PaymentException |
||
156 | * @return Response |
||
157 | */ |
||
158 | public function notificationAction(Request $request) |
||
164 | |||
165 | /** |
||
166 | * @Method("POST") |
||
167 | * @Route("/verify-order", name="loevgaard_dandomain_altapay_callback_verify_order") |
||
168 | * |
||
169 | * @LogHttpTransaction() |
||
170 | * |
||
171 | * @param Request $request |
||
172 | * @throws CallbackException |
||
173 | * @throws PaymentException |
||
174 | * @return Response |
||
175 | */ |
||
176 | public function verifyOrderAction(Request $request) |
||
182 | |||
183 | /** |
||
184 | * @param Request $request |
||
185 | * |
||
186 | * @return Payment |
||
187 | * |
||
188 | * @throws CallbackException |
||
189 | * @throws PaymentException |
||
190 | */ |
||
191 | protected function handleCallback(Request $request) |
||
250 | |||
251 | /** |
||
252 | * @param Request $request |
||
253 | * |
||
254 | * @return Payment |
||
255 | * |
||
256 | * @throws CallbackException |
||
257 | */ |
||
258 | protected function getPaymentFromRequest(Request $request) |
||
272 | |||
273 | /** |
||
274 | * @return PaymentRepository |
||
275 | */ |
||
276 | protected function getPaymentRepository() |
||
280 | |||
281 | /** |
||
282 | * @param Payment $payment |
||
283 | * @return SiteSetting[] |
||
284 | */ |
||
285 | protected function getSiteSettings(Payment $payment) : array |
||
291 | } |
||
292 |
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.