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 |
||
17 | class PGTicketRepository |
||
18 | { |
||
19 | /** |
||
20 | * @var PGTicket |
||
21 | */ |
||
22 | protected $pgTicket; |
||
23 | /** |
||
24 | * @var ServiceRepository |
||
25 | */ |
||
26 | protected $serviceRepository; |
||
27 | |||
28 | /** |
||
29 | * @var TicketGenerator |
||
30 | */ |
||
31 | protected $ticketGenerator; |
||
32 | |||
33 | /** |
||
34 | * PGTicketRepository constructor. |
||
35 | * @param PGTicket $pgTicket |
||
36 | * @param ServiceRepository $serviceRepository |
||
37 | * @param TicketGenerator $ticketGenerator |
||
38 | */ |
||
39 | 30 | public function __construct( |
|
48 | |||
49 | /** |
||
50 | * @param string $ticket |
||
51 | * @param bool $checkExpired |
||
52 | * @return null|PGTicket |
||
53 | */ |
||
54 | 1 | View Code Duplication | public function getByTicket($ticket, $checkExpired = true) |
63 | |||
64 | /** |
||
65 | * @param UserModel $user |
||
66 | */ |
||
67 | 1 | public function invalidTicketByUser(UserModel $user) |
|
71 | |||
72 | /** |
||
73 | * @param UserModel $user |
||
74 | * @param string $pgtUrl |
||
75 | * @param array $proxies |
||
76 | * @return PGTicket |
||
77 | * @throws CasException |
||
78 | */ |
||
79 | 4 | View Code Duplication | public function applyTicket(UserModel $user, $pgtUrl, $proxies = []) |
105 | |||
106 | /** |
||
107 | * @param string $totalLength |
||
108 | * @return string|false |
||
109 | */ |
||
110 | 1 | View Code Duplication | protected function getAvailableTicket($totalLength) |
121 | } |
||
122 |
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.