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:
Complex classes like ValidateController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ValidateController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ValidateController extends Controller |
||
32 | { |
||
33 | /** |
||
34 | * @var TicketLocker |
||
35 | */ |
||
36 | protected $ticketLocker; |
||
37 | /** |
||
38 | * @var TicketRepository |
||
39 | */ |
||
40 | protected $ticketRepository; |
||
41 | |||
42 | /** |
||
43 | * @var PGTicketRepository |
||
44 | */ |
||
45 | protected $pgTicketRepository; |
||
46 | |||
47 | /** |
||
48 | * @var TicketGenerator |
||
49 | */ |
||
50 | protected $ticketGenerator; |
||
51 | |||
52 | /** |
||
53 | * @var PGTCaller |
||
54 | */ |
||
55 | protected $pgtCaller; |
||
56 | |||
57 | /** |
||
58 | * ValidateController constructor. |
||
59 | * @param TicketLocker $ticketLocker |
||
60 | * @param TicketRepository $ticketRepository |
||
61 | * @param PGTicketRepository $pgTicketRepository |
||
62 | * @param TicketGenerator $ticketGenerator |
||
63 | * @param PGTCaller $pgtCaller |
||
64 | */ |
||
65 | 20 | public function __construct( |
|
78 | |||
79 | 5 | public function v1ValidateAction(Request $request) |
|
102 | |||
103 | 1 | public function v2ServiceValidateAction(Request $request) |
|
107 | |||
108 | 1 | public function v3ServiceValidateAction(Request $request) |
|
112 | |||
113 | 1 | public function v2ProxyValidateAction(Request $request) |
|
117 | |||
118 | 1 | public function v3ProxyValidateAction(Request $request) |
|
122 | |||
123 | 3 | public function proxyAction(Request $request) |
|
151 | |||
152 | /** |
||
153 | * @param Request $request |
||
154 | * @param bool $returnAttr |
||
155 | * @param bool $allowProxy |
||
156 | * @return Response |
||
157 | */ |
||
158 | 11 | protected function casValidate(Request $request, $returnAttr, $allowProxy) |
|
220 | |||
221 | /** |
||
222 | * @param string $username |
||
223 | * @param string $format |
||
224 | * @param array $attributes |
||
225 | * @param array $proxies |
||
226 | * @param string|null $pgt |
||
227 | * @return Response |
||
228 | */ |
||
229 | 1 | protected function authSuccessResponse($username, $format, $attributes, $proxies = [], $pgt = null) |
|
250 | |||
251 | /** |
||
252 | * @param string $code |
||
253 | * @param string $description |
||
254 | * @param string $format |
||
255 | * @return Response |
||
256 | */ |
||
257 | 1 | View Code Duplication | protected function authFailureResponse($code, $description, $format) |
268 | |||
269 | /** |
||
270 | * @param string $ticket |
||
271 | * @param string $format |
||
272 | * @return Response |
||
273 | */ |
||
274 | 1 | protected function proxySuccessResponse($ticket, $format) |
|
285 | |||
286 | /** |
||
287 | * @param string $code |
||
288 | * @param string $description |
||
289 | * @param string $format |
||
290 | * @return Response |
||
291 | */ |
||
292 | 1 | View Code Duplication | protected function proxyFailureResponse($code, $description, $format) |
303 | |||
304 | /** |
||
305 | * @param string $ticket |
||
306 | * @return bool |
||
307 | */ |
||
308 | 1 | protected function lockTicket($ticket) |
|
312 | |||
313 | /** |
||
314 | * @param string $ticket |
||
315 | * @return bool |
||
316 | */ |
||
317 | 1 | protected function unlockTicket($ticket) |
|
321 | } |
||
322 |
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.