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 |
||
| 33 | class NoteService { |
||
| 34 | |||
| 35 | private $noteMapper; |
||
| 36 | private $utils; |
||
| 37 | private $groupService; |
||
| 38 | |||
| 39 | public function __construct(NoteMapper $noteMapper, Utils $utils,NotebookService $groupService) { |
||
| 40 | $this->noteMapper = $noteMapper; |
||
| 41 | $this->utils = $utils; |
||
| 42 | $this->groupService = $groupService; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get notes from a user. |
||
| 47 | * |
||
| 48 | * @param $userId |
||
| 49 | * @param int|bool $deleted |
||
| 50 | * @param string|bool $grouping |
||
| 51 | * @return Note[] |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function findNotesFromUser($userId, $deleted = false, $grouping = false) { |
|
| 54 | // Get shares |
||
| 55 | |||
| 56 | $dbNotes = $this->noteMapper->findNotesFromUser($userId, $deleted, $grouping); |
||
| 57 | |||
| 58 | $n = $dbNotes; |
||
| 59 | if($dbNotes instanceof Note){ |
||
| 60 | $dbNotes = []; |
||
| 61 | /** |
||
| 62 | * @var $n Note |
||
| 63 | */ |
||
| 64 | $dbNotes[$n->getId()] = $n; |
||
| 65 | } |
||
| 66 | |||
| 67 | //$sharedNotes = $this->sharing->getSharedNotes(); |
||
| 68 | $sharedNotes = []; |
||
| 69 | $notes = array_merge($dbNotes, $sharedNotes); |
||
| 70 | return $notes; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get a single note |
||
| 75 | * |
||
| 76 | * @param $note_id |
||
| 77 | * @param $user_id |
||
| 78 | * @param bool|int $deleted |
||
| 79 | * @return Note |
||
| 80 | * @internal param $vault_id |
||
| 81 | */ |
||
| 82 | public function find($note_id, $user_id = null, $deleted = false) { |
||
| 83 | $note = $this->noteMapper->find($note_id, $user_id, $deleted); |
||
| 84 | return $note; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Creates a note |
||
| 89 | * |
||
| 90 | * @param Note $note |
||
| 91 | * @return Note |
||
| 92 | * @throws \Exception |
||
| 93 | */ |
||
| 94 | public function create(Note $note) { |
||
| 95 | if (!$note instanceof Note) { |
||
| 96 | throw new \Exception("Expected Note object!"); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this->noteMapper->insert($note); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Update note |
||
| 104 | * |
||
| 105 | * @param $note Note |
||
| 106 | * @return Note|bool |
||
| 107 | * @throws \Exception |
||
| 108 | * @internal param $userId |
||
| 109 | * @internal param $vault |
||
| 110 | */ |
||
| 111 | public function update(Note $note) { |
||
| 112 | if (!$note instanceof Note) { |
||
| 113 | throw new \Exception("Expected Note object!"); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $this->noteMapper->updateNote($note); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Delete a note from user |
||
| 121 | * |
||
| 122 | * @param $note_id |
||
| 123 | * @param string $user_id |
||
| 124 | * @return bool |
||
| 125 | * @internal param string $vault_guid |
||
| 126 | */ |
||
| 127 | public function delete($note_id, $user_id = null) { |
||
| 128 | $note = $this->noteMapper->find($note_id, $user_id); |
||
| 129 | if ($note instanceof Note) { |
||
| 130 | $this->noteMapper->deleteNote($note); |
||
| 131 | return true; |
||
| 132 | } else { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Creates an example note for a user. |
||
| 139 | * @param $userId |
||
| 140 | */ |
||
| 141 | public function createExampleNote($userId) { |
||
| 150 | } |
||
| 151 |