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 Ticket implements LoggerAwareInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var \LiteCQRS\Bus\CommandBus |
||
25 | */ |
||
26 | private $commandBus; |
||
27 | |||
28 | /** |
||
29 | * @var TicketRepository |
||
30 | */ |
||
31 | private $ticketRepo; |
||
32 | |||
33 | /** |
||
34 | * @var LoggerInterface |
||
35 | */ |
||
36 | private $logger; |
||
37 | |||
38 | /** |
||
39 | * @param CommandBus $commandBus |
||
40 | * @param TicketRepository $ticketRepo |
||
41 | */ |
||
42 | public function __construct(CommandBus $commandBus, TicketRepository $ticketRepo) |
||
43 | { |
||
44 | $this->commandBus = $commandBus; |
||
45 | $this->ticketRepo = $ticketRepo; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Sets a logger instance on the object |
||
50 | * |
||
51 | * @param LoggerInterface $logger |
||
52 | * |
||
53 | * @return null |
||
54 | */ |
||
55 | public function setLogger(LoggerInterface $logger) |
||
56 | { |
||
57 | $this->logger = $logger; |
||
58 | } |
||
59 | |||
60 | View Code Duplication | public function onTicketMailSent(TicketMailSentEvent $event) |
|
68 | |||
69 | public function onTicketDeleted(TicketDeletedEvent $event) |
||
70 | { |
||
79 | |||
80 | /** |
||
81 | * Register payment on tickets |
||
82 | * |
||
83 | * @param RegistrationPaidEvent $event |
||
84 | */ |
||
85 | public function onRegistrationPaid(RegistrationPaidEvent $event) |
||
106 | } |
||
107 |
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.