Complex classes like NotesService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NotesService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class NotesService { |
||
| 27 | |||
| 28 | private $l10n; |
||
| 29 | private $root; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param IRootFolder $root |
||
| 33 | * @param IL10N $l10n |
||
| 34 | */ |
||
| 35 | public function __construct (IRootFolder $root, IL10N $l10n) { |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $userId |
||
| 43 | * @return array with all notes in the current directory |
||
| 44 | */ |
||
| 45 | public function getAll ($userId){ |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Used to get a single note by id |
||
| 70 | * @param int $id the id of the note to get |
||
| 71 | * @param string $userId |
||
| 72 | * @throws NoteDoesNotExistException if note does not exist |
||
| 73 | * @return Note |
||
| 74 | */ |
||
| 75 | public function get ($id, $userId) { |
||
| 79 | |||
| 80 | private function getTags ($id) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Creates a note and returns the empty note |
||
| 92 | * @param string $userId |
||
| 93 | * @see update for setting note content |
||
| 94 | * @return Note the newly created note |
||
| 95 | */ |
||
| 96 | public function create ($userId) { |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Updates a note. Be sure to check the returned note since the title is |
||
| 112 | * dynamically generated and filename conflicts are resolved |
||
| 113 | * @param int $id the id of the note used to update |
||
| 114 | * @param string $content the content which will be written into the note |
||
| 115 | * the title is generated from the first line of the content |
||
| 116 | * @param int $mtime time of the note modification (optional) |
||
| 117 | * @throws NoteDoesNotExistException if note does not exist |
||
| 118 | * @return \OCA\Notes\Db\Note the updated note |
||
| 119 | */ |
||
| 120 | public function update ($id, $content, $userId, $category=null, $mtime=0) { |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Set or unset a note as favorite. |
||
| 180 | * @param int $id the id of the note used to update |
||
| 181 | * @param boolean $favorite whether the note should be a favorite or not |
||
| 182 | * @throws NoteDoesNotExistException if note does not exist |
||
| 183 | * @return boolean the new favorite state of the note |
||
| 184 | */ |
||
| 185 | public function favorite ($id, $favorite, $userId){ |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Deletes a note |
||
| 204 | * @param int $id the id of the note which should be deleted |
||
| 205 | * @param string $userId |
||
| 206 | * @throws NoteDoesNotExistException if note does not |
||
| 207 | * exist |
||
| 208 | */ |
||
| 209 | public function delete ($id, $userId) { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Folder $folder |
||
| 218 | * @param int $id |
||
| 219 | * @throws NoteDoesNotExistException |
||
| 220 | * @return \OCP\Files\File |
||
| 221 | */ |
||
| 222 | private function getFileById ($folder, $id) { |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $userId the user id |
||
| 234 | * @return Folder |
||
| 235 | */ |
||
| 236 | private function getFolderForUser ($userId) { |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Finds a folder and creates it if non-existent |
||
| 244 | * @param string $path path to the folder |
||
| 245 | * @return Folder |
||
| 246 | */ |
||
| 247 | private function getOrCreateFolder($path) { |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * get path of file and the title.txt and check if they are the same |
||
| 259 | * file. If not the title needs to be renamed |
||
| 260 | * |
||
| 261 | * @param Folder $folder a folder to the notes directory |
||
| 262 | * @param string $title the filename which should be used |
||
| 263 | * @param string $extension the extension which should be used |
||
| 264 | * @param int $id the id of the note for which the title should be generated |
||
| 265 | * used to see if the file itself has the title and not a different file for |
||
| 266 | * checking for filename collisions |
||
| 267 | * @return string the resolved filename to prevent overwriting different |
||
| 268 | * files with the same title |
||
| 269 | */ |
||
| 270 | private function generateFileName (Folder $folder, $title, $extension, $id) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * gather note files in given directory and all subdirectories |
||
| 293 | * @param Folder $folder |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | private function gatherNoteFiles ($folder) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * test if file is a note |
||
| 314 | * |
||
| 315 | * @param \OCP\Files\File $file |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | private function isNote($file) { |
||
| 329 | |||
| 330 | } |
||
| 331 |