Skip to content

Commit 128f56c

Browse files
authored
Merge pull request nextcloud#57820 from nextcloud/carl/fix-return-type-systemtagobjectmapper
2 parents 93c254f + 2d648e3 commit 128f56c

File tree

5 files changed

+46
-42
lines changed

5 files changed

+46
-42
lines changed

apps/dav/lib/SystemTag/SystemTagPlugin.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
6262
*/
6363
private $server;
6464

65-
/** @var array<int, string[]> */
65+
/** @var array<string, list<string>> */
6666
private array $cachedTagMappings = [];
6767
/** @var array<string, ISystemTag> */
6868
private array $cachedTags = [];
@@ -210,17 +210,17 @@ private function preloadCollection(
210210
}
211211

212212
if ($collection instanceof Directory
213-
&& !isset($this->cachedTagMappings[$collection->getId()])
213+
&& !isset($this->cachedTagMappings[(string)$collection->getId()])
214214
&& $propFind->getStatus(
215215
self::SYSTEM_TAGS_PROPERTYNAME
216216
) !== null) {
217-
$fileIds = [$collection->getId()];
217+
$fileIds = [(string)$collection->getId()];
218218

219219
// note: pre-fetching only supported for depth <= 1
220220
$folderContent = $collection->getChildren();
221221
foreach ($folderContent as $info) {
222222
if ($info instanceof Node) {
223-
$fileIds[] = $info->getId();
223+
$fileIds[] = (string)$info->getId();
224224
}
225225
}
226226

@@ -231,7 +231,7 @@ private function preloadCollection(
231231

232232
// also cache the ones that were not found
233233
foreach ($emptyFileIds as $fileId) {
234-
$this->cachedTagMappings[$fileId] = [];
234+
$this->cachedTagMappings[(string)$fileId] = [];
235235
}
236236
}
237237
}
@@ -350,10 +350,10 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
350350
* @return ISystemTag[]
351351
*/
352352
private function getTagsForFile(int $fileId, ?IUser $user): array {
353-
if (isset($this->cachedTagMappings[$fileId])) {
354-
$tagIds = $this->cachedTagMappings[$fileId];
353+
if (isset($this->cachedTagMappings[(string)$fileId])) {
354+
$tagIds = $this->cachedTagMappings[(string)$fileId];
355355
} else {
356-
$tags = $this->tagMapper->getTagIdsForObjects([$fileId], 'files');
356+
$tags = $this->tagMapper->getTagIdsForObjects([(string)$fileId], 'files');
357357
$fileTags = current($tags);
358358
if ($fileTags) {
359359
$tagIds = $fileTags;
@@ -362,13 +362,10 @@ private function getTagsForFile(int $fileId, ?IUser $user): array {
362362
}
363363
}
364364

365-
$tags = array_filter(array_map(function (string $tagId) {
366-
return $this->cachedTags[$tagId] ?? null;
367-
}, $tagIds));
365+
$tags = array_filter(array_map(
366+
fn (string $tagId): ?ISystemTag => $this->cachedTags[$tagId] ?? null, $tagIds));
368367

369-
$uncachedTagIds = array_filter($tagIds, function (string $tagId): bool {
370-
return !isset($this->cachedTags[$tagId]);
371-
});
368+
$uncachedTagIds = array_filter($tagIds, fn (string $tagId): bool => !isset($this->cachedTags[$tagId]));
372369

373370
if (count($uncachedTagIds)) {
374371
$retrievedTags = $this->tagManager->getTagsByIds($uncachedTagIds);

apps/systemtags/lib/Command/Files/DeleteAll.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ public function execute(InputInterface $input, OutputInterface $output): int {
3535
$targetInput = $input->getArgument('target');
3636
$targetNode = $this->fileUtils->getNode($targetInput);
3737

38-
if (! $targetNode) {
38+
if (!$targetNode) {
3939
$output->writeln("<error>file $targetInput not found</error>");
4040
return 1;
4141
}
4242

43-
$tags = $this->systemTagObjectMapper->getTagIdsForObjects([$targetNode->getId()], 'files');
44-
$this->systemTagObjectMapper->unassignTags((string)$targetNode->getId(), 'files', $tags[$targetNode->getId()]);
43+
$targetNodeId = (string)$targetNode->getId();
44+
$tags = $this->systemTagObjectMapper->getTagIdsForObjects([$targetNodeId], 'files');
45+
$this->systemTagObjectMapper->unassignTags($targetNodeId, 'files', $tags[$targetNodeId]);
4546
$output->writeln('<info>all tags removed.</info>');
4647

4748
return 0;

apps/systemtags/lib/Search/TagSearchProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
130130
$searchResultEntry = new SearchResultEntry(
131131
$thumbnailUrl,
132132
$result->getName(),
133-
$this->formatSubline($query, $matchedTags[$nodeId]),
133+
$this->formatSubline($query, $matchedTags[(string)$nodeId]),
134134
$this->urlGenerator->getAbsoluteURL($link),
135135
$result->getMimetype() === FileInfo::MIMETYPE_FOLDER
136136
? 'icon-folder'

lib/private/SystemTag/SystemTagObjectMapper.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OCP\SystemTag\TagAssignedEvent;
2121
use OCP\SystemTag\TagNotFoundException;
2222
use OCP\SystemTag\TagUnassignedEvent;
23+
use Override;
2324

2425
class SystemTagObjectMapper implements ISystemTagObjectMapper {
2526
public const RELATION_TABLE = 'systemtag_object_mapping';
@@ -31,9 +32,7 @@ public function __construct(
3132
) {
3233
}
3334

34-
/**
35-
* {@inheritdoc}
36-
*/
35+
#[Override]
3736
public function getTagIdsForObjects($objIds, string $objectType): array {
3837
if (!\is_array($objIds)) {
3938
$objIds = [$objIds];
@@ -59,7 +58,7 @@ public function getTagIdsForObjects($objIds, string $objectType): array {
5958
$result = $query->executeQuery();
6059
while ($row = $result->fetch()) {
6160
$objectId = $row['objectid'];
62-
$mapping[$objectId][] = $row['systemtagid'];
61+
$mapping[$objectId][] = (string)$row['systemtagid'];
6362
}
6463

6564
$result->closeCursor();
@@ -108,9 +107,7 @@ public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0,
108107
return $objectIds;
109108
}
110109

111-
/**
112-
* {@inheritdoc}
113-
*/
110+
#[Override]
114111
public function assignTags(string $objId, string $objectType, $tagIds): void {
115112
if (!\is_array($tagIds)) {
116113
$tagIds = [$tagIds];
@@ -168,18 +165,18 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
168165
return;
169166
}
170167

168+
$tagsAssigned = array_map(static fn (string $tagId): int => (int)$tagId, $tagsAssigned);
169+
171170
$this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent(
172171
MapperEvent::EVENT_ASSIGN,
173172
$objectType,
174173
$objId,
175-
$tagsAssigned
174+
$tagsAssigned,
176175
));
177176
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, [$objId], $tagsAssigned));
178177
}
179178

180-
/**
181-
* {@inheritdoc}
182-
*/
179+
#[Override]
183180
public function unassignTags(string $objId, string $objectType, $tagIds): void {
184181
if (!\is_array($tagIds)) {
185182
$tagIds = [$tagIds];
@@ -199,6 +196,9 @@ public function unassignTags(string $objId, string $objectType, $tagIds): void {
199196

200197
$this->updateEtagForTags($tagIds);
201198

199+
// convert ids to int because the event uses ints
200+
$tagIds = array_map(static fn (string $tagId): int => (int)$tagId, $tagIds);
201+
202202
$this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
203203
MapperEvent::EVENT_UNASSIGN,
204204
$objectType,

lib/public/SystemTag/ISystemTagObjectMapper.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,36 @@
88
*/
99
namespace OCP\SystemTag;
1010

11+
use OCP\AppFramework\Attribute\Consumable;
12+
1113
/**
1214
* Public interface to access and manage system-wide tags.
1315
*
1416
* @since 9.0.0
1517
*/
18+
#[Consumable(since: '9.0.0')]
1619
interface ISystemTagObjectMapper {
1720
/**
1821
* Get a list of tag ids for the given object ids.
1922
*
2023
* This returns an array that maps object id to tag ids
24+
*
25+
* ```
2126
* [
22-
* 1 => array('id1', 'id2'),
23-
* 2 => array('id3', 'id2'),
24-
* 3 => array('id5'),
25-
* 4 => array()
27+
* 1 => ['id1', 'id2'],
28+
* 2 => ['id3', 'id2'],
29+
* 3 => ['id5'],
30+
* 4 => []
2631
* ]
32+
* ```
2733
*
2834
* Untagged objects will have an empty array associated.
2935
*
30-
* @param string|array $objIds object ids
36+
* @param string|list<string> $objIds object ids
3137
* @param string $objectType object type
3238
*
33-
* @return array with object id as key and an array
34-
* of tag ids as value
39+
* @return array<string, list<string>> with object id as key and an array
40+
* of tag ids as value
3541
*
3642
* @since 9.0.0
3743
*/
@@ -40,12 +46,12 @@ public function getTagIdsForObjects($objIds, string $objectType): array;
4046
/**
4147
* Get a list of objects tagged with $tagIds.
4248
*
43-
* @param string|array $tagIds Tag id or array of tag ids.
49+
* @param string|list<string> $tagIds Tag id or array of tag ids.
4450
* @param string $objectType object type
4551
* @param int $limit Count of object ids you want to get
4652
* @param string $offset The last object id you already received
4753
*
48-
* @return string[] array of object ids or empty array if none found
54+
* @return list<string> array of object ids or empty array if none found
4955
*
5056
* @throws TagNotFoundException if at least one of the
5157
* given tags does not exist
@@ -66,7 +72,7 @@ public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0,
6672
*
6773
* @param string $objId object id
6874
* @param string $objectType object type
69-
* @param string|array $tagIds tag id or array of tag ids to assign
75+
* @param string|list<string> $tagIds tag id or array of tag ids to assign
7076
*
7177
* @throws TagNotFoundException if at least one of the
7278
* given tags does not exist
@@ -85,7 +91,7 @@ public function assignTags(string $objId, string $objectType, $tagIds);
8591
*
8692
* @param string $objId object id
8793
* @param string $objectType object type
88-
* @param string|array $tagIds tag id or array of tag ids to unassign
94+
* @param string|list<string> $tagIds tag id or array of tag ids to unassign
8995
*
9096
* @throws TagNotFoundException if at least one of the
9197
* given tags does not exist
@@ -97,7 +103,7 @@ public function unassignTags(string $objId, string $objectType, $tagIds);
97103
/**
98104
* Checks whether the given objects have the given tag.
99105
*
100-
* @param string|array $objIds object ids
106+
* @param string|list<string> $objIds object ids
101107
* @param string $objectType object type
102108
* @param string $tagId tag id to check
103109
* @param bool $all true to check that ALL objects have the tag assigned,
@@ -128,7 +134,7 @@ public function getAvailableObjectTypes(): array;
128134
*
129135
* @param string $tagId tag id
130136
* @param string $objectType object type
131-
* @param string[] $objectIds list of object ids
137+
* @param list<string> $objectIds list of object ids
132138
*
133139
* @throws TagNotFoundException if the tag does not exist
134140
* @since 31.0.0

0 commit comments

Comments
 (0)