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 TicketRepository |
||
18 | { |
||
19 | /** |
||
20 | * @var Ticket |
||
21 | */ |
||
22 | protected $ticket; |
||
23 | |||
24 | /** |
||
25 | * @var ServiceRepository |
||
26 | */ |
||
27 | protected $serviceRepository; |
||
28 | |||
29 | /** |
||
30 | * @var TicketGenerator |
||
31 | */ |
||
32 | protected $ticketGenerator; |
||
33 | |||
34 | /** |
||
35 | * TicketRepository constructor. |
||
36 | * @param Ticket $ticket |
||
37 | * @param ServiceRepository $serviceRepository |
||
38 | * @param TicketGenerator $ticketGenerator |
||
39 | */ |
||
40 | 19 | public function __construct(Ticket $ticket, ServiceRepository $serviceRepository, TicketGenerator $ticketGenerator) |
|
46 | |||
47 | /** |
||
48 | * @param UserModel $user |
||
49 | * @param string $serviceUrl |
||
50 | * @param array $proxies |
||
51 | * @throws CasException |
||
52 | * @return Ticket |
||
53 | */ |
||
54 | 1 | View Code Duplication | public function applyTicket(UserModel $user, $serviceUrl, $proxies = []) |
79 | |||
80 | /** |
||
81 | * @param string $ticket |
||
82 | * @param bool $checkExpired |
||
83 | * @return null|Ticket |
||
84 | */ |
||
85 | 1 | View Code Duplication | public function getByTicket($ticket, $checkExpired = true) |
94 | |||
95 | /** |
||
96 | * @param Ticket $ticket |
||
97 | * @return bool|null |
||
98 | */ |
||
99 | 1 | public function invalidTicket(Ticket $ticket) |
|
103 | |||
104 | /** |
||
105 | * @param integer $totalLength |
||
106 | * @param string $prefix |
||
107 | * @return string|false |
||
108 | */ |
||
109 | 1 | View Code Duplication | protected function getAvailableTicket($totalLength, $prefix) |
120 | } |
||
121 |
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.