Skip to content

Commit

Permalink
chore: ugly type juggling
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz committed Jun 10, 2023
1 parent 5085eac commit a38b21c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
16 changes: 12 additions & 4 deletions lib/private/Files/Cache/QuerySearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,31 @@ public function searchInCaches(ISearchQuery $searchQuery, array $caches): array
}

/**
* @return array{array<string, ICache>, array<string, IMountPoint>}
* @return array{0?: array<string, ICache>, 1?: array<string, IMountPoint>}
*/
public function getCachesAndMountPointsForSearch(IRootFolder $root, string $path, bool $limitToHome = false): array {
$rootLength = strlen($path);
$mount = $root->getMount($path);
$storage = $mount->getStorage();
if (method_exists($root, 'getMount')) {
/** @var IMountPoint $mount */
$mount = $root->getMount($path);
$storage = $mount->getStorage();
}
if ($storage === null) {
return [];
}
$internalPath = $mount->getInternalPath($path);

if ($internalPath !== '') {
// a temporary CacheJail is used to handle filtering down the results to within this folder
/** @var ICache[] $caches */
$caches = ['' => new CacheJail($storage->getCache(''), $internalPath)];
} else {
/** @var ICache[] $caches */
$caches = ['' => $storage->getCache('')];
}
$mountByMountPoint = ['' => $mount];

if (!$limitToHome) {
if (!$limitToHome && method_exists($root, 'getMountsIn')) {
/** @var IMountPoint[] $mounts */
$mounts = $root->getMountsIn($path);
foreach ($mounts as $mount) {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorage;
use OCP\IUser;

class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
Expand Down Expand Up @@ -79,7 +80,7 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {

/**
* @param string|boolean $path
* @param Storage\Storage $storage
* @param Storage\Storage|IStorage $storage
* @param string $internalPath
* @param array|ICacheEntry $data
* @param IMountPoint $mount
Expand Down
11 changes: 8 additions & 3 deletions lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,13 @@ protected function getAppDataDirectoryName(): string {
* @return array
*/
protected function getByIdInRootMount(int $id): array {
$mount = $this->root->getMount('');
$cacheEntry = $mount->getStorage()->getCache($this->path)->get($id);
$storage = null;
if (\method_exists($this->root, 'getMount')) {
/** @var IMountPoint $mount */
$mount = $this->root->getMount('');
$storage = $mount->getStorage();
}
$cacheEntry = $storage?->getCache($this->path)->get($id);
if (!$cacheEntry) {
return [];
}
Expand All @@ -346,7 +351,7 @@ protected function getByIdInRootMount(int $id): array {
return [$this->root->createNode(
$absolutePath, new \OC\Files\FileInfo(
$absolutePath,
$mount->getStorage(),
$storage,
$cacheEntry->getPath(),
$cacheEntry,
$mount
Expand Down
16 changes: 8 additions & 8 deletions lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
use OCP\Files\FileInfo;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
use Symfony\Component\EventDispatcher\GenericEvent;

// FIXME: this class really should be abstract
class Node implements \OCP\Files\Node {
class Node implements INode {
/**
* @var \OC\Files\View $view
*/
Expand Down Expand Up @@ -128,7 +129,9 @@ protected function sendHooks($hooks, array $args = null) {
$args = !empty($args) ? $args : [$this];
$dispatcher = \OC::$server->getEventDispatcher();
foreach ($hooks as $hook) {
$this->root->emit('\OC\Files', $hook, $args);
if (method_exists($this->root, 'emit')) {
$this->root->emit('\OC\Files', $hook, $args);
}
$dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
}
}
Expand Down Expand Up @@ -288,10 +291,7 @@ public function isCreatable() {
return $this->getFileInfo(false)->isCreatable();
}

/**
* @return Node
*/
public function getParent() {
public function getParent(): INode|IRootFolder {
if ($this->parent === null) {
$newPath = dirname($this->path);
if ($newPath === '' || $newPath === '.' || $newPath === '/') {
Expand Down Expand Up @@ -400,7 +400,7 @@ public function unlock($type) {

/**
* @param string $targetPath
* @return \OCP\Files\Node
* @return INode
* @throws InvalidPathException
* @throws NotFoundException
* @throws NotPermittedException if copy not allowed or failed
Expand All @@ -426,7 +426,7 @@ public function copy($targetPath) {

/**
* @param string $targetPath
* @return \OCP\Files\Node
* @return INode
* @throws InvalidPathException
* @throws NotFoundException
* @throws NotPermittedException if move not allowed or failed
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCP\Files\Events\Node\FilesystemTornDownEvent;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IUser;
Expand Down Expand Up @@ -342,7 +343,7 @@ public function isShareable() {
* @return Node
* @throws \OCP\Files\NotFoundException
*/
public function getParent() {
public function getParent(): INode|IRootFolder {
throw new NotFoundException();
}

Expand Down
8 changes: 5 additions & 3 deletions tests/lib/Files/Node/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Jail;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\Search\ISearchComparison;
Expand Down Expand Up @@ -462,12 +463,13 @@ public function testSearchSubStorages() {
}

public function testIsSubNode() {
$file = new Node(null, $this->view, '/foo/bar');
$folder = new Folder(null, $this->view, '/foo');
$rootFolderMock = $this->createMock(IRootFolder::class);
$file = new Node($rootFolderMock, $this->view, '/foo/bar');
$folder = new Folder($rootFolderMock, $this->view, '/foo');
$this->assertTrue($folder->isSubNode($file));
$this->assertFalse($folder->isSubNode($folder));

$file = new Node(null, $this->view, '/foobar');
$file = new Node($rootFolderMock, $this->view, '/foobar');
$this->assertFalse($folder->isSubNode($file));
}

Expand Down

0 comments on commit a38b21c

Please sign in to comment.