Skip to content

Commit ffeb797

Browse files
committed
refactor(mimeloader): modernize MimeTypeLoader
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent f316edb commit ffeb797

File tree

4 files changed

+27
-40
lines changed

4 files changed

+27
-40
lines changed

lib/private/Files/Cache/SearchBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,18 @@ private function getOperatorFieldAndValueInner(string $field, mixed $value, stri
228228
if ($field === 'mimetype') {
229229
$value = (string)$value;
230230
if ($type === ISearchComparison::COMPARE_EQUAL) {
231-
$value = (int)$this->mimetypeLoader->getId($value);
231+
$value = $this->mimetypeLoader->getId($value);
232232
} elseif ($type === ISearchComparison::COMPARE_LIKE) {
233233
// transform "mimetype='foo/%'" to "mimepart='foo'"
234234
if (preg_match('|(.+)/%|', $value, $matches)) {
235235
$field = 'mimepart';
236-
$value = (int)$this->mimetypeLoader->getId($matches[1]);
236+
$value = $this->mimetypeLoader->getId($matches[1]);
237237
$type = ISearchComparison::COMPARE_EQUAL;
238238
} elseif (str_contains($value, '%')) {
239239
throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
240240
} else {
241241
$field = 'mimetype';
242-
$value = (int)$this->mimetypeLoader->getId($value);
242+
$value = $this->mimetypeLoader->getId($value);
243243
$type = ISearchComparison::COMPARE_EQUAL;
244244
}
245245
}

lib/private/Files/Type/Loader.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@
3838
class Loader implements IMimeTypeLoader {
3939
use TTransactional;
4040

41-
/** @var IDBConnection */
42-
private $dbConnection;
41+
private IDBConnection $dbConnection;
4342

44-
/** @var array [id => mimetype] */
45-
protected $mimetypes;
43+
/** @psalm-var array<int, string> */
44+
protected array $mimetypes;
4645

47-
/** @var array [mimetype => id] */
48-
protected $mimetypeIds;
46+
/** @psalm-var array<string, int> */
47+
protected array $mimetypeIds;
4948

5049
/**
5150
* @param IDBConnection $dbConnection
@@ -58,11 +57,8 @@ public function __construct(IDBConnection $dbConnection) {
5857

5958
/**
6059
* Get a mimetype from its ID
61-
*
62-
* @param int $id
63-
* @return string|null
6460
*/
65-
public function getMimetypeById($id) {
61+
public function getMimetypeById(int $id): ?string {
6662
if (!$this->mimetypes) {
6763
$this->loadMimetypes();
6864
}
@@ -74,11 +70,8 @@ public function getMimetypeById($id) {
7470

7571
/**
7672
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
77-
*
78-
* @param string $mimetype
79-
* @return int
8073
*/
81-
public function getId($mimetype) {
74+
public function getId(string $mimetype): int {
8275
if (!$this->mimetypeIds) {
8376
$this->loadMimetypes();
8477
}
@@ -90,11 +83,8 @@ public function getId($mimetype) {
9083

9184
/**
9285
* Test if a mimetype exists in the database
93-
*
94-
* @param string $mimetype
95-
* @return bool
9686
*/
97-
public function exists($mimetype) {
87+
public function exists(string $mimetype): bool {
9888
if (!$this->mimetypeIds) {
9989
$this->loadMimetypes();
10090
}
@@ -104,7 +94,7 @@ public function exists($mimetype) {
10494
/**
10595
* Clear all loaded mimetypes, allow for re-loading
10696
*/
107-
public function reset() {
97+
public function reset(): void {
10898
$this->mimetypes = [];
10999
$this->mimetypeIds = [];
110100
}
@@ -115,7 +105,7 @@ public function reset() {
115105
* @param string $mimetype
116106
* @return int inserted ID
117107
*/
118-
protected function store($mimetype) {
108+
protected function store(string $mimetype): int {
119109
try {
120110
$mimetypeId = $this->atomic(function () use ($mimetype) {
121111
$insert = $this->dbConnection->getQueryBuilder();
@@ -153,29 +143,27 @@ protected function store($mimetype) {
153143
/**
154144
* Load all mimetypes from DB
155145
*/
156-
private function loadMimetypes() {
146+
private function loadMimetypes(): void {
157147
$qb = $this->dbConnection->getQueryBuilder();
158148
$qb->select('id', 'mimetype')
159149
->from('mimetypes');
160150

161-
$result = $qb->execute();
151+
$result = $qb->executeQuery();
162152
$results = $result->fetchAll();
163153
$result->closeCursor();
164154

165155
foreach ($results as $row) {
166-
$this->mimetypes[$row['id']] = $row['mimetype'];
167-
$this->mimetypeIds[$row['mimetype']] = $row['id'];
156+
$this->mimetypes[(int) $row['id']] = $row['mimetype'];
157+
$this->mimetypeIds[$row['mimetype']] = (int) $row['id'];
168158
}
169159
}
170160

171161
/**
172162
* Update filecache mimetype based on file extension
173163
*
174-
* @param string $ext file extension
175-
* @param int $mimeTypeId
176164
* @return int number of changed rows
177165
*/
178-
public function updateFilecache($ext, $mimeTypeId) {
166+
public function updateFilecache(string $ext, int $mimeTypeId): int {
179167
$folderMimeTypeId = $this->getId('httpd/unix-directory');
180168
$update = $this->dbConnection->getQueryBuilder();
181169
$update->update('filecache')

lib/public/Files/IMimeTypeLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface IMimeTypeLoader {
3535
* @return string|null
3636
* @since 8.2.0
3737
*/
38-
public function getMimetypeById($id);
38+
public function getMimetypeById(int $id): ?string;
3939

4040
/**
4141
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
@@ -44,7 +44,7 @@ public function getMimetypeById($id);
4444
* @return int
4545
* @since 8.2.0
4646
*/
47-
public function getId($mimetype);
47+
public function getId(string $mimetype): int;
4848

4949
/**
5050
* Test if a mimetype exists in the database
@@ -53,12 +53,12 @@ public function getId($mimetype);
5353
* @return bool
5454
* @since 8.2.0
5555
*/
56-
public function exists($mimetype);
56+
public function exists(string $mimetype): bool;
5757

5858
/**
5959
* Clear all loaded mimetypes, allow for re-loading
6060
*
6161
* @since 8.2.0
6262
*/
63-
public function reset();
63+
public function reset(): void;
6464
}

tests/lib/Files/Type/LoaderTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323

2424
use OC\Files\Type\Loader;
2525
use OCP\IDBConnection;
26+
use Test\TestCase;
2627

27-
class LoaderTest extends \Test\TestCase {
28-
/** @var IDBConnection */
29-
protected $db;
30-
/** @var Loader */
31-
protected $loader;
28+
class LoaderTest extends TestCase {
29+
protected IDBConnection $db;
30+
protected Loader $loader;
3231

3332
protected function setUp(): void {
34-
$this->db = \OC::$server->getDatabaseConnection();
33+
$this->db = \OC::$server->get(IDBConnection::class);
3534
$this->loader = new Loader($this->db);
3635
}
3736

0 commit comments

Comments
 (0)