diff options
Diffstat (limited to 'lib/private/Comments')
-rw-r--r-- | lib/private/Comments/Comment.php | 135 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 106 |
2 files changed, 82 insertions, 159 deletions
diff --git a/lib/private/Comments/Comment.php b/lib/private/Comments/Comment.php index 4284beb3fcc..35e88c74438 100644 --- a/lib/private/Comments/Comment.php +++ b/lib/private/Comments/Comment.php @@ -30,7 +30,7 @@ use OCP\Comments\IllegalIDChangeException; use OCP\Comments\MessageTooLongException; class Comment implements IComment { - protected $data = [ + protected array $data = [ 'id' => '', 'parentId' => '0', 'topmostParentId' => '0', @@ -61,21 +61,20 @@ class Comment implements IComment { } /** - * returns the ID of the comment + * Returns the ID of the comment * * It may return an empty string, if the comment was not stored. * It is expected that the concrete Comment implementation gives an ID * by itself (e.g. after saving). * - * @return string * @since 9.0.0 */ - public function getId() { + public function getId(): string { return $this->data['id']; } /** - * sets the ID of the comment and returns itself + * Sets the ID of the comment and returns itself * * It is only allowed to set the ID only, if the current id is an empty * string (which means it is not stored in a database, storage or whatever @@ -87,7 +86,7 @@ class Comment implements IComment { * @throws IllegalIDChangeException * @since 9.0.0 */ - public function setId($id) { + public function setId($id): IComment { if (!is_string($id)) { throw new \InvalidArgumentException('String expected.'); } @@ -102,23 +101,21 @@ class Comment implements IComment { } /** - * returns the parent ID of the comment + * Returns the parent ID of the comment * - * @return string * @since 9.0.0 */ - public function getParentId() { + public function getParentId(): string { return $this->data['parentId']; } /** - * sets the parent ID and returns itself + * Sets the parent ID and returns itself * * @param string $parentId - * @return IComment * @since 9.0.0 */ - public function setParentId($parentId) { + public function setParentId($parentId): IComment { if (!is_string($parentId)) { throw new \InvalidArgumentException('String expected.'); } @@ -127,24 +124,22 @@ class Comment implements IComment { } /** - * returns the topmost parent ID of the comment + * Returns the topmost parent ID of the comment * - * @return string * @since 9.0.0 */ - public function getTopmostParentId() { + public function getTopmostParentId(): string { return $this->data['topmostParentId']; } /** - * sets the topmost parent ID and returns itself + * Sets the topmost parent ID and returns itself * * @param string $id - * @return IComment * @since 9.0.0 */ - public function setTopmostParentId($id) { + public function setTopmostParentId($id): IComment { if (!is_string($id)) { throw new \InvalidArgumentException('String expected.'); } @@ -153,23 +148,21 @@ class Comment implements IComment { } /** - * returns the number of children + * Returns the number of children * - * @return int * @since 9.0.0 */ - public function getChildrenCount() { + public function getChildrenCount(): int { return $this->data['childrenCount']; } /** - * sets the number of children + * Sets the number of children * * @param int $count - * @return IComment * @since 9.0.0 */ - public function setChildrenCount($count) { + public function setChildrenCount($count): IComment { if (!is_int($count)) { throw new \InvalidArgumentException('Integer expected.'); } @@ -178,12 +171,10 @@ class Comment implements IComment { } /** - * returns the message of the comment - * - * @return string + * Returns the message of the comment * @since 9.0.0 */ - public function getMessage() { + public function getMessage(): string { return $this->data['message']; } @@ -192,11 +183,10 @@ class Comment implements IComment { * * @param string $message * @param int $maxLength - * @return IComment * @throws MessageTooLongException * @since 9.0.0 */ - public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { + public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH): IComment { if (!is_string($message)) { throw new \InvalidArgumentException('String expected.'); } @@ -228,9 +218,9 @@ class Comment implements IComment { * ] * */ - public function getMentions() { + public function getMentions(): array { $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"group\/[a-z0-9_\-@\.\' ]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); - if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { + if (!$ok || !isset($mentions[0])) { return []; } $mentionIds = array_unique($mentions[0]); @@ -252,23 +242,21 @@ class Comment implements IComment { } /** - * returns the verb of the comment + * Returns the verb of the comment * - * @return string * @since 9.0.0 */ - public function getVerb() { + public function getVerb(): string { return $this->data['verb']; } /** - * sets the verb of the comment, e.g. 'comment' or 'like' + * Sets the verb of the comment, e.g. 'comment' or 'like' * * @param string $verb - * @return IComment * @since 9.0.0 */ - public function setVerb($verb) { + public function setVerb($verb): IComment { if (!is_string($verb) || !trim($verb)) { throw new \InvalidArgumentException('Non-empty String expected.'); } @@ -277,34 +265,29 @@ class Comment implements IComment { } /** - * returns the actor type - * - * @return string + * Returns the actor type * @since 9.0.0 */ - public function getActorType() { + public function getActorType(): string { return $this->data['actorType']; } /** - * returns the actor ID - * - * @return string + * Returns the actor ID * @since 9.0.0 */ - public function getActorId() { + public function getActorId(): string { return $this->data['actorId']; } /** - * sets (overwrites) the actor type and id + * Sets (overwrites) the actor type and id * * @param string $actorType e.g. 'users' * @param string $actorId e.g. 'zombie234' - * @return IComment * @since 9.0.0 */ - public function setActor($actorType, $actorId) { + public function setActor($actorType, $actorId): IComment { if ( !is_string($actorType) || !trim($actorType) || !is_string($actorId) || $actorId === '' @@ -317,76 +300,68 @@ class Comment implements IComment { } /** - * returns the creation date of the comment. + * Returns the creation date of the comment. * * If not explicitly set, it shall default to the time of initialization. - * - * @return \DateTime * @since 9.0.0 + * @throw \LogicException if creation date time is not set yet */ - public function getCreationDateTime() { + public function getCreationDateTime(): \DateTime { + if (!isset($this->data['creationDT'])) { + throw new \LogicException('Cannot get creation date before setting one or writting to database'); + } return $this->data['creationDT']; } /** - * sets the creation date of the comment and returns itself - * - * @param \DateTime $timestamp - * @return IComment + * Sets the creation date of the comment and returns itself * @since 9.0.0 */ - public function setCreationDateTime(\DateTime $timestamp) { - $this->data['creationDT'] = $timestamp; + public function setCreationDateTime(\DateTime $dateTime): IComment { + $this->data['creationDT'] = $dateTime; return $this; } /** - * returns the DateTime of the most recent child, if set, otherwise null - * - * @return \DateTime|null + * Returns the DateTime of the most recent child, if set, otherwise null * @since 9.0.0 */ - public function getLatestChildDateTime() { + public function getLatestChildDateTime(): ?\DateTime { return $this->data['latestChildDT']; } /** * @inheritDoc */ - public function setLatestChildDateTime(?\DateTime $dateTime = null) { + public function setLatestChildDateTime(?\DateTime $dateTime = null): IComment { $this->data['latestChildDT'] = $dateTime; return $this; } /** - * returns the object type the comment is attached to - * - * @return string + * Returns the object type the comment is attached to * @since 9.0.0 */ - public function getObjectType() { + public function getObjectType(): string { return $this->data['objectType']; } /** - * returns the object id the comment is attached to - * - * @return string + * Returns the object id the comment is attached to * @since 9.0.0 */ - public function getObjectId() { + public function getObjectId(): string { return $this->data['objectId']; } /** - * sets (overwrites) the object of the comment + * Sets (overwrites) the object of the comment * * @param string $objectType e.g. 'files' * @param string $objectId e.g. '16435' - * @return IComment * @since 9.0.0 */ - public function setObject($objectType, $objectId) { + public function setObject($objectType, $objectId): IComment { if ( !is_string($objectType) || !trim($objectType) || !is_string($objectId) || trim($objectId) === '' @@ -399,9 +374,7 @@ class Comment implements IComment { } /** - * returns the reference id of the comment - * - * @return string|null + * Returns the reference id of the comment * @since 19.0.0 */ public function getReferenceId(): ?string { @@ -409,10 +382,9 @@ class Comment implements IComment { } /** - * sets (overwrites) the reference id of the comment + * Sets (overwrites) the reference id of the comment * * @param string $referenceId e.g. sha256 hash sum - * @return IComment * @since 19.0.0 */ public function setReferenceId(?string $referenceId): IComment { @@ -463,9 +435,8 @@ class Comment implements IComment { * database. * * @param array $data - * @return IComment */ - protected function fromArray($data) { + protected function fromArray($data): IComment { foreach (array_keys($data) as $key) { // translate DB keys to internal setter names $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index 117318fb278..af4fda277d6 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -47,35 +47,23 @@ use OCP\Util; use Psr\Log\LoggerInterface; class Manager implements ICommentsManager { - /** @var IDBConnection */ - protected $dbConn; - - /** @var LoggerInterface */ - protected $logger; - - /** @var IConfig */ - protected $config; - - /** @var ITimeFactory */ - protected $timeFactory; - - /** @var IEmojiHelper */ - protected $emojiHelper; - - /** @var IInitialStateService */ - protected $initialStateService; - + protected IDBConnection $dbConn; + protected LoggerInterface $logger; + protected IConfig $config; + protected ITimeFactory $timeFactory; + protected IEmojiHelper $emojiHelper; + protected IInitialStateService $initialStateService; /** @var IComment[] */ - protected $commentsCache = []; + protected array $commentsCache = []; /** @var \Closure[] */ - protected $eventHandlerClosures = []; + protected array $eventHandlerClosures = []; /** @var ICommentsEventHandler[] */ - protected $eventHandlers = []; + protected array $eventHandlers = []; /** @var \Closure[] */ - protected $displayNameResolvers = []; + protected array $displayNameResolvers = []; public function __construct(IDBConnection $dbConn, LoggerInterface $logger, @@ -96,9 +84,8 @@ class Manager implements ICommentsManager { * IComment interface. * * @param array $data - * @return array */ - protected function normalizeDatabaseData(array $data) { + protected function normalizeDatabaseData(array $data): array { $data['id'] = (string)$data['id']; $data['parent_id'] = (string)$data['parent_id']; $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; @@ -134,11 +121,6 @@ class Manager implements ICommentsManager { return $data; } - - /** - * @param array $data - * @return IComment - */ public function getCommentFromData(array $data): IComment { return new Comment($this->normalizeDatabaseData($data)); } @@ -152,7 +134,7 @@ class Manager implements ICommentsManager { * by parameter for convenience * @throws \UnexpectedValueException */ - protected function prepareCommentForDatabaseWrite(IComment $comment) { + protected function prepareCommentForDatabaseWrite(IComment $comment): IComment { if (!$comment->getActorType() || $comment->getActorId() === '' || !$comment->getObjectType() @@ -172,7 +154,9 @@ class Manager implements ICommentsManager { $comment->setLatestChildDateTime(null); } - if (is_null($comment->getCreationDateTime())) { + try { + $comment->getCreationDateTime(); + } catch(\LogicException $e) { $comment->setCreationDateTime(new \DateTime()); } @@ -191,10 +175,9 @@ class Manager implements ICommentsManager { * returns the topmost parent id of a given comment identified by ID * * @param string $id - * @return string * @throws NotFoundException */ - protected function determineTopmostParentId($id) { + protected function determineTopmostParentId($id): string { $comment = $this->get($id); if ($comment->getParentId() === '0') { return $comment->getId(); @@ -210,7 +193,7 @@ class Manager implements ICommentsManager { * @param \DateTime $cDateTime the date time of the most recent child * @throws NotFoundException */ - protected function updateChildrenInformation($id, \DateTime $cDateTime) { + protected function updateChildrenInformation($id, \DateTime $cDateTime): void { $qb = $this->dbConn->getQueryBuilder(); $query = $qb->select($qb->func()->count('id')) ->from('comments') @@ -237,7 +220,7 @@ class Manager implements ICommentsManager { * @param string $id * @throws \InvalidArgumentException */ - protected function checkRoleParameters($role, $type, $id) { + protected function checkRoleParameters($role, $type, $id): void { if ( !is_string($type) || empty($type) || !is_string($id) || empty($id) @@ -248,10 +231,8 @@ class Manager implements ICommentsManager { /** * run-time caches a comment - * - * @param IComment $comment */ - protected function cache(IComment $comment) { + protected function cache(IComment $comment): void { $id = $comment->getId(); if (empty($id)) { return; @@ -264,7 +245,7 @@ class Manager implements ICommentsManager { * * @param mixed $id the comment's id */ - protected function uncache($id) { + protected function uncache($id): void { $id = (string)$id; if (isset($this->commentsCache[$id])) { unset($this->commentsCache[$id]); @@ -275,12 +256,11 @@ class Manager implements ICommentsManager { * returns a comment instance * * @param string $id the ID of the comment - * @return IComment * @throws NotFoundException * @throws \InvalidArgumentException * @since 9.0.0 */ - public function get($id) { + public function get($id): IComment { if ((int)$id === 0) { throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); } @@ -309,35 +289,9 @@ class Manager implements ICommentsManager { } /** - * returns the comment specified by the id and all it's child comments. - * At this point of time, we do only support one level depth. - * - * @param string $id - * @param int $limit max number of entries to return, 0 returns all - * @param int $offset the start entry - * @return array - * @since 9.0.0 - * - * The return array looks like this - * [ - * 'comment' => IComment, // root comment - * 'replies' => - * [ - * 0 => - * [ - * 'comment' => IComment, - * 'replies' => [] - * ] - * 1 => - * [ - * 'comment' => IComment, - * 'replies'=> [] - * ], - * … - * ] - * ] + * @inheritDoc */ - public function getTree($id, $limit = 0, $offset = 0) { + public function getTree($id, $limit = 0, $offset = 0): array { $tree = []; $tree['comment'] = $this->get($id); $tree['replies'] = []; @@ -382,7 +336,7 @@ class Manager implements ICommentsManager { * @param int $offset optional, starting point * @param \DateTime $notOlderThan optional, timestamp of the oldest comments * that may be returned - * @return IComment[] + * @return list<IComment> * @since 9.0.0 */ public function getForObject( @@ -434,8 +388,7 @@ class Manager implements ICommentsManager { * @param int $limit optional, number of maximum comments to be returned. if * set to 0, all comments are returned. * @param bool $includeLastKnown - * @return IComment[] - * @return array + * @return list<IComment> */ public function getForObjectSince( string $objectType, @@ -465,7 +418,7 @@ class Manager implements ICommentsManager { * @param int $limit optional, number of maximum comments to be returned. if * set to 0, all comments are returned. * @param bool $includeLastKnown - * @return IComment[] + * @return list<IComment> */ public function getCommentsWithVerbForObjectSinceComment( string $objectType, @@ -565,11 +518,10 @@ class Manager implements ICommentsManager { * @param string $objectType the object type, e.g. 'files' * @param string $objectId the id of the object * @param int $id the comment to look for - * @return Comment|null */ protected function getLastKnownComment(string $objectType, string $objectId, - int $id) { + int $id): ?IComment { $query = $this->dbConn->getQueryBuilder(); $query->select('*') ->from('comments') @@ -599,7 +551,7 @@ class Manager implements ICommentsManager { * @param string $verb Limit the verb of the comment * @param int $offset * @param int $limit - * @return IComment[] + * @return list<IComment> */ public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { $objectIds = []; @@ -618,7 +570,7 @@ class Manager implements ICommentsManager { * @param string $verb Limit the verb of the comment * @param int $offset * @param int $limit - * @return IComment[] + * @return list<IComment> */ public function searchForObjects(string $search, string $objectType, array $objectIds, string $verb, int $offset, int $limit = 50): array { $query = $this->dbConn->getQueryBuilder(); |