Skip to content

Commit

Permalink
Merge pull request #48219 from nextcloud/fix/istorage/return-types
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Sep 27, 2024
2 parents 7887ab8 + 227609a commit 4f88123
Show file tree
Hide file tree
Showing 46 changed files with 789 additions and 1,928 deletions.
4 changes: 2 additions & 2 deletions apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ public function testGetQuotaInfoUnlimited(): void {
$storage->expects($this->any())
->method('instanceOfStorage')
->willReturnMap([
'\OCA\Files_Sharing\SharedStorage' => false,
'\OC\Files\Storage\Wrapper\Quota' => false,
['\OCA\Files_Sharing\SharedStorage', false],
['\OC\Files\Storage\Wrapper\Quota', false],
]);

$storage->expects($this->once())
Expand Down
75 changes: 29 additions & 46 deletions apps/files_external/lib/Lib/Storage/AmazonS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {

private LoggerInterface $logger;

public function needsPartFile() {
public function needsPartFile(): bool {
return false;
}

Expand Down Expand Up @@ -63,7 +63,7 @@ public function __construct($parameters) {
* @param string $path
* @return string correctly encoded path
*/
private function normalizePath($path) {
private function normalizePath($path): string {
$path = trim($path, '/');

if (!$path) {
Expand All @@ -73,24 +73,24 @@ private function normalizePath($path) {
return $path;
}

private function isRoot($path) {
private function isRoot($path): bool {
return $path === '.';
}

private function cleanKey($path) {
private function cleanKey($path): string {
if ($this->isRoot($path)) {
return '/';
}
return $path;
}

private function clearCache() {
private function clearCache(): void {
$this->objectCache = new CappedMemoryCache();
$this->directoryCache = new CappedMemoryCache();
$this->filesCache = new CappedMemoryCache();
}

private function invalidateCache($key) {
private function invalidateCache($key): void {
unset($this->objectCache[$key]);
$keys = array_keys($this->objectCache->getData());
$keyLength = strlen($key);
Expand All @@ -110,10 +110,7 @@ private function invalidateCache($key) {
unset($this->directoryCache[$key]);
}

/**
* @return array|false
*/
private function headObject(string $key) {
private function headObject(string $key): array|false {
if (!isset($this->objectCache[$key])) {
try {
$this->objectCache[$key] = $this->getConnection()->headObject([
Expand Down Expand Up @@ -144,11 +141,9 @@ private function headObject(string $key) {
* Implementation from flysystem-aws-s3-v3:
* https://github.com/thephpleague/flysystem-aws-s3-v3/blob/8241e9cc5b28f981e0d24cdaf9867f14c7498ae4/src/AwsS3Adapter.php#L670-L694
*
* @param $path
* @return bool
* @throws \Exception
*/
private function doesDirectoryExist($path) {
private function doesDirectoryExist($path): bool {
if ($path === '.' || $path === '') {
return true;
}
Expand Down Expand Up @@ -190,13 +185,7 @@ private function doesDirectoryExist($path) {
return false;
}

/**
* Remove a file or folder
*
* @param string $path
* @return bool
*/
protected function remove($path) {
protected function remove($path): bool {
// remember fileType to reduce http calls
$fileType = $this->filetype($path);
if ($fileType === 'dir') {
Expand All @@ -208,7 +197,7 @@ protected function remove($path) {
}
}

public function mkdir($path) {
public function mkdir($path): bool {
$path = $this->normalizePath($path);

if ($this->is_dir($path)) {
Expand Down Expand Up @@ -236,12 +225,12 @@ public function mkdir($path) {
return true;
}

public function file_exists($path) {
public function file_exists($path): bool {
return $this->filetype($path) !== false;
}


public function rmdir($path) {
public function rmdir($path): bool {
$path = $this->normalizePath($path);

if ($this->isRoot($path)) {
Expand All @@ -256,12 +245,12 @@ public function rmdir($path) {
return $this->batchDelete($path);
}

protected function clearBucket() {
protected function clearBucket(): bool {
$this->clearCache();
return $this->batchDelete();
}

private function batchDelete($path = null) {
private function batchDelete($path = null): bool {
// TODO explore using https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.BatchDelete.html
$params = [
'Bucket' => $this->bucket
Expand Down Expand Up @@ -312,7 +301,7 @@ public function opendir($path) {
}
}

public function stat($path) {
public function stat($path): array|false {
$path = $this->normalizePath($path);

if ($this->is_dir($path)) {
Expand All @@ -334,11 +323,8 @@ public function stat($path) {
*
* When the information is already present (e.g. opendir has been called before)
* this value is return. Otherwise a headObject is emitted.
*
* @param $path
* @return int|mixed
*/
private function getContentLength($path) {
private function getContentLength($path): int {
if (isset($this->filesCache[$path])) {
return (int)$this->filesCache[$path]['ContentLength'];
}
Expand All @@ -356,11 +342,8 @@ private function getContentLength($path) {
*
* When the information is already present (e.g. opendir has been called before)
* this value is return. Otherwise a headObject is emitted.
*
* @param $path
* @return mixed|string
*/
private function getLastModified($path) {
private function getLastModified($path): string {
if (isset($this->filesCache[$path])) {
return $this->filesCache[$path]['LastModified'];
}
Expand All @@ -373,7 +356,7 @@ private function getLastModified($path) {
return 'now';
}

public function is_dir($path) {
public function is_dir($path): bool {
$path = $this->normalizePath($path);

if (isset($this->filesCache[$path])) {
Expand All @@ -391,7 +374,7 @@ public function is_dir($path) {
}
}

public function filetype($path) {
public function filetype($path): string|false {
$path = $this->normalizePath($path);

if ($this->isRoot($path)) {
Expand Down Expand Up @@ -419,15 +402,15 @@ public function filetype($path) {
return false;
}

public function getPermissions($path) {
public function getPermissions($path): int {
$type = $this->filetype($path);
if (!$type) {
return 0;
}
return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
}

public function unlink($path) {
public function unlink($path): bool {
$path = $this->normalizePath($path);

if ($this->is_dir($path)) {
Expand Down Expand Up @@ -506,7 +489,7 @@ public function fopen($path, $mode) {
return false;
}

public function touch($path, $mtime = null) {
public function touch($path, $mtime = null): bool {
if (is_null($mtime)) {
$mtime = time();
}
Expand Down Expand Up @@ -541,7 +524,7 @@ public function touch($path, $mtime = null) {
return true;
}

public function copy($source, $target, $isFile = null) {
public function copy($source, $target, $isFile = null): bool {
$source = $this->normalizePath($source);
$target = $this->normalizePath($target);

Expand Down Expand Up @@ -584,7 +567,7 @@ public function copy($source, $target, $isFile = null) {
return true;
}

public function rename($source, $target) {
public function rename($source, $target): bool {
$source = $this->normalizePath($source);
$target = $this->normalizePath($target);

Expand All @@ -611,18 +594,18 @@ public function rename($source, $target) {
return true;
}

public function test() {
public function test(): bool {
$this->getConnection()->headBucket([
'Bucket' => $this->bucket
]);
return true;
}

public function getId() {
public function getId(): string {
return $this->id;
}

public function writeBack($tmpFile, $path) {
public function writeBack($tmpFile, $path): bool {
try {
$source = fopen($tmpFile, 'r');
$this->writeObject($path, $source, $this->mimeDetector->detectPath($path));
Expand All @@ -642,7 +625,7 @@ public function writeBack($tmpFile, $path) {
/**
* check if curl is installed
*/
public static function checkDependencies() {
public static function checkDependencies(): bool {
return true;
}

Expand Down Expand Up @@ -743,7 +726,7 @@ protected function getVersioningStatusFromBucket(): bool {
}
}

public function hasUpdated($path, $time) {
public function hasUpdated($path, $time): bool {
// for files we can get the proper mtime
if ($path !== '' && $object = $this->headObject($path)) {
$stat = $this->objectToMetaData($object);
Expand Down
Loading

0 comments on commit 4f88123

Please sign in to comment.