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 |
||
| 29 | class LabelMapper extends DeckMapper implements IPermissionMapper { |
||
| 30 | |||
| 31 | public function __construct(IDBConnection $db) { |
||
| 32 | parent::__construct($db, 'deck_labels', '\OCA\Deck\Db\Label'); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function findAll($boardId, $limit = null, $offset = null) { |
||
| 36 | $sql = 'SELECT * FROM `*PREFIX*deck_labels` WHERE `board_id` = ? ORDER BY `id`'; |
||
| 37 | return $this->findEntities($sql, [$boardId], $limit, $offset); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function delete(\OCP\AppFramework\Db\Entity $entity) { |
||
| 41 | // delete assigned labels |
||
| 42 | $this->deleteLabelAssignments($entity->getId()); |
||
| 43 | // delete label |
||
| 44 | return parent::delete($entity); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function findAssignedLabelsForCard($cardId, $limit = null, $offset = null) { |
||
| 48 | $sql = 'SELECT l.* FROM `*PREFIX*deck_assigned_labels` as al INNER JOIN *PREFIX*deck_labels as l ON l.id = al.label_id WHERE `card_id` = ? ORDER BY l.id'; |
||
| 49 | return $this->findEntities($sql, [$cardId], $limit, $offset); |
||
| 50 | } |
||
| 51 | public function findAssignedLabelsForBoard($boardId, $limit = null, $offset = null) { |
||
| 52 | $sql = "SELECT c.id as card_id, l.id as id, l.title as title, l.color as color FROM oc_deck_cards as c " . |
||
| 53 | " INNER JOIN oc_deck_assigned_labels as al ON al.card_id = c.id INNER JOIN oc_deck_labels as l ON al.label_id = l.id WHERE board_id=? ORDER BY l.id"; |
||
| 54 | $entities = $this->findEntities($sql, [$boardId], $limit, $offset); |
||
| 55 | return $entities; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getAssignedLabelsForBoard($boardId) { |
||
| 59 | $labels = $this->findAssignedLabelsForBoard($boardId); |
||
| 60 | $result = array(); |
||
| 61 | foreach ($labels as $label) { |
||
| 62 | if (!array_key_exists($label->getCardId(), $result)) { |
||
| 63 | $result[$label->getCardId()] = array(); |
||
| 64 | } |
||
| 65 | $result[$label->getCardId()][] = $label; |
||
| 66 | } |
||
| 67 | return $result; |
||
| 68 | } |
||
| 69 | |||
| 70 | View Code Duplication | public function deleteLabelAssignments($labelId) { |
|
| 76 | |||
| 77 | View Code Duplication | public function deleteLabelAssignmentsForCard($cardId) { |
|
| 83 | |||
| 84 | View Code Duplication | public function isOwner($userId, $labelId) { |
|
| 90 | |||
| 91 | public function findBoardId($labelId) { |
||
| 95 | } |
||
| 96 |
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.