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 |
||
22 | class NotesService { |
||
23 | private $l10n; |
||
24 | private $root; |
||
25 | /** |
||
26 | * @param IRootFolder $root |
||
27 | * @param IL10N $l10n |
||
28 | */ |
||
29 | 11 | public function __construct (IRootFolder $root, IL10N $l10n) { |
|
33 | /** |
||
34 | * @param string $userId |
||
35 | * @return array with all notes in the current directory |
||
36 | */ |
||
37 | 1 | public function getAll ($userId){ |
|
55 | /** |
||
56 | * Used to get a single note by id |
||
57 | * @param int $id the id of the note to get |
||
58 | * @param string $userId |
||
59 | * @throws NoteDoesNotExistException if note does not exist |
||
60 | * @return Note |
||
61 | */ |
||
62 | 3 | public function get ($id, $userId) { |
|
75 | /** |
||
76 | * Creates a note and returns the empty note |
||
77 | * @param string $userId |
||
78 | * @see update for setting note content |
||
79 | * @return Note the newly created note |
||
80 | */ |
||
81 | 2 | public function create ($userId) { |
|
91 | /** |
||
92 | * Updates a note. Be sure to check the returned note since the title is |
||
93 | * dynamically generated and filename conflicts are resolved |
||
94 | * @param int $id the id of the note used to update |
||
95 | * @param string $content the content which will be written into the note |
||
96 | * the title is generated from the first line of the content |
||
97 | * @throws NoteDoesNotExistException if note does not exist |
||
98 | * @return \OCA\Notes\Db\Note the updated note |
||
99 | */ |
||
100 | 2 | public function update ($id, $content, $userId){ |
|
130 | /** |
||
131 | * Set or unset a note as favorite. |
||
132 | * @param int $id the id of the note used to update |
||
133 | * @param boolean $favorite whether the note should be a favorite or not |
||
134 | * @throws NoteDoesNotExistException if note does not exist |
||
135 | * @return boolean the new favorite state of the note |
||
136 | */ |
||
137 | public function favorite ($id, $favorite, $userId){ |
||
151 | /** |
||
152 | * Deletes a note |
||
153 | * @param int $id the id of the note which should be deleted |
||
154 | * @param string $userId |
||
155 | * @throws NoteDoesNotExistException if note does not |
||
156 | * exist |
||
157 | */ |
||
158 | 3 | public function delete ($id, $userId) { |
|
163 | /** |
||
164 | * @param Folder $folder |
||
165 | * @param int $id |
||
166 | * @throws NoteDoesNotExistException |
||
167 | * @return \OCP\Files\File |
||
168 | */ |
||
169 | 8 | private function getFileById ($folder, $id) { |
|
176 | /** |
||
177 | * @param string $userId the user id |
||
178 | * @return Folder |
||
179 | */ |
||
180 | 11 | private function getFolderForUser ($userId) { |
|
189 | /** |
||
190 | * get path of file and the title.txt and check if they are the same |
||
191 | * file. If not the title needs to be renamed |
||
192 | * |
||
193 | * @param Folder $folder a folder to the notes directory |
||
194 | * @param string $title the filename which should be used |
||
195 | * @param string $extension the extension which should be used |
||
196 | * @param int $id the id of the note for which the title should be generated |
||
197 | * used to see if the file itself has the title and not a different file for |
||
198 | * checking for filename collisions |
||
199 | * @return string the resolved filename to prevent overwriting different |
||
200 | * files with the same title |
||
201 | */ |
||
202 | 2 | private function generateFileName (Folder $folder, $title, $extension, $id) { |
|
221 | /** |
||
222 | * gather note files in given directory and all subdirectories |
||
223 | * @param Folder $folder |
||
224 | * @return array |
||
225 | */ |
||
226 | 1 | private function gatherNoteFiles ($folder) { |
|
241 | /** |
||
242 | * test if file is a note |
||
243 | * |
||
244 | * @param \OCP\Files\File $file |
||
245 | * @return bool |
||
246 | */ |
||
247 | 7 | private function isNote($file) { |
|
256 | } |
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.