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 |
||
| 32 | class NotesService { |
||
| 33 | |||
| 34 | private $l10n; |
||
| 35 | private $root; |
||
| 36 | private $logger; |
||
| 37 | private $config; |
||
| 38 | private $settings; |
||
| 39 | private $appName; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param IRootFolder $root |
||
| 43 | * @param IL10N $l10n |
||
| 44 | * @param ILogger $logger |
||
| 45 | * @param IConfig $config |
||
| 46 | * @param \OCA\Notes\Service\SettingsService $settings |
||
| 47 | * @param String $appName |
||
| 48 | */ |
||
| 49 | public function __construct (IRootFolder $root, IL10N $l10n, ILogger $logger, IConfig $config, SettingsService $settings, $appName) { |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $userId |
||
| 61 | * @return array with all notes in the current directory |
||
| 62 | */ |
||
| 63 | public function getAll ($userId, $onlyMeta=false) { |
||
| 64 | $notesFolder = $this->getFolderForUser($userId); |
||
| 65 | $notes = $this->gatherNoteFiles($notesFolder); |
||
| 66 | $filesById = []; |
||
| 67 | foreach($notes as $note) { |
||
| 68 | $filesById[$note->getId()] = $note; |
||
| 69 | } |
||
| 70 | $tagger = \OC::$server->getTagManager()->load('files'); |
||
| 71 | if($tagger===null) { |
||
| 72 | $tags = []; |
||
| 73 | } else { |
||
| 74 | $tags = $tagger->getTagsForObjects(array_keys($filesById)); |
||
| 75 | } |
||
| 76 | |||
| 77 | $notes = []; |
||
| 78 | foreach($filesById as $id=>$file) { |
||
| 79 | $notes[] = $this->getNote($file, $notesFolder, array_key_exists($id, $tags) ? $tags[$id] : [], $onlyMeta); |
||
| 80 | } |
||
| 81 | |||
| 82 | return $notes; |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Used to get a single note by id |
||
| 88 | * @param int $id the id of the note to get |
||
| 89 | * @param string $userId |
||
| 90 | * @throws NoteDoesNotExistException if note does not exist |
||
| 91 | * @return Note |
||
| 92 | */ |
||
| 93 | public function get ($id, $userId) { |
||
| 97 | |||
| 98 | private function getTags ($id) { |
||
| 107 | |||
| 108 | private function getNote($file, $notesFolder, $tags=[], $onlyMeta=false) { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Creates a note and returns the empty note |
||
| 125 | * @param string $userId |
||
| 126 | * @see update for setting note content |
||
| 127 | * @return Note the newly created note |
||
| 128 | */ |
||
| 129 | public function create ($userId) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Updates a note. Be sure to check the returned note since the title is |
||
| 150 | * dynamically generated and filename conflicts are resolved |
||
| 151 | * @param int $id the id of the note used to update |
||
| 152 | * @param string $content the content which will be written into the note |
||
| 153 | * the title is generated from the first line of the content |
||
| 154 | * @param int $mtime time of the note modification (optional) |
||
| 155 | * @throws NoteDoesNotExistException if note does not exist |
||
| 156 | * @return \OCA\Notes\Db\Note the updated note |
||
| 157 | */ |
||
| 158 | public function update ($id, $content, $userId, $category=null, $mtime=0) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set or unset a note as favorite. |
||
| 213 | * @param int $id the id of the note used to update |
||
| 214 | * @param boolean $favorite whether the note should be a favorite or not |
||
| 215 | * @throws NoteDoesNotExistException if note does not exist |
||
| 216 | * @return boolean the new favorite state of the note |
||
| 217 | */ |
||
| 218 | public function favorite ($id, $favorite, $userId){ |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Deletes a note |
||
| 237 | * @param int $id the id of the note which should be deleted |
||
| 238 | * @param string $userId |
||
| 239 | * @throws NoteDoesNotExistException if note does not |
||
| 240 | * exist |
||
| 241 | */ |
||
| 242 | public function delete ($id, $userId) { |
||
| 249 | |||
| 250 | // removes characters that are illegal in a file or folder name on some operating systems |
||
| 251 | private function sanitisePath($str) { |
||
| 271 | |||
| 272 | private function getSafeTitleFromContent($content) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param Folder $folder |
||
| 299 | * @param int $id |
||
| 300 | * @throws NoteDoesNotExistException |
||
| 301 | * @return \OCP\Files\File |
||
| 302 | */ |
||
| 303 | private function getFileById ($folder, $id) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $userId the user id |
||
| 314 | * @return boolean true if folder is accessible, or Exception otherwise |
||
| 315 | */ |
||
| 316 | public function checkNotesFolder($userId) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $userId the user id |
||
| 323 | * @return Folder |
||
| 324 | */ |
||
| 325 | private function getFolderForUser ($userId) { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Finds a folder and creates it if non-existent |
||
| 338 | * @param string $path path to the folder |
||
| 339 | * @return Folder |
||
| 340 | */ |
||
| 341 | private function getOrCreateFolder($path) { |
||
| 349 | |||
| 350 | /* |
||
| 351 | * Delete a folder and it's parent(s) if it's/they're empty |
||
| 352 | * @param Folder root folder for notes |
||
| 353 | * @param Folder folder to delete |
||
| 354 | */ |
||
| 355 | private function deleteEmptyFolder(Folder $notesFolder, Folder $folder) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * get path of file and the title.txt and check if they are the same |
||
| 369 | * file. If not the title needs to be renamed |
||
| 370 | * |
||
| 371 | * @param Folder $folder a folder to the notes directory |
||
| 372 | * @param string $title the filename which should be used |
||
| 373 | * @param string $suffix the suffix (incl. dot) which should be used |
||
| 374 | * @param int $id the id of the note for which the title should be generated |
||
| 375 | * used to see if the file itself has the title and not a different file for |
||
| 376 | * checking for filename collisions |
||
| 377 | * @return string the resolved filename to prevent overwriting different |
||
| 378 | * files with the same title |
||
| 379 | */ |
||
| 380 | private function generateFileName (Folder $folder, $title, $suffix, $id) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * gather note files in given directory and all subdirectories |
||
| 403 | * @param Folder $folder |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | private function gatherNoteFiles ($folder) { |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * test if file is a note |
||
| 424 | * |
||
| 425 | * @param \OCP\Files\File $file |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | private function isNote($file) { |
||
| 440 | |||
| 441 | } |
||
| 442 |
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.