Completed
Push — v2 ( f8a429...e6c7b3 )
by Beñat
03:14
created

AnswerApi   C

Complexity

Total Complexity 66

Size/Duplication

Total Lines 493
Duplicated Lines 15.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 66
c 5
b 1
f 3
lcom 1
cbo 3
dl 77
loc 493
rs 5.7475

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 14 14 4
B getOfIds() 0 14 5
A accept() 0 11 3
A undoAccept() 0 11 3
A delete() 0 11 3
A downvote() 0 11 3
A undoDownvote() 0 11 3
A update() 11 11 3
A upvote() 0 11 3
A undoUpvote() 0 11 3
A addFlag() 0 11 3
B getOfQuestionIds() 0 14 5
A addOfQuestionId() 13 13 3
A render() 0 16 4
B getOfUserIds() 14 14 5
A myAnswers() 11 11 3
B getTopOfUserAndTags() 14 14 5
A myTopAnswersOfTags() 0 12 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AnswerApi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AnswerApi, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\StackExchangeApiClient\Api;
13
14
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
15
use BenatEspina\StackExchangeApiClient\Http\Http;
16
use BenatEspina\StackExchangeApiClient\Model\Answer;
17
use BenatEspina\StackExchangeApiClient\Serializer\AnswerSerializer;
18
19
/**
20
 * The answer api class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
final class AnswerApi
25
{
26
    const URL = 'answers/';
27
    const QUERY_PARAMS = [
28
        'order'  => 'desc',
29
        'sort'   => 'activity',
30
        'site'   => 'stackoverflow',
31
        'filter' => Http::FILTER_ALL,
32
    ];
33
34
    /**
35
     * The authentication.
36
     *
37
     * @var Authentication|null
38
     */
39
    private $authentication;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param Authentication|null $anAuthentication The authentication
45
     */
46
    public function __construct(Authentication $anAuthentication = null)
47
    {
48
        $this->authentication = $anAuthentication;
49
    }
50
51
    /**
52
     * Get all answers on the site.
53
     *
54
     * More info: http://api.stackexchange.com/docs/answers
55
     *
56
     * @param array $params    QueryString parameter(s)
57
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
58
     *
59
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
60
     */
61 View Code Duplication
    public function all(array $params = ['site' => 'stackoverflow'], $serialize = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        if ($this->authentication instanceof Authentication) {
64
            if (true === empty($params)) {
65
                $params = array_merge($params, self::QUERY_PARAMS);
66
            }
67
            $params = array_merge($params, $this->authentication->toArray());
68
        }
69
        $response = Http::instance()->get(
70
            self::URL, $params
71
        );
72
73
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
74
    }
75
76
    /**
77
     * Get answers identified by a set of ids.
78
     *
79
     * More info: http://api.stackexchange.com/docs/answers-by-ids
80
     *
81
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
82
     * @param array        $params    QueryString parameter(s)
83
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
84
     *
85
     * @return array|Answer
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
86
     */
87
    public function getOfIds($ids, array $params = ['site' => 'stackoverflow'], $serialize = true)
88
    {
89
        if ($this->authentication instanceof Authentication) {
90
            if (true === empty($params)) {
91
                $params = array_merge($params, self::QUERY_PARAMS);
92
            }
93
            $params = array_merge($params, $this->authentication->toArray());
94
        }
95
        $response = Http::instance()->get(
96
            self::URL . (is_array($ids) ? implode(';', $ids) : $ids), $params
97
        );
98
99
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
100
    }
101
102
    /**
103
     * Casts an accept vote on the given answer.
104
     *
105
     * More info: https://api.stackexchange.com/docs/accept-answer
106
     *
107
     * @param string $id        The id of question
108
     * @param array  $params    QueryString parameter(s)
109
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
110
     *
111
     * @throws \Exception when the auth is null
112
     *
113
     * @return Answer
114
     */
115
    public function accept($id, array $params = self::QUERY_PARAMS, $serialize = true)
116
    {
117
        if (!$this->authentication instanceof Authentication) {
118
            throw new \Exception('Authentication is required');
119
        }
120
        $response = Http::instance()->put(
121
            self::URL . $id . '/accept', array_merge($params, $this->authentication->toArray())
122
        );
123
124
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 124 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::accept of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
125
    }
126
127
    /**
128
     * Undoes an accept vote on the given answer.
129
     *
130
     * More info: https://api.stackexchange.com/docs/undo-accept-answer
131
     *
132
     * @param string $id        The id of question
133
     * @param array  $params    QueryString parameter(s)
134
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
135
     *
136
     * @throws \Exception when the auth is null
137
     *
138
     * @return Answer
139
     */
140
    public function undoAccept($id, array $params = self::QUERY_PARAMS, $serialize = true)
141
    {
142
        if (!$this->authentication instanceof Authentication) {
143
            throw new \Exception('Authentication is required');
144
        }
145
        $response = Http::instance()->put(
146
            self::URL . $id . '/accept/undo', array_merge($params, $this->authentication->toArray())
147
        );
148
149
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 149 which is incompatible with the return type documented by BenatEspina\StackExchang...i\AnswerApi::undoAccept of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
150
    }
151
152
    /**
153
     * Deletes an answer.
154
     *
155
     * More info: https://api.stackexchange.com/docs/delete-answer
156
     *
157
     * @param string $id        The id of question
158
     * @param array  $params    QueryString parameter(s)
159
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
160
     *
161
     * @throws \Exception when the auth is null
162
     *
163
     * @return Answer
164
     */
165
    public function delete($id, array $params = self::QUERY_PARAMS, $serialize = true)
166
    {
167
        if (!$this->authentication instanceof Authentication) {
168
            throw new \Exception('Authentication is required');
169
        }
170
        $response = Http::instance()->delete(
171
            self::URL . $id . '/delete', array_merge($params, $this->authentication->toArray())
172
        );
173
174
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 174 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::delete of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
175
    }
176
177
    /**
178
     * Downvotes an answer.
179
     *
180
     * More info: https://api.stackexchange.com/docs/downvote-answer
181
     *
182
     * @param string $id        The id of question
183
     * @param array  $params    QueryString parameter(s)
184
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
185
     *
186
     * @throws \Exception when the auth is null
187
     *
188
     * @return Answer
189
     */
190
    public function downvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
191
    {
192
        if (!$this->authentication instanceof Authentication) {
193
            throw new \Exception('Authentication is required');
194
        }
195
        $response = Http::instance()->put(
196
            self::URL . $id . '/downvote', array_merge($params, $this->authentication->toArray())
197
        );
198
199
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 199 which is incompatible with the return type documented by BenatEspina\StackExchang...Api\AnswerApi::downvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
200
    }
201
202
    /**
203
     * Undoes an downvote on an answer.
204
     *
205
     * More info: https://api.stackexchange.com/docs/undo-downvote-answer
206
     *
207
     * @param string $id        The id of question
208
     * @param array  $params    QueryString parameter(s)
209
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
210
     *
211
     * @throws \Exception when the auth is null
212
     *
213
     * @return Answer
214
     */
215
    public function undoDownvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
216
    {
217
        if (!$this->authentication instanceof Authentication) {
218
            throw new \Exception('Authentication is required');
219
        }
220
        $response = Http::instance()->put(
221
            self::URL . $id . '/downvote/undo', array_merge($params, $this->authentication->toArray())
222
        );
223
224
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 224 which is incompatible with the return type documented by BenatEspina\StackExchang...AnswerApi::undoDownvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
225
    }
226
227
    /**
228
     * Edit an existing answer.
229
     *
230
     * More info: https://api.stackexchange.com/docs/edit-answer
231
     *
232
     * @param string $id        The id of question
233
     * @param string $body      The body of the answer
234
     * @param array  $params    QueryString parameter(s)
235
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
236
     *
237
     * @throws \Exception when the auth is null
238
     *
239
     * @return Answer
240
     */
241 View Code Duplication
    public function update($id, $body, array $params = self::QUERY_PARAMS, $serialize = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        if (!$this->authentication instanceof Authentication) {
244
            throw new \Exception('Authentication is required');
245
        }
246
        $response = Http::instance()->put(
247
            self::URL . $id . '/edit', array_merge(['body' => $body], $params, $this->authentication->toArray())
248
        );
249
250
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 250 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::update of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
251
    }
252
253
    /**
254
     * Upvotes an answer.
255
     *
256
     * More info: https://api.stackexchange.com/docs/upvote-answer
257
     *
258
     * @param string $id        The id of question
259
     * @param array  $params    QueryString parameter(s)
260
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
261
     *
262
     * @throws \Exception when the auth is null
263
     *
264
     * @return Answer
265
     */
266
    public function upvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
267
    {
268
        if (!$this->authentication instanceof Authentication) {
269
            throw new \Exception('Authentication is required');
270
        }
271
        $response = Http::instance()->put(
272
            self::URL . $id . '/upvote', array_merge($params, $this->authentication->toArray())
273
        );
274
275
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 275 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::upvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
276
    }
277
278
    /**
279
     * Undoes an upvote on an answer.
280
     *
281
     * More info: https://api.stackexchange.com/docs/undo-upvote-answer
282
     *
283
     * @param string $id        The id of question
284
     * @param array  $params    QueryString parameter(s)
285
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
286
     *
287
     * @throws \Exception when the auth is null
288
     *
289
     * @return Answer
290
     */
291
    public function undoUpvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
292
    {
293
        if (!$this->authentication instanceof Authentication) {
294
            throw new \Exception('Authentication is required');
295
        }
296
        $response = Http::instance()->put(
297
            self::URL . $id . '/upvote/undo', array_merge($params, $this->authentication->toArray())
298
        );
299
300
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 300 which is incompatible with the return type documented by BenatEspina\StackExchang...i\AnswerApi::undoUpvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
301
    }
302
303
    /**
304
     * Casts a flag against the answer identified by id.
305
     *
306
     * More info: https://api.stackexchange.com/docs/create-answer-flag
307
     *
308
     * @param string $id        The id of question
309
     * @param array  $params    QueryString parameter(s)
310
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
311
     *
312
     * @throws \Exception when the auth is null
313
     *
314
     * @return Answer
315
     */
316
    public function addFlag($id, array $params = self::QUERY_PARAMS, $serialize = true)
317
    {
318
        if (!$this->authentication instanceof Authentication) {
319
            throw new \Exception('Authentication is required');
320
        }
321
        $response = Http::instance()->put(
322
            self::URL . $id . '/flags/add', array_merge($params, $this->authentication->toArray())
323
        );
324
325
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 325 which is incompatible with the return type documented by BenatEspina\StackExchang...\Api\AnswerApi::addFlag of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
326
    }
327
328
    /**
329
     * Gets the answers to a set of questions identified in id.
330
     *
331
     * More info: https://api.stackexchange.com/docs/answers-on-questions
332
     *
333
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
334
     * @param array        $params    QueryString parameter(s)
335
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
336
     *
337
     * @return array|Answer
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
338
     */
339
    public function getOfQuestionIds($ids, array $params = ['site' => 'stackoverflow'], $serialize = true)
340
    {
341
        if ($this->authentication instanceof Authentication) {
342
            if (true === empty($params)) {
343
                $params = array_merge($params, self::QUERY_PARAMS);
344
            }
345
            $params = array_merge($params, $this->authentication->toArray());
346
        }
347
        $response = Http::instance()->get(
348
            'questions/' . (is_array($ids) ? implode(';', $ids) : $ids) . self::URL, $params
349
        );
350
351
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
352
    }
353
354
    /**
355
     * Create a new answer on the given question.
356
     *
357
     * More info: https://api.stackexchange.com/docs/create-answer
358
     *
359
     * @param string $id        The id of question
360
     * @param string $body      The body of the answer
361
     * @param array  $params    QueryString parameter(s)
362
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
363
     *
364
     * @throws \Exception when the auth is null
365
     *
366
     * @return Answer
367
     */
368 View Code Duplication
    public function addOfQuestionId($id, $body, array $params = self::QUERY_PARAMS, $serialize = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
    {
370
        if (!$this->authentication instanceof Authentication) {
371
            throw new \Exception('Authentication is required');
372
        }
373
        $response = Http::instance()->post(
374
            'questions/' . $id . '/' . self::URL . 'add', array_merge(
375
                ['body' => $body], $params, $this->authentication->toArray()
376
            )
377
        );
378
379
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 379 which is incompatible with the return type documented by BenatEspina\StackExchang...werApi::addOfQuestionId of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
380
    }
381
382
    /**
383
     * Render an answer given it's body and the question it's on.
384
     *
385
     * More info: https://api.stackexchange.com/docs/render-answer
386
     *
387
     * @param string $id        The id of question
388
     * @param string $body      The body of the answer
389
     * @param array  $params    QueryString parameter(s)
390
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
391
     *
392
     * @throws \Exception when the auth is null
393
     *
394
     * @return Answer
395
     */
396
    public function render($id, $body, array $params = ['site' => 'stackoverflow'], $serialize = true)
397
    {
398
        if ($this->authentication instanceof Authentication) {
399
            if (true === empty($params)) {
400
                $params = array_merge($params, self::QUERY_PARAMS);
401
            }
402
            $params = array_merge($params, $this->authentication->toArray());
403
        }
404
        $response = Http::instance()->post(
405
            'questions/' . $id . '/' . self::URL . 'render', array_merge(
406
                ['body' => $body], $params, $this->authentication->toArray()
407
            )
408
        );
409
410
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 410 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::render of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
411
    }
412
413
    /**
414
     * Returns the answers the users in {ids} have posted.
415
     *
416
     * More info: https://api.stackexchange.com/docs/answers-on-users
417
     *
418
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
419
     * @param array        $params    QueryString parameter(s)
420
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
421
     *
422
     * @return array|Answer
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
423
     */
424 View Code Duplication
    public function getOfUserIds($ids, array $params = ['site' => 'stackoverflow'], $serialize = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
425
    {
426
        if ($this->authentication instanceof Authentication) {
427
            if (true === empty($params)) {
428
                $params = array_merge($params, self::QUERY_PARAMS);
429
            }
430
            $params = array_merge($params, $this->authentication->toArray());
431
        }
432
        $response = Http::instance()->get(
433
            'users/' . (is_array($ids) ? implode(';', $ids) : $ids) . '/' . self::URL, $params
434
        );
435
436
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
437
    }
438
439
    /**
440
     * Returns the answers owned by the user associated with the given access_token.
441
     *
442
     * More info: https://api.stackexchange.com/docs/me-answers
443
     *
444
     * @param array $params    QueryString parameter(s)
445
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
446
     *
447
     * @throws \Exception when the auth is null
448
     *
449
     * @return array|Answer
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
450
     */
451 View Code Duplication
    public function myAnswers(array $params = self::QUERY_PARAMS, $serialize = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
452
    {
453
        if (!$this->authentication instanceof Authentication) {
454
            throw new \Exception('Authentication is required');
455
        }
456
        $response = Http::instance()->get(
457
            'me/' . self::URL, array_merge($params, $this->authentication->toArray())
458
        );
459
460
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
461
    }
462
463
    /**
464
     * Returns the top 30 answers a user has posted in response to questions with the given tags.
465
     *
466
     * More info: https://api.stackexchange.com/docs/top-user-answers-in-tags
467
     *
468
     * @param string       $userId    The user id
469
     * @param string|array $tags      Array which contains the $tags delimited by semicolon, or a simple tag
470
     * @param array        $params    QueryString parameter(s)
471
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
472
     *
473
     * @return array|Answer
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
474
     */
475 View Code Duplication
    public function getTopOfUserAndTags($userId, $tags, array $params = ['site' => 'stackoverflow'], $serialize = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
476
    {
477
        if ($this->authentication instanceof Authentication) {
478
            if (true === empty($params)) {
479
                $params = array_merge($params, self::QUERY_PARAMS);
480
            }
481
            $params = array_merge($params, $this->authentication->toArray());
482
        }
483
        $response = Http::instance()->get(
484
            'users/' . $userId . '/tags/' . (is_array($tags) ? implode(';', $tags) : $tags) . '/top-' . self::URL, $params
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
485
        );
486
487
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
488
    }
489
490
    /**
491
     * Returns the top 30 answers the user associated with the given
492
     * access_token has posted in response to questions with the given tags.
493
     *
494
     * More info: https://api.stackexchange.com/docs/me-tags-top-answers
495
     *
496
     * @param string|array $tags      Array which contains the tags delimited by semicolon, or a simple tag
497
     * @param array        $params    QueryString parameter(s)
498
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
499
     *
500
     * @throws \Exception when the auth is null
501
     *
502
     * @return array|Answer
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
503
     */
504
    public function myTopAnswersOfTags($tags, array $params = self::QUERY_PARAMS, $serialize = true)
505
    {
506
        if (!$this->authentication instanceof Authentication) {
507
            throw new \Exception('Authentication is required');
508
        }
509
        $response = Http::instance()->get(
510
            'me/tags/' . (is_array($tags) ? implode(';', $tags) : $tags) . '/top-' . self::URL,
511
            array_merge($params, $this->authentication->toArray())
512
        );
513
514
        return $serialize === true ? AnswerSerializer::serialize($response) : $response;
515
    }
516
}
517