Skip to content

Commit 3608f49

Browse files
committed
Improved Comments Trait.
1 parent 3829651 commit 3608f49

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/FabianBeiner/Todoist/TodoistCommentsTrait.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* An unofficial PHP client library for accessing the official Todoist REST API.
55
*
66
* @author Fabian Beiner <fb@fabianbeiner.de>
7-
* @license MIT
7+
* @license https://opensource.org/licenses/MIT MIT
88
* @link https://github.com/FabianBeiner/Todoist-PHP-API-Library
99
*/
1010

@@ -39,19 +39,17 @@ public function getAllCommentsByTask($taskId)
3939
*
4040
* @return array|bool Array with all comments (can be empty), or false on failure.
4141
*/
42-
public function getAllComments($type, $typeId)
42+
public function getAllComments(string $type, int $typeId)
4343
{
4444
$type = mb_strtolower($type, 'UTF-8');
45-
if (($type !== 'project' && $type !== 'task') || ( ! filter_var($typeId,
46-
FILTER_VALIDATE_INT) || $typeId <= 0)) {
45+
if (($type !== 'project' && $type !== 'task') || ( ! $typeId || ! filter_var($typeId,
46+
FILTER_VALIDATE_INT))) {
4747
return false;
4848
}
4949

50-
parse_str($this->tokenQuery, $query);
51-
$query[$type . '_id'] = $typeId;
52-
$localQuery = http_build_query($query, null, '&', PHP_QUERY_RFC3986);
50+
$query = http_build_query([$type . '_id' => $typeId], null, '&', PHP_QUERY_RFC3986);
5351

54-
$result = $this->client->get('comments?' . $localQuery);
52+
$result = $this->client->get('comments?' . $query);
5553

5654
$status = $result->getStatusCode();
5755
if ($status === 204) {
@@ -71,7 +69,7 @@ public function getAllComments($type, $typeId)
7169
*
7270
* @return array|bool Array with all comments (can be empty), or false on failure.
7371
*/
74-
public function getAllCommentsByProject($projectId)
72+
public function getAllCommentsByProject(int $projectId)
7573
{
7674
return $this->getAllComments('project', $projectId);
7775
}
@@ -82,9 +80,10 @@ public function getAllCommentsByProject($projectId)
8280
* @param int $taskId ID of the task.
8381
* @param string $comment Comment to be added.
8482
*
85-
* @return array|bool Array with values of the new comment, or false on failure.
83+
* @return array|bool Array with values of the new comment, or false on
84+
* failure.
8685
*/
87-
public function createCommentForTask($taskId, $comment)
86+
public function createCommentForTask(int $taskId, string $comment)
8887
{
8988
return $this->createComment('task', $taskId, $comment);
9089
}
@@ -98,18 +97,18 @@ public function createCommentForTask($taskId, $comment)
9897
*
9998
* @return array|bool Array with values of the new comment, or false on failure.
10099
*/
101-
public function createComment($type, $typeId, $comment)
100+
public function createComment(string $type, int $typeId, string $comment)
102101
{
103102
$type = mb_strtolower($type, 'UTF-8');
104103
if (($type !== 'project' && $type !== 'task') ||
105-
($typeId <= 0 || ! filter_var($typeId,
106-
FILTER_VALIDATE_INT)) ||
104+
( ! $typeId || ! filter_var($typeId,
105+
FILTER_VALIDATE_INT)) ||
107106
! mb_strlen($comment,
108107
'utf8')) {
109108
return false;
110109
}
111110

112-
$result = $this->client->post('comments?' . $this->tokenQuery,
111+
$result = $this->client->post('comments',
113112
[
114113
RequestOptions::JSON => [
115114
$type . '_id' => (int)$typeId,
@@ -133,7 +132,7 @@ public function createComment($type, $typeId, $comment)
133132
*
134133
* @return array|bool Array with values of the new comment, or false on failure.
135134
*/
136-
public function createCommentForProject($projectId, $comment)
135+
public function createCommentForProject(int $projectId, string $comment)
137136
{
138137
return $this->createComment('project', $projectId, $comment);
139138
}
@@ -145,13 +144,13 @@ public function createCommentForProject($projectId, $comment)
145144
*
146145
* @return array|bool Array with values of the comment, or false on failure.
147146
*/
148-
public function getComment($commentId)
147+
public function getComment(int $commentId)
149148
{
150-
if ($commentId <= 0 || ! filter_var($commentId, FILTER_VALIDATE_INT)) {
149+
if ( ! $commentId || ! filter_var($commentId, FILTER_VALIDATE_INT)) {
151150
return false;
152151
}
153152

154-
$result = $this->client->get('comments/' . $commentId . '?' . $this->tokenQuery);
153+
$result = $this->client->get('comments/' . $commentId);
155154

156155
$status = $result->getStatusCode();
157156
if ($status === 200) {
@@ -169,13 +168,13 @@ public function getComment($commentId)
169168
*
170169
* @return bool True on success, false on failure.
171170
*/
172-
public function updateComment($commentId, $content)
171+
public function updateComment(int $commentId, string $content)
173172
{
174-
if ($commentId <= 0 || ! mb_strlen($content, 'utf8') || ! filter_var($commentId, FILTER_VALIDATE_INT)) {
173+
if ( ! $commentId || ! mb_strlen($content, 'utf8') || ! filter_var($commentId, FILTER_VALIDATE_INT)) {
175174
return false;
176175
}
177176

178-
$result = $this->client->post('comments/' . $commentId . '?' . $this->tokenQuery,
177+
$result = $this->client->post('comments/' . $commentId,
179178
[
180179
RequestOptions::JSON => ['content' => trim($content)]
181180
]);
@@ -192,13 +191,13 @@ public function updateComment($commentId, $content)
192191
*
193192
* @return bool True on success, false on failure.
194193
*/
195-
public function deleteComment($commentId)
194+
public function deleteComment(int $commentId)
196195
{
197-
if ($commentId <= 0 || ! filter_var($commentId, FILTER_VALIDATE_INT)) {
196+
if ( ! $commentId || ! filter_var($commentId, FILTER_VALIDATE_INT)) {
198197
return false;
199198
}
200199

201-
$result = $this->client->delete('comments/' . $commentId . '?' . $this->tokenQuery);
200+
$result = $this->client->delete('comments/' . $commentId);
202201

203202
$status = $result->getStatusCode();
204203

0 commit comments

Comments
 (0)