Skip to content

Commit

Permalink
Merge pull request #36836 from nextcloud/fix/view-type-cleanup
Browse files Browse the repository at this point in the history
Tidy up typing in OC\Files\View
  • Loading branch information
come-nc authored Apr 5, 2023
2 parents 88ab6d4 + 6633b4c commit 8f55039
Show file tree
Hide file tree
Showing 25 changed files with 209 additions and 331 deletions.
3 changes: 1 addition & 2 deletions apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function rename($path1, $path2) {
return $this->canRename;
}

public function getRelativePath($path) {
public function getRelativePath($path): ?string {
return $path;
}
}
Expand All @@ -73,7 +73,6 @@ public function getRelativePath($path) {
* @group DB
*/
class DirectoryTest extends \Test\TestCase {

use UserTrait;

/** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */
Expand Down
3 changes: 1 addition & 2 deletions apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public function testCopy($sourcePath, $targetPath, $targetParent): void {
$view = $this->createMock(View::class);
$view->expects($this->once())
->method('verifyPath')
->with($targetParent)
->willReturn(true);
->with($targetParent);
$view->expects($this->once())
->method('file_exists')
->with($targetPath)
Expand Down
2 changes: 1 addition & 1 deletion apps/encryption/tests/Command/FixEncryptedVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function testRepairUnencryptedFileWhenVersionIsSet() {
$cacheInfo = ['encryptedVersion' => 1, 'encrypted' => 1];
$cache1->put($fileCache1->getPath(), $cacheInfo);

$absPath = $view->getLocalFolder(''). '/hello.txt';
$absPath = $storage1->getSourcePath('').$fileInfo1->getInternalPath();

// create unencrypted file on disk, the version stays
file_put_contents($absPath, 'hello contents');
Expand Down
2 changes: 1 addition & 1 deletion apps/files_versions/lib/Versions/IVersionBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function rollback(IVersion $version);
* Open the file for reading
*
* @param IVersion $version
* @return resource
* @return resource|false
* @throws NotFoundException
* @since 15.0.0
*/
Expand Down
36 changes: 15 additions & 21 deletions lib/private/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@
use OCP\IUser;

class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
private array|ICacheEntry $data;
/**
* @var array $data
*/
private $data;

/**
* @var string $path
* @var string
*/
private $path;

Expand All @@ -53,7 +49,7 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
private $storage;

/**
* @var string $internalPath
* @var string
*/
private $internalPath;

Expand All @@ -62,22 +58,19 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
*/
private $mount;

/**
* @var IUser
*/
private $owner;
private ?IUser $owner;

/**
* @var string[]
*/
private $childEtags = [];
private array $childEtags = [];

/**
* @var IMountPoint[]
*/
private $subMounts = [];
private array $subMounts = [];

private $subMountsUsed = false;
private bool $subMountsUsed = false;

/**
* The size of the file/folder without any sub mount
Expand All @@ -89,8 +82,8 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
* @param Storage\Storage $storage
* @param string $internalPath
* @param array|ICacheEntry $data
* @param \OCP\Files\Mount\IMountPoint $mount
* @param \OCP\IUser|null $owner
* @param IMountPoint $mount
* @param ?IUser $owner
*/
public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) {
$this->path = $path;
Expand All @@ -107,6 +100,9 @@ public function __construct($path, $storage, $internalPath, $data, $mount, $owne
}

public function offsetSet($offset, $value): void {
if (is_null($offset)) {
throw new \TypeError('Null offset not supported');
}
$this->data[$offset] = $value;
}

Expand Down Expand Up @@ -239,10 +235,8 @@ public function isEncrypted() {

/**
* Return the currently version used for the HMAC in the encryption app
*
* @return int
*/
public function getEncryptedVersion() {
public function getEncryptedVersion(): int {
return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
}

Expand Down Expand Up @@ -357,7 +351,7 @@ public function getMountPoint() {
/**
* Get the owner of the file
*
* @return \OCP\IUser
* @return ?IUser
*/
public function getOwner() {
return $this->owner;
Expand All @@ -370,7 +364,7 @@ public function setSubMounts(array $mounts) {
$this->subMounts = $mounts;
}

private function updateEntryfromSubMounts() {
private function updateEntryfromSubMounts(): void {
if ($this->subMountsUsed) {
return;
}
Expand Down
90 changes: 29 additions & 61 deletions lib/private/Files/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,24 @@
use OC\User\NoUserException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\FilesystemTornDownEvent;
use OCP\Files\Mount\IMountManager;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;

class Filesystem {
/**
* @var Mount\Manager $mounts
*/
private static $mounts;
private static ?Mount\Manager $mounts = null;

public static $loaded = false;
/**
* @var \OC\Files\View $defaultInstance
*/
private static $defaultInstance;
public static bool $loaded = false;

private static $usersSetup = [];
private static ?View $defaultInstance = null;

private static $normalizedPathCache = null;

private static $listeningForProviders = false;
private static ?CappedMemoryCache $normalizedPathCache = null;

/** @var string[]|null */
private static $blacklist = null;
private static ?array $blacklist = null;

/**
* classname which used for hooks handling
Expand Down Expand Up @@ -186,22 +178,18 @@ class Filesystem {
public const signal_param_mount_type = 'mounttype';
public const signal_param_users = 'users';

/**
* @var \OC\Files\Storage\StorageFactory $loader
*/
private static $loader;
private static ?\OC\Files\Storage\StorageFactory $loader = null;

/** @var bool */
private static $logWarningWhenAddingStorageWrapper = true;
private static bool $logWarningWhenAddingStorageWrapper = true;

/**
* @param bool $shouldLog
* @return bool previous value
* @internal
*/
public static function logWarningWhenAddingStorageWrapper($shouldLog) {
public static function logWarningWhenAddingStorageWrapper(bool $shouldLog): bool {
$previousValue = self::$logWarningWhenAddingStorageWrapper;
self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog;
self::$logWarningWhenAddingStorageWrapper = $shouldLog;
return $previousValue;
}

Expand Down Expand Up @@ -232,18 +220,17 @@ public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50)
*/
public static function getLoader() {
if (!self::$loader) {
self::$loader = \OC::$server->query(IStorageFactory::class);
self::$loader = \OC::$server->get(IStorageFactory::class);
}
return self::$loader;
}

/**
* Returns the mount manager
*
* @return \OC\Files\Mount\Manager
*/
public static function getMountManager($user = '') {
public static function getMountManager(): Mount\Manager {
self::initMountManager();
assert(self::$mounts !== null);
return self::$mounts;
}

Expand Down Expand Up @@ -313,14 +300,14 @@ public static function getMountByNumericId($id) {
* resolve a path to a storage and internal path
*
* @param string $path
* @return array an array consisting of the storage and the internal path
* @return array{?\OCP\Files\Storage\IStorage, string} an array consisting of the storage and the internal path
*/
public static function resolvePath($path) {
public static function resolvePath($path): array {
$mount = self::getMountManager()->find($path);
return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')];
}

public static function init($user, $root) {
public static function init(string|IUser|null $user, string $root): bool {
if (self::$defaultInstance) {
return false;
}
Expand All @@ -332,7 +319,7 @@ public static function init($user, $root) {
return true;
}

public static function initInternal($root) {
public static function initInternal(string $root): bool {
if (self::$defaultInstance) {
return false;
}
Expand All @@ -342,32 +329,28 @@ public static function initInternal($root) {
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
$eventDispatcher->addListener(FilesystemTornDownEvent::class, function () {
self::$defaultInstance = null;
self::$usersSetup = [];
self::$loaded = false;
});

if (!self::$mounts) {
self::$mounts = \OC::$server->getMountManager();
}
self::initMountManager();

self::$loaded = true;

return true;
}

public static function initMountManager() {
public static function initMountManager(): void {
if (!self::$mounts) {
self::$mounts = \OC::$server->getMountManager();
self::$mounts = \OC::$server->get(IMountManager::class);
}
}

/**
* Initialize system and personal mount points for a user
*
* @param string|IUser|null $user
* @throws \OC\User\NoUserException if the user is not available
*/
public static function initMountPoints($user = '') {
public static function initMountPoints(string|IUser|null $user = ''): void {
/** @var IUserManager $userManager */
$userManager = \OC::$server->get(IUserManager::class);

Expand All @@ -382,11 +365,9 @@ public static function initMountPoints($user = '') {
}

/**
* get the default filesystem view
*
* @return View
* Get the default filesystem view
*/
public static function getView() {
public static function getView(): ?View {
if (!self::$defaultInstance) {
/** @var IUserSession $session */
$session = \OC::$server->get(IUserSession::class);
Expand All @@ -409,7 +390,7 @@ public static function tearDown() {
/**
* get the relative path of the root data directory for the current user
*
* @return string
* @return ?string
*
* Returns path like /admin/files
*/
Expand Down Expand Up @@ -439,22 +420,11 @@ public static function mount($class, $arguments, $mountpoint) {
* return the path to a local version of the file
* we need this because we can't know if a file is stored local or not from
* outside the filestorage and for some purposes a local file is needed
*
* @param string $path
* @return string
*/
public static function getLocalFile($path) {
public static function getLocalFile(string $path): string|false {
return self::$defaultInstance->getLocalFile($path);
}

/**
* @param string $path
* @return string
*/
public static function getLocalFolder($path) {
return self::$defaultInstance->getLocalFolder($path);
}

/**
* return path to file which reflects one visible in browser
*
Expand Down Expand Up @@ -611,9 +581,10 @@ public static function fopen($path, $mode) {
}

/**
* @return string
* @param string $path
* @throws \OCP\Files\InvalidPathException
*/
public static function toTmpFile($path) {
public static function toTmpFile($path): string|false {
return self::$defaultInstance->toTmpFile($path);
}

Expand Down Expand Up @@ -787,11 +758,8 @@ public static function getOwner($path) {

/**
* get the ETag for a file or folder
*
* @param string $path
* @return string
*/
public static function getETag($path) {
public static function getETag(string $path): string|false {
return self::$defaultInstance->getETag($path);
}
}
2 changes: 1 addition & 1 deletion lib/private/Files/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function putContent($data) {

/**
* @param string $mode
* @return resource
* @return resource|false
* @throws NotPermittedException
* @throws LockedException
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/SimpleFS/NewSimpleFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function getExtension(): string {
/**
* Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
*
* @return resource
* @return resource|false
* @throws \OCP\Files\NotPermittedException
* @since 14.0.0
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/SimpleFS/SimpleFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getExtension(): string {
/**
* Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
*
* @return resource
* @return resource|false
* @throws \OCP\Files\NotPermittedException
* @since 14.0.0
*/
Expand Down
Loading

0 comments on commit 8f55039

Please sign in to comment.