Skip to content

Commit

Permalink
Refactors lib/private/SystemTag.
Browse files Browse the repository at this point in the history
Mainly using PHP8's constructor property promotion.

Signed-off-by: Faraz Samapoor <[email protected]>
  • Loading branch information
fsamapoor authored and Faraz Samapoor committed Jun 25, 2023
1 parent 2de859d commit ad7d431
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 108 deletions.
16 changes: 3 additions & 13 deletions lib/private/SystemTag/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,17 @@
* @since 9.0.0
*/
class ManagerFactory implements ISystemTagManagerFactory {
/**
* Server container
*
* @var IServerContainer
*/
private $serverContainer;

/**
* Constructor for the system tag manager factory
*
* @param IServerContainer $serverContainer server container
*/
public function __construct(IServerContainer $serverContainer) {
$this->serverContainer = $serverContainer;
public function __construct(
private IServerContainer $serverContainer,
) {
}

/**
* Creates and returns an instance of the system tag manager
*
* @return ISystemTagManager
* @since 9.0.0
*/
public function getManager(): ISystemTagManager {
Expand All @@ -72,7 +63,6 @@ public function getManager(): ISystemTagManager {
* Creates and returns an instance of the system tag object
* mapper
*
* @return ISystemTagObjectMapper
* @since 9.0.0
*/
public function getObjectMapper(): ISystemTagObjectMapper {
Expand Down
53 changes: 13 additions & 40 deletions lib/private/SystemTag/SystemTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,12 @@
use OCP\SystemTag\ISystemTag;

class SystemTag implements ISystemTag {
/**
* @var string
*/
private $id;

/**
* @var string
*/
private $name;

/**
* @var bool
*/
private $userVisible;

/**
* @var bool
*/
private $userAssignable;

/**
* Constructor.
*
* @param string $id tag id
* @param string $name tag name
* @param bool $userVisible whether the tag is user visible
* @param bool $userAssignable whether the tag is user assignable
*/
public function __construct(string $id, string $name, bool $userVisible, bool $userAssignable) {
$this->id = $id;
$this->name = $name;
$this->userVisible = $userVisible;
$this->userAssignable = $userAssignable;
public function __construct(
private string $id,
private string $name,
private bool $userVisible,
private bool $userAssignable,
) {
}

/**
Expand Down Expand Up @@ -97,14 +70,14 @@ public function isUserAssignable(): bool {
* {@inheritdoc}
*/
public function getAccessLevel(): int {
if ($this->userVisible) {
if ($this->userAssignable) {
return self::ACCESS_LEVEL_PUBLIC;
} else {
return self::ACCESS_LEVEL_RESTRICTED;
}
} else {
if (!$this->userVisible) {
return self::ACCESS_LEVEL_INVISIBLE;
}

if (!$this->userAssignable) {
return self::ACCESS_LEVEL_RESTRICTED;
}

return self::ACCESS_LEVEL_PUBLIC;
}
}
43 changes: 13 additions & 30 deletions lib/private/SystemTag/SystemTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,16 @@ class SystemTagManager implements ISystemTagManager {
public const TAG_TABLE = 'systemtag';
public const TAG_GROUP_TABLE = 'systemtag_group';

/** @var IDBConnection */
protected $connection;

/** @var EventDispatcherInterface */
protected $dispatcher;

/** @var IGroupManager */
protected $groupManager;

/**
* Prepared query for selecting tags directly
*
* @var \OCP\DB\QueryBuilder\IQueryBuilder
*/
private $selectTagQuery;
private IQueryBuilder $selectTagQuery;

/**
* Constructor.
*
* @param IDBConnection $connection database connection
* @param IGroupManager $groupManager
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(
IDBConnection $connection,
IGroupManager $groupManager,
EventDispatcherInterface $dispatcher
protected IDBConnection $connection,
protected IGroupManager $groupManager,
protected EventDispatcherInterface $dispatcher,
) {
$this->connection = $connection;
$this->groupManager = $groupManager;
$this->dispatcher = $dispatcher;

$query = $this->connection->getQueryBuilder();
$this->selectTagQuery = $query->select('*')
->from(self::TAG_TABLE)
Expand Down Expand Up @@ -232,7 +210,12 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab
/**
* {@inheritdoc}
*/
public function updateTag(string $tagId, string $newName, bool $userVisible, bool $userAssignable) {
public function updateTag(
string $tagId,
string $newName,
bool $userVisible,
bool $userAssignable,
): void {
try {
$tags = $this->getTagsByIds($tagId);
} catch (TagNotFoundException $e) {
Expand Down Expand Up @@ -282,7 +265,7 @@ public function updateTag(string $tagId, string $newName, bool $userVisible, boo
/**
* {@inheritdoc}
*/
public function deleteTags($tagIds) {
public function deleteTags($tagIds): void {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
}
Expand Down Expand Up @@ -374,14 +357,14 @@ public function canUserSeeTag(ISystemTag $tag, IUser $user): bool {
return false;
}

private function createSystemTagFromRow($row) {
private function createSystemTagFromRow($row): SystemTag {
return new SystemTag((string)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']);
}

/**
* {@inheritdoc}
*/
public function setTagGroups(ISystemTag $tag, array $groupIds) {
public function setTagGroups(ISystemTag $tag, array $groupIds): void {
// delete relationships first
$this->connection->beginTransaction();
try {
Expand Down
32 changes: 8 additions & 24 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,11 @@
class SystemTagObjectMapper implements ISystemTagObjectMapper {
public const RELATION_TABLE = 'systemtag_object_mapping';

/** @var ISystemTagManager */
protected $tagManager;

/** @var IDBConnection */
protected $connection;

/** @var EventDispatcherInterface */
protected $dispatcher;

/**
* Constructor.
*
* @param IDBConnection $connection database connection
* @param ISystemTagManager $tagManager system tag manager
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) {
$this->connection = $connection;
$this->tagManager = $tagManager;
$this->dispatcher = $dispatcher;
public function __construct(
protected IDBConnection $connection,
protected ISystemTagManager $tagManager,
protected EventDispatcherInterface $dispatcher,
) {
}

/**
Expand Down Expand Up @@ -96,7 +81,6 @@ public function getTagIdsForObjects($objIds, string $objectType): array {
$result->closeCursor();
}


return $mapping;
}

Expand Down Expand Up @@ -142,7 +126,7 @@ public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0,
/**
* {@inheritdoc}
*/
public function assignTags(string $objId, string $objectType, $tagIds) {
public function assignTags(string $objId, string $objectType, $tagIds): void {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
}
Expand Down Expand Up @@ -183,7 +167,7 @@ public function assignTags(string $objId, string $objectType, $tagIds) {
/**
* {@inheritdoc}
*/
public function unassignTags(string $objId, string $objectType, $tagIds) {
public function unassignTags(string $objId, string $objectType, $tagIds): void {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
}
Expand Down Expand Up @@ -255,7 +239,7 @@ public function haveTag($objIds, string $objectType, string $tagId, bool $all =
*
* @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
*/
private function assertTagsExist($tagIds) {
private function assertTagsExist(array $tagIds): void {
$tags = $this->tagManager->getTagsByIds($tagIds);
if (\count($tags) !== \count($tagIds)) {
// at least one tag missing, bail out
Expand Down
4 changes: 3 additions & 1 deletion lib/private/SystemTag/SystemTagsInFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
use OCP\Files\Search\ISearchComparison;

class SystemTagsInFilesDetector {
public function __construct(protected QuerySearchHelper $searchHelper) {
public function __construct(
protected QuerySearchHelper $searchHelper,
) {
}

public function detectAssignedSystemTagsIn(
Expand Down

0 comments on commit ad7d431

Please sign in to comment.