Skip to content

Commit c03b784

Browse files
committed
fix(psalm): systemtags
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
1 parent 74ced3d commit c03b784

File tree

8 files changed

+103
-17
lines changed

8 files changed

+103
-17
lines changed

apps/dav/lib/SystemTag/SystemTagList.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public function __construct(array $tags, ISystemTagManager $tagManager, IUser $u
4949
$this->user = $user;
5050
}
5151

52-
public function getTags() {
52+
/**
53+
* @return ISystemTag[]
54+
*/
55+
public function getTags(): array {
5356
return $this->tags;
5457
}
5558

apps/dav/lib/SystemTag/SystemTagMappingNode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public function getName() {
137137
* @param string $name The new name
138138
*
139139
* @throws MethodNotAllowed not allowed to rename node
140+
*
141+
* @return never
140142
*/
141143
public function setName($name) {
142144
throw new MethodNotAllowed();
@@ -145,13 +147,16 @@ public function setName($name) {
145147
/**
146148
* Returns null, not supported
147149
*
150+
* @return null
148151
*/
149152
public function getLastModified() {
150153
return null;
151154
}
152155

153156
/**
154157
* Delete tag to object association
158+
*
159+
* @return void
155160
*/
156161
public function delete() {
157162
try {

apps/dav/lib/SystemTag/SystemTagNode.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public function getSystemTag() {
103103
* @param string $name The new name
104104
*
105105
* @throws MethodNotAllowed not allowed to rename node
106+
*
107+
* @return never
106108
*/
107109
public function setName($name) {
108110
throw new MethodNotAllowed();
@@ -114,11 +116,12 @@ public function setName($name) {
114116
* @param string $name new tag name
115117
* @param bool $userVisible user visible
116118
* @param bool $userAssignable user assignable
119+
*
117120
* @throws NotFound whenever the given tag id does not exist
118121
* @throws Forbidden whenever there is no permission to update said tag
119122
* @throws Conflict whenever a tag already exists with the given attributes
120123
*/
121-
public function update($name, $userVisible, $userAssignable) {
124+
public function update($name, $userVisible, $userAssignable): void {
122125
try {
123126
if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
124127
throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
@@ -151,11 +154,15 @@ public function update($name, $userVisible, $userAssignable) {
151154
/**
152155
* Returns null, not supported
153156
*
157+
* @return null
154158
*/
155159
public function getLastModified() {
156160
return null;
157161
}
158162

163+
/**
164+
* @return void
165+
*/
159166
public function delete() {
160167
try {
161168
if (!$this->isAdmin) {

apps/dav/lib/SystemTag/SystemTagPlugin.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCA\DAV\Connector\Sabre\Directory;
2929
use OCA\DAV\Connector\Sabre\Node;
3030
use OCP\IGroupManager;
31+
use OCP\IUser;
3132
use OCP\IUserSession;
3233
use OCP\SystemTag\ISystemTag;
3334
use OCP\SystemTag\ISystemTagManager;
@@ -83,7 +84,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
8384

8485
/** @var array<int, int[]> */
8586
private array $cachedTagMappings = [];
86-
/** @var array<int, ISystemTag> */
87+
/** @var array<string, ISystemTag> */
8788
private array $cachedTags = [];
8889

8990
private ISystemTagObjectMapper $tagMapper;
@@ -225,6 +226,8 @@ private function createTag($data, $contentType = 'application/json') {
225226
*
226227
* @param PropFind $propFind
227228
* @param \Sabre\DAV\INode $node
229+
*
230+
* @return void
228231
*/
229232
public function handleGetProperties(
230233
PropFind $propFind,
@@ -279,34 +282,42 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
279282
if ($node instanceof Directory
280283
&& $propFind->getDepth() !== 0
281284
&& !is_null($propFind->getStatus(self::SYSTEM_TAGS_PROPERTYNAME))) {
285+
$fileIds = [$node->getId()];
286+
282287
// note: pre-fetching only supported for depth <= 1
283288
$folderContent = $node->getNode()->getDirectoryListing();
284-
$fileIds[] = (int)$node->getId();
285289
foreach ($folderContent as $info) {
286-
$fileIds[] = (int)$info->getId();
290+
$fileIds[] = $info->getId();
287291
}
292+
288293
$tags = $this->tagMapper->getTagIdsForObjects($fileIds, 'files');
289294

290295
$this->cachedTagMappings = $this->cachedTagMappings + $tags;
291296
$emptyFileIds = array_diff($fileIds, array_keys($tags));
297+
292298
// also cache the ones that were not found
293299
foreach ($emptyFileIds as $fileId) {
294300
$this->cachedTagMappings[$fileId] = [];
295301
}
296302
}
297303

298304
$propFind->handle(self::SYSTEM_TAGS_PROPERTYNAME, function () use ($node) {
299-
$tags = $this->getTagsForFile($node->getId());
300-
return new SystemTagList($tags, $this->tagManager, $this->userSession->getUser());
305+
$user = $this->userSession->getUser();
306+
if ($user === null) {
307+
return;
308+
}
309+
310+
$tags = $this->getTagsForFile($node->getId(), $user);
311+
return new SystemTagList($tags, $this->tagManager, $user);
301312
});
302313
}
303314

304315
/**
305316
* @param int $fileId
306317
* @return ISystemTag[]
307318
*/
308-
private function getTagsForFile(int $fileId): array {
309-
$user = $this->userSession->getUser();
319+
private function getTagsForFile(int $fileId, IUser $user): array {
320+
310321
if (isset($this->cachedTagMappings[$fileId])) {
311322
$tagIds = $this->cachedTagMappings[$fileId];
312323
} else {
@@ -318,20 +329,23 @@ private function getTagsForFile(int $fileId): array {
318329
$tagIds = [];
319330
}
320331
}
321-
$tags = array_filter(array_map(function($tagId) {
332+
333+
$tags = array_filter(array_map(function(string $tagId) {
322334
return $this->cachedTags[$tagId] ?? null;
323335
}, $tagIds));
324336

325-
$uncachedTagIds = array_filter($tagIds, function($tagId): bool {
337+
$uncachedTagIds = array_filter($tagIds, function(string $tagId): bool {
326338
return !isset($this->cachedTags[$tagId]);
327339
});
340+
328341
if (count($uncachedTagIds)) {
329342
$retrievedTags = $this->tagManager->getTagsByIds($uncachedTagIds);
330343
foreach ($retrievedTags as $tag) {
331344
$this->cachedTags[$tag->getId()] = $tag;
332345
}
333346
$tags += $retrievedTags;
334347
}
348+
335349
return array_filter($tags, function(ISystemTag $tag) use ($user) {
336350
return $this->tagManager->canUserSeeTag($tag, $user);
337351
});

apps/dav/lib/SystemTag/SystemTagsByIdCollection.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,28 @@ private function isAdmin() {
8484
/**
8585
* @param string $name
8686
* @param resource|string $data Initial payload
87+
*
8788
* @throws Forbidden
89+
*
90+
* @return never
8891
*/
8992
public function createFile($name, $data = null) {
9093
throw new Forbidden('Cannot create tags by id');
9194
}
9295

9396
/**
9497
* @param string $name
98+
*
99+
* @return never
95100
*/
96101
public function createDirectory($name) {
97102
throw new Forbidden('Permission denied to create collections');
98103
}
99104

100105
/**
101106
* @param string $name
107+
*
108+
* @return SystemTagNode
102109
*/
103110
public function getChild($name) {
104111
try {
@@ -115,6 +122,11 @@ public function getChild($name) {
115122
}
116123
}
117124

125+
/**
126+
* @return SystemTagNode[]
127+
*
128+
* @psalm-return array<SystemTagNode>
129+
*/
118130
public function getChildren() {
119131
$visibilityFilter = true;
120132
if ($this->isAdmin()) {
@@ -145,22 +157,33 @@ public function childExists($name) {
145157
}
146158
}
147159

160+
/**
161+
* @return never
162+
*/
148163
public function delete() {
149164
throw new Forbidden('Permission denied to delete this collection');
150165
}
151166

167+
/**
168+
* @return string
169+
*
170+
* @psalm-return 'systemtags'
171+
*/
152172
public function getName() {
153173
return 'systemtags';
154174
}
155175

176+
/**
177+
* @return never
178+
*/
156179
public function setName($name) {
157180
throw new Forbidden('Permission denied to rename this collection');
158181
}
159182

160183
/**
161184
* Returns the last modification time, as a unix timestamp
162185
*
163-
* @return int
186+
* @return null
164187
*/
165188
public function getLastModified() {
166189
return null;

apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public function __construct(
9292
$this->user = $user;
9393
}
9494

95+
/**
96+
* @return void
97+
*/
9598
public function createFile($name, $data = null) {
9699
$tagId = $name;
97100
try {
@@ -110,10 +113,16 @@ public function createFile($name, $data = null) {
110113
}
111114
}
112115

116+
/**
117+
* @return never
118+
*/
113119
public function createDirectory($name) {
114120
throw new Forbidden('Permission denied to create collections');
115121
}
116122

123+
/**
124+
* @return SystemTagMappingNode
125+
*/
117126
public function getChild($tagName) {
118127
try {
119128
if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagName, true)) {
@@ -131,6 +140,11 @@ public function getChild($tagName) {
131140
}
132141
}
133142

143+
/**
144+
* @return SystemTagMappingNode[]
145+
*
146+
* @psalm-return list<SystemTagMappingNode>
147+
*/
134148
public function getChildren() {
135149
$tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType));
136150
if (empty($tagIds)) {
@@ -168,6 +182,9 @@ public function childExists($tagId) {
168182
}
169183
}
170184

185+
/**
186+
* @return never
187+
*/
171188
public function delete() {
172189
throw new Forbidden('Permission denied to delete this collection');
173190
}
@@ -176,14 +193,17 @@ public function getName() {
176193
return $this->objectId;
177194
}
178195

196+
/**
197+
* @return never
198+
*/
179199
public function setName($name) {
180200
throw new Forbidden('Permission denied to rename this collection');
181201
}
182202

183203
/**
184204
* Returns the last modification time, as a unix timestamp
185205
*
186-
* @return int
206+
* @return null
187207
*/
188208
public function getLastModified() {
189209
return null;

apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public function __construct(
9898
/**
9999
* @param string $name
100100
* @param resource|string $data Initial payload
101-
* @return null|string
101+
*
102+
* @return never
103+
*
102104
* @throws Forbidden
103105
*/
104106
public function createFile($name, $data = null) {
@@ -107,7 +109,10 @@ public function createFile($name, $data = null) {
107109

108110
/**
109111
* @param string $name
112+
*
110113
* @throws Forbidden
114+
*
115+
* @return never
111116
*/
112117
public function createDirectory($name) {
113118
throw new Forbidden('Permission denied to create collections');
@@ -133,6 +138,9 @@ public function getChild($objectName) {
133138
);
134139
}
135140

141+
/**
142+
* @return never
143+
*/
136144
public function getChildren() {
137145
// do not list object ids
138146
throw new MethodNotAllowed();
@@ -148,6 +156,9 @@ public function childExists($name) {
148156
return call_user_func($this->childExistsFunction, $name);
149157
}
150158

159+
/**
160+
* @return never
161+
*/
151162
public function delete() {
152163
throw new Forbidden('Permission denied to delete this collection');
153164
}
@@ -158,7 +169,10 @@ public function getName() {
158169

159170
/**
160171
* @param string $name
172+
*
161173
* @throws Forbidden
174+
*
175+
* @return never
162176
*/
163177
public function setName($name) {
164178
throw new Forbidden('Permission denied to rename this collection');
@@ -167,7 +181,7 @@ public function setName($name) {
167181
/**
168182
* Returns the last modification time, as a unix timestamp
169183
*
170-
* @return int
184+
* @return null
171185
*/
172186
public function getLastModified() {
173187
return null;

0 commit comments

Comments
 (0)