| Conditions | 56 | 
| Paths | 148 | 
| Total Lines | 204 | 
| Code Lines | 131 | 
| Lines | 49 | 
| Ratio | 24.02 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 237 | 	public function getListing($FOLDER, $showdel) { | ||
| 238 | // Get the listing from the database | ||
| 239 | $requery = false; | ||
| 240 | $uid = \OC::$server->getUserSession()->getUser()->getUID(); | ||
| 241 | 		$shared_items = \OCP\Share::getItemsSharedWith('ownnote', 'populated_shares'); | ||
| 242 | /** | ||
| 243 | * @var $results OwnNote[] | ||
| 244 | */ | ||
| 245 | $results = array_merge($this->findNotesFromUser($uid), $shared_items); | ||
| 246 | |||
| 247 | $results2 = $results; | ||
| 248 | if ($results) | ||
| 249 | 			foreach ($results as $result) { | ||
| 250 | 				if($result instanceof OwnNote) { | ||
| 251 | $result = $result->jsonSerialize(); | ||
| 252 | } | ||
| 253 | |||
| 254 | 				foreach ($results2 as $result2) { | ||
| 255 | 					if($result2 instanceof OwnNote) { | ||
| 256 | $result2 = $result2->jsonSerialize(); | ||
| 257 | } | ||
| 258 | View Code Duplication | 					if ($result['id'] != $result2['id'] && $result['name'] == $result2['name'] && $result['grouping'] == $result2['grouping']) { | |
| 259 | // We have a duplicate that should not exist. Need to remove the offending record first | ||
| 260 | $delid = -1; | ||
| 261 | 						if ($result['mtime'] == $result2['mtime']) { | ||
| 262 | // If the mtime's match, delete the oldest ID. | ||
| 263 | $delid = $result['id']; | ||
| 264 | if ($result['id'] > $result2['id']) | ||
| 265 | $delid = $result2['id']; | ||
| 266 | 						} elseif ($result['mtime'] > $result2['mtime']) { | ||
| 267 | // Again, delete the oldest | ||
| 268 | $delid = $result2['id']; | ||
| 269 | 						} elseif ($result['mtime'] < $result2['mtime']) { | ||
| 270 | // The only thing left is if result is older | ||
| 271 | $delid = $result['id']; | ||
| 272 | } | ||
| 273 | 						if ($delid != -1) { | ||
| 274 | 							$this->delete('', $delid); | ||
| 275 | $requery = true; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | View Code Duplication | 		if ($requery) { | |
| 281 | 			$shared_items = \OCP\Share::getItemsSharedWith('ownnote', 'populated_shares'); | ||
| 282 | $results = array_merge($this->findNotesFromUser($uid), $shared_items); | ||
| 283 | $requery = false; | ||
| 284 | } | ||
| 285 | |||
| 286 | // Tests to add a bunch of notes | ||
| 287 | //$now = new DateTime(); | ||
| 288 | 		//for ($x = 0; $x < 199; $x++) { | ||
| 289 | 		//saveNote('', "Test ".$x, '', '', $now->getTimestamp()); | ||
| 290 | //} | ||
| 291 | $farray = array(); | ||
| 292 | 		if ($FOLDER != '') { | ||
| 293 | // Create the folder if it doesn't exist | ||
| 294 | View Code Duplication | 			if (!Filesystem::is_dir($FOLDER)) { | |
| 295 | 				if (!Filesystem::mkdir($FOLDER)) { | ||
| 296 | 					\OCP\Util::writeLog('ownnote', 'Could not create ownNote directory.', \OCP\Util::ERROR); | ||
| 297 | 					throw new \Exception("Error creating ownNote directory"); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | // Synchronize files to the database | ||
| 301 | $filearr = array(); | ||
| 302 | 			if ($listing = Filesystem::opendir($FOLDER)) { | ||
| 303 | 				if (!$listing) { | ||
| 304 | 					\OCP\Util::writeLog('ownnote', 'Error listing directory.', \OCP\Util::ERROR); | ||
| 305 | 					throw new \Exception("Error listing dir"); | ||
| 306 | } | ||
| 307 | 				while (($file = readdir($listing)) !== false) { | ||
| 308 | $tmpfile = $file; | ||
| 309 | if ($tmpfile == "." || $tmpfile == "..") continue; | ||
| 310 | if (!$this->utils->endsWith($tmpfile, ".htm") && !$this->utils->endsWith($tmpfile, ".html")) continue; | ||
| 311 | 					if ($info = Filesystem::getFileInfo($FOLDER . "/" . $tmpfile)) { | ||
| 312 | // Check for EVERNOTE but wait to rename them to get around: | ||
| 313 | // https://github.com/owncloud/core/issues/16202 | ||
| 314 | 						if ($this->utils->endsWith($tmpfile, ".html")) { | ||
| 315 | Evernote::checkEvernote($FOLDER, $tmpfile); | ||
| 316 | } | ||
| 317 | // Separate the name and group name | ||
| 318 | 						$name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $tmpfile); | ||
| 319 | $group = ""; | ||
| 320 | View Code Duplication | 						if (substr($name, 0, 1) == "[") { | |
| 321 | $end = strpos($name, ']'); | ||
| 322 | $group = substr($name, 1, $end - 1); | ||
| 323 | $name = substr($name, $end + 1, strlen($name) - $end + 1); | ||
| 324 | $name = trim($name); | ||
| 325 | } | ||
| 326 | // Set array for later checking | ||
| 327 | $filearr[] = $tmpfile; | ||
| 328 | // Check to see if the file is in the DB | ||
| 329 | $fileindb = false; | ||
| 330 | if ($results) | ||
| 331 | 							foreach ($results as $result) { | ||
| 332 | 								if($result instanceof OwnNote) { | ||
| 333 | $result = $result->jsonSerialize(); | ||
| 334 | } | ||
| 335 | if ($result['deleted'] == 0) | ||
| 336 | 									if ($name == $result['name'] && $group == $result['grouping']) { | ||
| 337 | $fileindb = true; | ||
| 338 | // If it is in the DB, check if the filesystem file is newer than the DB | ||
| 339 | 										if ($result['mtime'] < $info['mtime']) { | ||
| 340 | // File is newer, this could happen if a user updates a file | ||
| 341 | $html = Filesystem::file_get_contents($FOLDER . "/" . $tmpfile); | ||
| 342 | $n = [ | ||
| 343 | 'id' => $result['id'], | ||
| 344 | 'mtime' => $info['mtime'], | ||
| 345 | 'note' => ($html) ? $html : '' | ||
| 346 | ]; | ||
| 347 | 											$this->update('', $n); | ||
| 348 | $requery = true; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | 						if (!$fileindb) { | ||
| 353 | // If it's not in the DB, add it. | ||
| 354 | 							if ($html = Filesystem::file_get_contents($FOLDER . "/" . $tmpfile)) { | ||
| 355 | 							} else { | ||
| 356 | $html = ""; | ||
| 357 | } | ||
| 358 | $n = [ | ||
| 359 | 'name' => $name, | ||
| 360 | 'group' => $group, | ||
| 361 | 'note' => $html, | ||
| 362 | 'mtime' => $info['mtime'], | ||
| 363 | 'uid' => $uid | ||
| 364 | ]; | ||
| 365 | 							$this->create('', $n, $uid); | ||
| 366 | $requery = true; | ||
| 367 | } | ||
| 368 | // We moved the rename down here to overcome the OC issue | ||
| 369 | View Code Duplication | 						if ($this->utils->endsWith($tmpfile, ".html")) { | |
| 370 | $tmpfile = substr($tmpfile, 0, -1); | ||
| 371 | 							if (!Filesystem::file_exists($FOLDER . "/" . $tmpfile)) { | ||
| 372 | Filesystem::rename($FOLDER . "/" . $file, $FOLDER . "/" . $tmpfile); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | } | ||
| 378 | View Code Duplication | 			if ($requery) { | |
| 379 | 				$shared_items = \OCP\Share::getItemsSharedWith('ownnote', 'populated_shares'); | ||
| 380 | $results = array_merge($this->findNotesFromUser($uid), $shared_items); | ||
| 381 | } | ||
| 382 | // Now also make sure the files exist, they may not if the user switched folders in admin. | ||
| 383 | if ($results) | ||
| 384 | 				foreach ($results as $result) { | ||
| 385 | 					if($result instanceof OwnNote) { | ||
| 386 | $result = $result->jsonSerialize(); | ||
| 387 | } | ||
| 388 | 					if ($result['deleted'] == 0) { | ||
| 389 | $tmpfile = $result['name'] . ".htm"; | ||
| 390 | View Code Duplication | if ($result['grouping'] != '') | |
| 391 | $tmpfile = '[' . $result['grouping'] . '] ' . $result['name'] . '.htm'; | ||
| 392 | $filefound = false; | ||
| 393 | 						foreach ($filearr as $f) { | ||
| 394 | 							if ($f == $tmpfile) { | ||
| 395 | $filefound = true; | ||
| 396 | break; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | 						if (!$filefound) { | ||
| 400 | $this->update($FOLDER, $result); | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } | ||
| 404 | } | ||
| 405 | // Now loop through and return the listing | ||
| 406 | 		if ($results) { | ||
| 407 | $count = 0; | ||
| 408 | $now = new \DateTime(); | ||
| 409 | $filetime = new \DateTime(); | ||
| 410 | |||
| 411 | 			foreach ($results as $result) { | ||
| 412 | 				if($result instanceof OwnNote) { | ||
| 413 | $result = $result->jsonSerialize(); | ||
| 414 | } | ||
| 415 | 				if ($result['deleted'] == 0 || $showdel == true) { | ||
| 416 | $filetime->setTimestamp($result['mtime']); | ||
| 417 | $timestring = $this->utils->getTimeString($filetime, $now); | ||
| 418 | $f = array(); | ||
| 419 | $f['id'] = $result['id']; | ||
| 420 | $f['uid'] = $result['uid']; | ||
| 421 | $f['name'] = $result['name']; | ||
| 422 | $f['group'] = ($result['grouping']) ? $result['grouping'] : ''; | ||
| 423 | $f['timestring'] = $timestring; | ||
| 424 | $f['mtime'] = $result['mtime']; | ||
| 425 | $f['timediff'] = $now->getTimestamp() - $result['mtime']; | ||
| 426 | $f['deleted'] = $result['deleted']; | ||
| 427 | $f['permissions'] = @$result['permissions']; | ||
| 428 | |||
| 429 | |||
| 430 | 					$shared_with = \OCP\Share::getUsersItemShared('ownnote', $result['id'], $result['uid']); | ||
| 431 | // add shares (all shares, if it's an owned note, only the user for shared notes (not disclosing other sharees)) | ||
| 432 | $f['shared_with'] = ($result['uid'] == $uid) ? $shared_with : [$uid]; | ||
| 433 | |||
| 434 | $farray[$count] = $f; | ||
| 435 | $count++; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | } | ||
| 439 | return $farray; | ||
| 440 | } | ||
| 441 | |||
| 456 |