Skip to content

Commit 024682b

Browse files
committed
refactor(files_external): Add Storage parameter strong types
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 7d1e6de commit 024682b

File tree

8 files changed

+116
-175
lines changed

8 files changed

+116
-175
lines changed

apps/files_external/lib/Lib/Storage/AmazonS3.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ public function __construct($parameters) {
5959
$this->logger = Server::get(LoggerInterface::class);
6060
}
6161

62-
/**
63-
* @param string $path
64-
* @return string correctly encoded path
65-
*/
66-
private function normalizePath($path): string {
62+
private function normalizePath(string $path): string {
6763
$path = trim($path, '/');
6864

6965
if (!$path) {
@@ -73,11 +69,11 @@ private function normalizePath($path): string {
7369
return $path;
7470
}
7571

76-
private function isRoot($path): bool {
72+
private function isRoot(string $path): bool {
7773
return $path === '.';
7874
}
7975

80-
private function cleanKey($path): string {
76+
private function cleanKey(string $path): string {
8177
if ($this->isRoot($path)) {
8278
return '/';
8379
}
@@ -90,7 +86,7 @@ private function clearCache(): void {
9086
$this->filesCache = new CappedMemoryCache();
9187
}
9288

93-
private function invalidateCache($key): void {
89+
private function invalidateCache(string $key): void {
9490
unset($this->objectCache[$key]);
9591
$keys = array_keys($this->objectCache->getData());
9692
$keyLength = strlen($key);
@@ -143,7 +139,7 @@ private function headObject(string $key): array|false {
143139
*
144140
* @throws \Exception
145141
*/
146-
private function doesDirectoryExist($path): bool {
142+
private function doesDirectoryExist(string $path): bool {
147143
if ($path === '.' || $path === '') {
148144
return true;
149145
}
@@ -185,7 +181,7 @@ private function doesDirectoryExist($path): bool {
185181
return false;
186182
}
187183

188-
protected function remove($path): bool {
184+
protected function remove(string $path): bool {
189185
// remember fileType to reduce http calls
190186
$fileType = $this->filetype($path);
191187
if ($fileType === 'dir') {
@@ -197,7 +193,7 @@ protected function remove($path): bool {
197193
}
198194
}
199195

200-
public function mkdir($path): bool {
196+
public function mkdir(string $path): bool {
201197
$path = $this->normalizePath($path);
202198

203199
if ($this->is_dir($path)) {
@@ -225,12 +221,12 @@ public function mkdir($path): bool {
225221
return true;
226222
}
227223

228-
public function file_exists($path): bool {
224+
public function file_exists(string $path): bool {
229225
return $this->filetype($path) !== false;
230226
}
231227

232228

233-
public function rmdir($path): bool {
229+
public function rmdir(string $path): bool {
234230
$path = $this->normalizePath($path);
235231

236232
if ($this->isRoot($path)) {
@@ -250,7 +246,7 @@ protected function clearBucket(): bool {
250246
return $this->batchDelete();
251247
}
252248

253-
private function batchDelete($path = null): bool {
249+
private function batchDelete(?string $path = null): bool {
254250
// TODO explore using https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.BatchDelete.html
255251
$params = [
256252
'Bucket' => $this->bucket
@@ -290,7 +286,7 @@ private function batchDelete($path = null): bool {
290286
return true;
291287
}
292288

293-
public function opendir($path) {
289+
public function opendir(string $path) {
294290
try {
295291
$content = iterator_to_array($this->getDirectoryContent($path));
296292
return IteratorDirectory::wrap(array_map(function (array $item) {
@@ -301,7 +297,7 @@ public function opendir($path) {
301297
}
302298
}
303299

304-
public function stat($path): array|false {
300+
public function stat(string $path): array|false {
305301
$path = $this->normalizePath($path);
306302

307303
if ($this->is_dir($path)) {
@@ -324,7 +320,7 @@ public function stat($path): array|false {
324320
* When the information is already present (e.g. opendir has been called before)
325321
* this value is return. Otherwise a headObject is emitted.
326322
*/
327-
private function getContentLength($path): int {
323+
private function getContentLength(string $path): int {
328324
if (isset($this->filesCache[$path])) {
329325
return (int)$this->filesCache[$path]['ContentLength'];
330326
}
@@ -343,7 +339,7 @@ private function getContentLength($path): int {
343339
* When the information is already present (e.g. opendir has been called before)
344340
* this value is return. Otherwise a headObject is emitted.
345341
*/
346-
private function getLastModified($path): string {
342+
private function getLastModified(string $path): string {
347343
if (isset($this->filesCache[$path])) {
348344
return $this->filesCache[$path]['LastModified'];
349345
}
@@ -356,7 +352,7 @@ private function getLastModified($path): string {
356352
return 'now';
357353
}
358354

359-
public function is_dir($path): bool {
355+
public function is_dir(string $path): bool {
360356
$path = $this->normalizePath($path);
361357

362358
if (isset($this->filesCache[$path])) {
@@ -374,7 +370,7 @@ public function is_dir($path): bool {
374370
}
375371
}
376372

377-
public function filetype($path): string|false {
373+
public function filetype(string $path): string|false {
378374
$path = $this->normalizePath($path);
379375

380376
if ($this->isRoot($path)) {
@@ -402,15 +398,15 @@ public function filetype($path): string|false {
402398
return false;
403399
}
404400

405-
public function getPermissions($path): int {
401+
public function getPermissions(string $path): int {
406402
$type = $this->filetype($path);
407403
if (!$type) {
408404
return 0;
409405
}
410406
return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
411407
}
412408

413-
public function unlink($path): bool {
409+
public function unlink(string $path): bool {
414410
$path = $this->normalizePath($path);
415411

416412
if ($this->is_dir($path)) {
@@ -431,7 +427,7 @@ public function unlink($path): bool {
431427
return true;
432428
}
433429

434-
public function fopen($path, $mode) {
430+
public function fopen(string $path, string $mode) {
435431
$path = $this->normalizePath($path);
436432

437433
switch ($mode) {
@@ -489,7 +485,7 @@ public function fopen($path, $mode) {
489485
return false;
490486
}
491487

492-
public function touch($path, $mtime = null): bool {
488+
public function touch(string $path, ?int $mtime = null): bool {
493489
if (is_null($mtime)) {
494490
$mtime = time();
495491
}
@@ -524,7 +520,7 @@ public function touch($path, $mtime = null): bool {
524520
return true;
525521
}
526522

527-
public function copy($source, $target, $isFile = null): bool {
523+
public function copy(string $source, string $target, ?bool $isFile = null): bool {
528524
$source = $this->normalizePath($source);
529525
$target = $this->normalizePath($target);
530526

@@ -567,7 +563,7 @@ public function copy($source, $target, $isFile = null): bool {
567563
return true;
568564
}
569565

570-
public function rename($source, $target): bool {
566+
public function rename(string $source, string $target): bool {
571567
$source = $this->normalizePath($source);
572568
$target = $this->normalizePath($target);
573569

@@ -605,7 +601,7 @@ public function getId(): string {
605601
return $this->id;
606602
}
607603

608-
public function writeBack($tmpFile, $path): bool {
604+
public function writeBack(string $tmpFile, string $path): bool {
609605
try {
610606
$source = fopen($tmpFile, 'r');
611607
$this->writeObject($path, $source, $this->mimeDetector->detectPath($path));
@@ -629,7 +625,7 @@ public static function checkDependencies(): bool {
629625
return true;
630626
}
631627

632-
public function getDirectoryContent($directory): \Traversable {
628+
public function getDirectoryContent(string $directory): \Traversable {
633629
$path = $this->normalizePath($directory);
634630

635631
if ($this->isRoot($path)) {
@@ -726,7 +722,7 @@ protected function getVersioningStatusFromBucket(): bool {
726722
}
727723
}
728724

729-
public function hasUpdated($path, $time): bool {
725+
public function hasUpdated(string $path, int $time): bool {
730726
// for files we can get the proper mtime
731727
if ($path !== '' && $object = $this->headObject($path)) {
732728
$stat = $this->objectToMetaData($object);

apps/files_external/lib/Lib/Storage/FTP.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function getId(): string {
8282
return 'ftp::' . $this->username . '@' . $this->host . '/' . $this->root;
8383
}
8484

85-
protected function buildPath($path): string {
85+
protected function buildPath(string $path): string {
8686
return rtrim($this->root . '/' . $path, '/');
8787
}
8888

@@ -94,7 +94,7 @@ public static function checkDependencies(): array|bool {
9494
}
9595
}
9696

97-
public function filemtime($path): int|false {
97+
public function filemtime(string $path): int|false {
9898
$result = $this->getConnection()->mdtm($this->buildPath($path));
9999

100100
if ($result === -1) {
@@ -126,7 +126,7 @@ public function filemtime($path): int|false {
126126
}
127127
}
128128

129-
public function filesize($path): false|int|float {
129+
public function filesize(string $path): false|int|float {
130130
$result = $this->getConnection()->size($this->buildPath($path));
131131
if ($result === -1) {
132132
return false;
@@ -135,7 +135,7 @@ public function filesize($path): false|int|float {
135135
}
136136
}
137137

138-
public function rmdir($path): bool {
138+
public function rmdir(string $path): bool {
139139
if ($this->is_dir($path)) {
140140
$result = $this->getConnection()->rmdir($this->buildPath($path));
141141
// recursive rmdir support depends on the ftp server
@@ -151,10 +151,7 @@ public function rmdir($path): bool {
151151
}
152152
}
153153

154-
/**
155-
* @param string $path
156-
*/
157-
private function recursiveRmDir($path): bool {
154+
private function recursiveRmDir(string $path): bool {
158155
$contents = $this->getDirectoryContent($path);
159156
$result = true;
160157
foreach ($contents as $content) {
@@ -177,7 +174,7 @@ public function test(): bool {
177174
}
178175
}
179176

180-
public function stat($path): array|false {
177+
public function stat(string $path): array|false {
181178
if (!$this->file_exists($path)) {
182179
return false;
183180
}
@@ -187,14 +184,14 @@ public function stat($path): array|false {
187184
];
188185
}
189186

190-
public function file_exists($path): bool {
187+
public function file_exists(string $path): bool {
191188
if ($path === '' || $path === '.' || $path === '/') {
192189
return true;
193190
}
194191
return $this->filetype($path) !== false;
195192
}
196193

197-
public function unlink($path): bool {
194+
public function unlink(string $path): bool {
198195
switch ($this->filetype($path)) {
199196
case 'dir':
200197
return $this->rmdir($path);
@@ -205,19 +202,19 @@ public function unlink($path): bool {
205202
}
206203
}
207204

208-
public function opendir($path) {
205+
public function opendir(string $path) {
209206
$files = $this->getConnection()->nlist($this->buildPath($path));
210207
return IteratorDirectory::wrap($files);
211208
}
212209

213-
public function mkdir($path): bool {
210+
public function mkdir(string $path): bool {
214211
if ($this->is_dir($path)) {
215212
return false;
216213
}
217214
return $this->getConnection()->mkdir($this->buildPath($path)) !== false;
218215
}
219216

220-
public function is_dir($path): bool {
217+
public function is_dir(string $path): bool {
221218
if ($path === '') {
222219
return true;
223220
}
@@ -229,11 +226,11 @@ public function is_dir($path): bool {
229226
}
230227
}
231228

232-
public function is_file($path): bool {
229+
public function is_file(string $path): bool {
233230
return $this->filesize($path) !== false;
234231
}
235232

236-
public function filetype($path): string|false {
233+
public function filetype(string $path): string|false {
237234
if ($this->is_dir($path)) {
238235
return 'dir';
239236
} elseif ($this->is_file($path)) {
@@ -243,7 +240,7 @@ public function filetype($path): string|false {
243240
}
244241
}
245242

246-
public function fopen($path, $mode) {
243+
public function fopen(string $path, string $mode) {
247244
$useExisting = true;
248245
switch ($mode) {
249246
case 'r':
@@ -309,7 +306,7 @@ public function readStream(string $path) {
309306
return $stream;
310307
}
311308

312-
public function touch($path, $mtime = null): bool {
309+
public function touch(string $path, ?int $mtime = null): bool {
313310
if ($this->file_exists($path)) {
314311
return false;
315312
} else {
@@ -318,12 +315,12 @@ public function touch($path, $mtime = null): bool {
318315
}
319316
}
320317

321-
public function rename($source, $target): bool {
318+
public function rename(string $source, string $target): bool {
322319
$this->unlink($target);
323320
return $this->getConnection()->rename($this->buildPath($source), $this->buildPath($target));
324321
}
325322

326-
public function getDirectoryContent($directory): \Traversable {
323+
public function getDirectoryContent(string $directory): \Traversable {
327324
$files = $this->getConnection()->mlsd($this->buildPath($directory));
328325
$mimeTypeDetector = \OC::$server->getMimeTypeDetector();
329326

0 commit comments

Comments
 (0)