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 DoctrineUnregistrationRepository extends EntityRepository implements UnregistrationRepository |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @return Unregistration[] |
||
| 20 | */ |
||
| 21 | public function getNewUnregistrations() |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param Event $event |
||
| 32 | * |
||
| 33 | * @return Unregistration[] |
||
| 34 | */ |
||
| 35 | public function getUnprocessedUnregistrations(Event $event) |
||
| 36 | { |
||
| 37 | $rsm = new ResultSetMappingBuilder($this->_em); |
||
| 38 | $rsm->addRootEntityFromClassMetadata('BCRM\BackendBundle\Entity\Event\Unregistration', 'u'); |
||
| 39 | $eventId = $event->getId(); |
||
| 40 | $unregistrations = "SELECT MAX(id) FROM unregistration WHERE confirmed = 1 AND event_id = $eventId AND processed = 0 GROUP BY event_id, email ORDER BY created ASC, id ASC"; |
||
| 41 | $sql = "SELECT * FROM unregistration WHERE id IN ($unregistrations)"; |
||
| 42 | $query = $this->_em->createNativeQuery( |
||
| 43 | $sql, |
||
| 44 | $rsm |
||
| 45 | ); |
||
| 46 | return $query->getResult(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $id |
||
| 51 | * @param string $key |
||
| 52 | * |
||
| 53 | * @return \PhpOption\Option |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function getUnregistrationByIdAndKey($id, $key) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param Event $event |
||
| 66 | * |
||
| 67 | * @return Unregistration[] |
||
| 68 | */ |
||
| 69 | public function getUnregistrationsForEvent(Event $event) |
||
| 76 | |||
| 77 | |||
| 78 | } |
||
| 79 |
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.