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 |
||
| 16 | class DoctrineTicketRepository extends EntityRepository implements TicketRepository |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @param Event $event |
||
| 20 | * |
||
| 21 | * @return Ticket[] |
||
| 22 | */ |
||
| 23 | View Code Duplication | public function getToNotify(Event $event) |
|
|
|
|||
| 24 | { |
||
| 25 | return $this->createQueryBuilder('t') |
||
| 26 | ->andWhere('t.event = :event')->setParameter('event', $event) |
||
| 27 | ->andWhere('t.notified = 0') |
||
| 28 | ->andWhere('t.payment IS NOT NULL') |
||
| 29 | ->getQuery() |
||
| 30 | ->getResult(); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param Event $event |
||
| 35 | * @param string $email |
||
| 36 | * |
||
| 37 | * @return Ticket[] |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function getTicketsForEmail(Event $event, $email) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @param integer $id |
||
| 50 | * @param string $code |
||
| 51 | * |
||
| 52 | * @return Option |
||
| 53 | */ |
||
| 54 | public function getTicketByIdAndCode($id, $code) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param Event $event |
||
| 65 | * |
||
| 66 | * @return Ticket[] |
||
| 67 | */ |
||
| 68 | public function getTicketsForEvent(Event $event) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns the number of tickets for the given day. |
||
| 78 | * |
||
| 79 | * @param Event $event |
||
| 80 | * @param $day |
||
| 81 | * |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function getTicketCountForEvent(Event $event, $day) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the number of checkins for the given day. |
||
| 96 | * |
||
| 97 | * @param Event $event |
||
| 98 | * @param $day |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function getCheckinCountForEvent(Event $event, $day) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the list of unprinted tickets for the given day. |
||
| 115 | * |
||
| 116 | * @param Event $event |
||
| 117 | * @param $day |
||
| 118 | * |
||
| 119 | * @return Ticket[] |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function getUnprintedTickets(Event $event, $day) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Search tickets matching the given term |
||
| 134 | * |
||
| 135 | * @param Event $event |
||
| 136 | * @param $day |
||
| 137 | * @param $term |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function searchTickets(Event $event, $day, $term) |
||
| 155 | |||
| 156 | |||
| 157 | } |
||
| 158 |
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.