Conditions | 30 |
Paths | 66 |
Total Lines | 223 |
Code Lines | 120 |
Lines | 0 |
Ratio | 0 % |
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 |
||
271 | public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { |
||
272 | $httpRequest = $this->server->httpRequest; |
||
273 | |||
274 | if ($node instanceof \OCA\DAV\Connector\Sabre\Node) { |
||
275 | /** |
||
276 | * This was disabled, because it made dir listing throw an exception, |
||
277 | * so users were unable to navigate into folders where one subitem |
||
278 | * is blocked by the files_accesscontrol app, see: |
||
279 | * https://github.com/nextcloud/files_accesscontrol/issues/65 |
||
280 | * if (!$node->getFileInfo()->isReadable()) { |
||
281 | * // avoid detecting files through this means |
||
282 | * throw new NotFound(); |
||
283 | * } |
||
284 | */ |
||
285 | |||
286 | $propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) { |
||
287 | return $node->getFileId(); |
||
288 | }); |
||
289 | |||
290 | $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { |
||
291 | return $node->getInternalFileId(); |
||
292 | }); |
||
293 | |||
294 | $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) { |
||
295 | $perms = $node->getDavPermissions(); |
||
296 | if ($this->isPublic) { |
||
297 | // remove mount information |
||
298 | $perms = str_replace(['S', 'M'], '', $perms); |
||
299 | } |
||
300 | return $perms; |
||
301 | }); |
||
302 | |||
303 | $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) { |
||
304 | $user = $this->userSession->getUser(); |
||
305 | if ($user === null) { |
||
306 | return null; |
||
307 | } |
||
308 | return $node->getSharePermissions( |
||
309 | $user->getUID() |
||
310 | ); |
||
311 | }); |
||
312 | |||
313 | $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest): ?string { |
||
314 | $user = $this->userSession->getUser(); |
||
315 | if ($user === null) { |
||
316 | return null; |
||
317 | } |
||
318 | $ncPermissions = $node->getSharePermissions( |
||
319 | $user->getUID() |
||
320 | ); |
||
321 | $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions); |
||
322 | return json_encode($ocmPermissions, JSON_THROW_ON_ERROR); |
||
323 | }); |
||
324 | |||
325 | $propFind->handle(self::SHARE_ATTRIBUTES_PROPERTYNAME, function () use ($node, $httpRequest) { |
||
326 | return json_encode($node->getShareAttributes(), JSON_THROW_ON_ERROR); |
||
327 | }); |
||
328 | |||
329 | $propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node): string { |
||
330 | return $node->getETag(); |
||
331 | }); |
||
332 | |||
333 | $propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node): ?string { |
||
334 | $owner = $node->getOwner(); |
||
335 | if (!$owner) { |
||
336 | return null; |
||
337 | } else { |
||
338 | return $owner->getUID(); |
||
339 | } |
||
340 | }); |
||
341 | $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node): ?string { |
||
342 | $owner = $node->getOwner(); |
||
343 | if (!$owner) { |
||
344 | return null; |
||
345 | } else { |
||
346 | return $owner->getDisplayName(); |
||
347 | } |
||
348 | }); |
||
349 | |||
350 | $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
||
351 | return json_encode($this->previewManager->isAvailable($node->getFileInfo()), JSON_THROW_ON_ERROR); |
||
352 | }); |
||
353 | $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node): int|float { |
||
354 | return $node->getSize(); |
||
355 | }); |
||
356 | $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
||
357 | return $node->getFileInfo()->getMountPoint()->getMountType(); |
||
358 | }); |
||
359 | |||
360 | $propFind->handle(self::SHARE_NOTE, function () use ($node, $httpRequest): ?string { |
||
361 | $user = $this->userSession->getUser(); |
||
362 | if ($user === null) { |
||
363 | return null; |
||
364 | } |
||
365 | return $node->getNoteFromShare( |
||
366 | $user->getUID() |
||
367 | ); |
||
368 | }); |
||
369 | |||
370 | $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () use ($node) { |
||
371 | return $this->config->getSystemValue('data-fingerprint', ''); |
||
372 | }); |
||
373 | $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function () use ($node) { |
||
374 | return (new \DateTimeImmutable()) |
||
375 | ->setTimestamp($node->getFileInfo()->getCreationTime()) |
||
376 | ->format(\DateTimeInterface::ATOM); |
||
377 | }); |
||
378 | $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function () use ($node) { |
||
379 | return $node->getFileInfo()->getCreationTime(); |
||
380 | }); |
||
381 | /** |
||
382 | * Return file/folder name as displayname. The primary reason to |
||
383 | * implement it this way is to avoid costly fallback to |
||
384 | * CustomPropertiesBackend (esp. visible when querying all files |
||
385 | * in a folder). |
||
386 | */ |
||
387 | $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) { |
||
388 | return $node->getName(); |
||
389 | }); |
||
390 | } |
||
391 | |||
392 | if ($node instanceof \OCA\DAV\Connector\Sabre\File) { |
||
393 | $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) { |
||
394 | try { |
||
395 | $directDownloadUrl = $node->getDirectDownload(); |
||
396 | if (isset($directDownloadUrl['url'])) { |
||
397 | return $directDownloadUrl['url']; |
||
398 | } |
||
399 | } catch (StorageNotAvailableException $e) { |
||
400 | return false; |
||
401 | } catch (ForbiddenException $e) { |
||
402 | return false; |
||
403 | } |
||
404 | return false; |
||
405 | }); |
||
406 | |||
407 | $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) { |
||
408 | $checksum = $node->getChecksum(); |
||
409 | if ($checksum === null || $checksum === '') { |
||
410 | return null; |
||
411 | } |
||
412 | |||
413 | return new ChecksumList($checksum); |
||
414 | }); |
||
415 | |||
416 | $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function () use ($node) { |
||
417 | return $node->getFileInfo()->getUploadTime(); |
||
418 | }); |
||
419 | |||
420 | if ($this->config->getSystemValueBool('enable_file_metadata', true)) { |
||
421 | $propFind->handle(self::FILE_METADATA_SIZE, function () use ($node) { |
||
422 | if (!str_starts_with($node->getFileInfo()->getMimetype(), 'image')) { |
||
423 | return json_encode((object)[], JSON_THROW_ON_ERROR); |
||
424 | } |
||
425 | |||
426 | if ($node->hasMetadata('size')) { |
||
427 | $sizeMetadata = $node->getMetadata('size'); |
||
428 | } else { |
||
429 | // This code path should not be called since we try to preload |
||
430 | // the metadata when loading the folder or the search results |
||
431 | // in one go |
||
432 | $metadataManager = \OC::$server->get(IMetadataManager::class); |
||
433 | $sizeMetadata = $metadataManager->fetchMetadataFor('size', [$node->getId()])[$node->getId()]; |
||
434 | |||
435 | // TODO would be nice to display this in the profiler... |
||
436 | \OC::$server->get(LoggerInterface::class)->debug('Inefficient fetching of metadata'); |
||
437 | } |
||
438 | |||
439 | return $sizeMetadata->getValue(); |
||
440 | }); |
||
441 | } |
||
442 | } |
||
443 | |||
444 | if ($node instanceof Directory) { |
||
445 | $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) { |
||
446 | return $node->getSize(); |
||
447 | }); |
||
448 | |||
449 | $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function () use ($node) { |
||
450 | return $node->getFileInfo()->isEncrypted() ? '1' : '0'; |
||
451 | }); |
||
452 | |||
453 | $requestProperties = $propFind->getRequestedProperties(); |
||
454 | |||
455 | // TODO detect dynamically which metadata groups are requested and |
||
456 | // preload all of them and not just size |
||
457 | if ($this->config->getSystemValueBool('enable_file_metadata', true) |
||
458 | && in_array(self::FILE_METADATA_SIZE, $requestProperties, true)) { |
||
459 | // Preloading of the metadata |
||
460 | $fileIds = []; |
||
461 | foreach ($node->getChildren() as $child) { |
||
462 | /** @var \OCP\Files\Node|Node $child */ |
||
463 | if (str_starts_with($child->getFileInfo()->getMimeType(), 'image/')) { |
||
464 | /** @var File $child */ |
||
465 | $fileIds[] = $child->getFileInfo()->getId(); |
||
466 | } |
||
467 | } |
||
468 | /** @var IMetaDataManager $metadataManager */ |
||
469 | $metadataManager = \OC::$server->get(IMetadataManager::class); |
||
470 | $preloadedMetadata = $metadataManager->fetchMetadataFor('size', $fileIds); |
||
471 | foreach ($node->getChildren() as $child) { |
||
472 | /** @var \OCP\Files\Node|Node $child */ |
||
473 | if (str_starts_with($child->getFileInfo()->getMimeType(), 'image')) { |
||
474 | /** @var File $child */ |
||
475 | $child->setMetadata('size', $preloadedMetadata[$child->getFileInfo()->getId()]); |
||
476 | } |
||
477 | } |
||
478 | } |
||
479 | |||
480 | if (in_array(self::SUBFILE_COUNT_PROPERTYNAME, $requestProperties, true) |
||
481 | || in_array(self::SUBFOLDER_COUNT_PROPERTYNAME, $requestProperties, true)) { |
||
482 | $nbFiles = 0; |
||
483 | $nbFolders = 0; |
||
484 | foreach ($node->getChildren() as $child) { |
||
485 | if ($child instanceof File) { |
||
486 | $nbFiles++; |
||
487 | } elseif ($child instanceof Directory) { |
||
488 | $nbFolders++; |
||
489 | } |
||
490 | } |
||
491 | |||
492 | $propFind->handle(self::SUBFILE_COUNT_PROPERTYNAME, $nbFiles); |
||
493 | $propFind->handle(self::SUBFOLDER_COUNT_PROPERTYNAME, $nbFolders); |
||
494 | } |
||
602 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: