Api   F
last analyzed

Complexity

Total Complexity 73

Size/Duplication

Total Lines 722
Duplicated Lines 25.21 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 68.99%

Importance

Changes 0
Metric Value
wmc 73
lcom 1
cbo 10
dl 182
loc 722
ccs 158
cts 229
cp 0.6899
rs 2.438
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A processResponse() 0 9 2
A fetchUser() 10 10 1
A fetchBoard() 10 10 1
A fetchMultipleBoards() 11 11 2
A fetchMultipleUsers() 10 10 1
A getUser() 0 10 2
A getBoard() 0 10 2
A getUserBoards() 0 6 1
A getUserPins() 0 6 1
A getCurrentUser() 0 6 1
A getUserFollowers() 0 6 1
A getUserFollowingBoards() 0 6 1
A getUserFollowing() 0 6 1
A getUserInterests() 0 6 1
A followUser() 16 16 2
A unfollowUser() 13 13 2
A followBoard() 20 20 3
A unfollowBoard() 17 17 3
A createBoard() 0 18 3
B createPin() 0 31 7
A getNextItems() 0 26 3
A buildRequestForPagedList() 0 15 1
A getBoardPins() 11 11 2
A execute() 0 14 3
A deleteBoard() 10 10 2
B updateBoard() 6 20 6
A deletePin() 10 10 2
A getPin() 11 11 2
B updatePin() 6 34 10
A fetchMultiplePins() 11 11 2
A fetchPin() 10 10 1

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 Api 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 Api, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Pinterest PHP library.
5
 *
6
 * (c) Hans Ott <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 *
11
 * Source: https://github.com/hansott/pinterest-php
12
 */
13
14
namespace Pinterest;
15
16
use Pinterest\Objects\Pin;
17
use Pinterest\Objects\User;
18
use Pinterest\Http\Request;
19
use Pinterest\Http\Response;
20
use Pinterest\Objects\Board;
21
use InvalidArgumentException;
22
use Pinterest\Objects\PagedList;
23
use Pinterest\Http\Exceptions\RateLimitedReached;
24
25
/**
26
 * The api client.
27
 *
28
 * @author Hans Ott <[email protected]>
29
 * @author Toon Daelman <[email protected]>
30
 */
31
class Api
32
{
33
    /**
34
     * The authentication client.
35
     *
36
     * @var Authentication
37
     */
38
    private $auth;
39
40
    /**
41
     * The constructor.
42
     *
43
     * @param Authentication $auth The authentication client.
44
     */
45 54
    public function __construct(Authentication $auth)
46
    {
47 54
        $this->auth = $auth;
48 54
    }
49
50
    /**
51
     * Processes a response.
52
     *
53
     * @param Response $response  The response object.
54
     * @param callable $processor The response processor.
55
     *
56
     * @return Response The response
57
     */
58 11
    private function processResponse(Response $response, $processor)
59
    {
60 11
        if ($response->ok()) {
61 11
            $result = $processor($response);
62 11
            $response->setResult($result);
63 6
        }
64
65 11
        return $response;
66
    }
67
68
    /**
69
     * Execute the given http request.
70
     *
71
     * @param Request       $request
72
     * @param callable|null $processor
73
     *
74
     * @throws RateLimitedReached
75
     *
76
     * @return Response The response
77
     */
78 34
    public function execute(Request $request, $processor = null)
79 1
    {
80 34
        $response = $this->auth->execute($request);
81
82 34
        if ($response->rateLimited()) {
83 19
            throw new RateLimitedReached($response);
84
        }
85
86 15
        if (is_callable($processor)) {
87 11
            $response = $this->processResponse($response, $processor);
88 6
        }
89
90 15
        return $response;
91
    }
92
93
    /**
94
     * Fetch a single user and processes the response.
95
     *
96
     * @param Request $request
97
     *
98
     * @throws RateLimitedReached
99
     *
100
     * @return Response The response
101
     */
102 4 View Code Duplication
    private function fetchUser(Request $request)
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...
103
    {
104 4
        $request->setFields(User::fields());
105
106
        return $this->execute($request, function (Response $response) {
107 4
            $mapper = new Mapper(new User());
108
109 4
            return $mapper->toSingle($response);
110 4
        });
111
    }
112
113
    /**
114
     * Fetch a single board and processes the response.
115
     *
116
     * @param Request $request
117
     *
118
     * @throws RateLimitedReached
119
     *
120
     * @return Response The response
121
     */
122 4 View Code Duplication
    private function fetchBoard(Request $request)
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...
123
    {
124 4
        $request->setFields(Board::fields());
125
126
        return $this->execute($request, function (Response $response) {
127 1
            $mapper = new Mapper(new Board());
128
129 1
            return $mapper->toSingle($response);
130 4
        });
131
    }
132
133
    /**
134
     * Fetch a single pin and processes the response.
135
     *
136
     * @param Request $request
137
     *
138
     * @throws RateLimitedReached
139
     *
140
     * @return Response The response
141
     */
142 8 View Code Duplication
    private function fetchPin(Request $request)
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...
143
    {
144 8
        $request->setFields(Pin::fields());
145
146
        return $this->execute($request, function (Response $response) {
147
            $mapper = new Mapper(new Pin());
148
149
            return $mapper->toSingle($response);
150 8
        });
151
    }
152
153
    /**
154
     * Fetch multiple boards and processes the response.
155
     *
156
     * @param Request $request
157
     * @param string[] $fields
158
     *
159
     * @throws RateLimitedReached
160
     *
161
     * @return Response The response
162
     */
163 6 View Code Duplication
    private function fetchMultipleBoards(Request $request, array $fields = null)
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...
164
    {
165 6
        $fields = $fields ? $fields : Board::fields();
166 6
        $request->setFields($fields);
167
168
        return $this->execute($request, function (Response $response) {
169 2
            $mapper = new Mapper(new Board());
170
171 2
            return $mapper->toList($response);
172 6
        });
173
    }
174
175
    /**
176
     * Fetch multiple users and processes the response.
177
     *
178
     * @param Request $request
179
     *
180
     * @throws RateLimitedReached
181
     *
182
     * @return Response The response
183
     */
184 4 View Code Duplication
    private function fetchMultipleUsers(Request $request)
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...
185
    {
186 4
        $request->setFields(User::fields());
187
188
        return $this->execute($request, function (Response $response) {
189 4
            $mapper = new Mapper(new User());
190
191 4
            return $mapper->toList($response);
192 4
        });
193
    }
194
195
    /**
196
     * Fetches multiple pins and processes the response.
197
     *
198
     * @param Request $request
199
     * @param $fields array The fields to require.
200
     *
201
     * @throws RateLimitedReached
202
     *
203
     * @return Response The response
204
     */
205 4 View Code Duplication
    private function fetchMultiplePins(Request $request, array $fields = null)
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...
206
    {
207 4
        $fields = $fields ? $fields : Pin::fields();
208 4
        $request->setFields($fields);
209
210
        return $this->execute($request, function (Response $response) {
211
            $mapper = new Mapper(new Pin());
212
213
            return $mapper->toList($response);
214 4
        });
215
    }
216
217
    /**
218
     * Get a user.
219
     *
220
     * @param string $usernameOrId The username or identifier of the user.
221
     *
222
     * @throws RateLimitedReached
223
     *
224
     * @return Response The response
225
     */
226 4
    public function getUser($usernameOrId)
227
    {
228 4
        if (empty($usernameOrId)) {
229 2
            throw new InvalidArgumentException('The username or id should not be empty.');
230
        }
231
232 2
        $request = new Request('GET', sprintf('users/%s/', $usernameOrId));
233
234 2
        return $this->fetchUser($request);
235
    }
236
237
    /**
238
     * Get a board.
239
     *
240
     * @param string $boardId The board id.
241
     *
242
     * @throws RateLimitedReached
243
     *
244
     * @return Response The response
245
     */
246 2
    public function getBoard($boardId)
247
    {
248 2
        if (empty($boardId)) {
249
            throw new InvalidArgumentException('The board id should not be empty.');
250
        }
251
252 2
        $request = new Request('GET', sprintf('boards/%s/', $boardId));
253
254 2
        return $this->fetchBoard($request);
255
    }
256
257
    /**
258
     * Update a board.
259
     *
260
     * @param Board $board The updated board.
261
     *
262
     * @throws RateLimitedReached
263
     *
264
     * @return Response The response
265
     */
266
    public function updateBoard(Board $board)
267
    {
268
        $params = array();
269
270
        if (empty($board->id)) {
271
            throw new InvalidArgumentException('The board id is required.');
272
        }
273
274 View Code Duplication
        if (isset($board->name) && empty($board->name) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
275
            $params['name'] = (string) $board->name;
276
        }
277
278 View Code Duplication
        if (isset($board->description) && empty($board->description) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
279
            $params['description'] = (string) $board->description;
280
        }
281
282
        $request = new Request('PATCH', sprintf('boards/%s/', $board->id), $params);
283
284
        return $this->fetchBoard($request);
285
    }
286
287
    /**
288
     * Get the boards of the authenticated user.
289
     *
290
     * @throws RateLimitedReached
291
     *
292
     * @return Response The response
293
     */
294 2
    public function getUserBoards()
295
    {
296 2
        $request = new Request('GET', 'me/boards/');
297
298 2
        return $this->fetchMultipleBoards($request);
299
    }
300
301
    /**
302
     * Get the pins of the authenticated user.
303
     *
304
     * @throws RateLimitedReached
305
     *
306
     * @return Response The response
307
     */
308 2
    public function getUserPins()
309
    {
310 2
        $request = new Request('GET', 'me/pins/');
311
312 2
        return $this->fetchMultiplePins($request);
313
    }
314
315
    /**
316
     * Get the authenticated user.
317
     *
318
     * @throws RateLimitedReached
319
     *
320
     * @return Response The response
321
     */
322 2
    public function getCurrentUser()
323
    {
324 2
        $request = new Request('GET', 'me/');
325
326 2
        return $this->fetchUser($request);
327
    }
328
329
    /**
330
     * Get the followers of the authenticated user.
331
     *
332
     * @throws RateLimitedReached
333
     *
334
     * @return Response The response
335
     */
336 2
    public function getUserFollowers()
337
    {
338 2
        $request = new Request('GET', 'me/followers/');
339
340 2
        return $this->fetchMultipleUsers($request);
341
    }
342
343
    /**
344
     * Get the boards that the authenticated user follows.
345
     *
346
     * @throws RateLimitedReached
347
     *
348
     * @return Response The response
349
     */
350 2
    public function getUserFollowingBoards()
351
    {
352 2
        $request = new Request('GET', 'me/following/boards/');
353
354 2
        return $this->fetchMultipleBoards($request);
355
    }
356
357
    /**
358
     * Get the users that the authenticated user follows.
359
     *
360
     * @throws RateLimitedReached
361
     *
362
     * @return Response The response
363
     */
364 2
    public function getUserFollowing()
365
    {
366 2
        $request = new Request('GET', 'me/following/users/');
367
368 2
        return $this->fetchMultipleUsers($request);
369
    }
370
371
    /**
372
     * Get the interests that the authenticated user follows.
373
     *
374
     * @link https://www.pinterest.com/explore/901179409185
375
     *
376
     * @throws RateLimitedReached
377
     *
378
     * @return Response The response
379
     */
380 2
    public function getUserInterests()
381
    {
382 2
        $request = new Request('GET', 'me/following/interests/');
383
384 2
        return $this->fetchMultipleBoards($request, array('id', 'name'));
385
    }
386
387
    /**
388
     * Follow a user.
389
     *
390
     * @param string $username The username of the user to follow.
391
     *
392
     * @throws RateLimitedReached
393
     *
394
     * @return Response The response
395
     */
396 4 View Code Duplication
    public function followUser($username)
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...
397
    {
398 4
        if (empty($username)) {
399 2
            throw new InvalidArgumentException('Username is required.');
400
        }
401
402 2
        $request = new Request(
403 2
            'POST',
404 2
            'me/following/users/',
405
            array(
406 2
                'user' => (string) $username,
407
            )
408 1
        );
409
410 2
        return $this->execute($request);
411
    }
412
413
    /**
414
     * Unfollow a user.
415
     *
416
     * @param string $usernameOrUserId The username or ID of the user to unfollow.
417
     *
418
     * @throws RateLimitedReached
419
     *
420
     * @return Response The response
421
     */
422 4 View Code Duplication
    public function unfollowUser($usernameOrUserId)
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...
423
    {
424 4
        if (empty($usernameOrUserId)) {
425 2
            throw new InvalidArgumentException('Username or user ID is required.');
426
        }
427
428 2
        $request = new Request(
429 2
            'DELETE',
430 2
            "me/following/users/{$usernameOrUserId}"
431 1
        );
432
433 2
        return $this->execute($request);
434
    }
435
436
    /**
437
     * Follow a board.
438
     *
439
     * @param string $username The username of the user that owns the board
440
     * @param string $boardName The name of the board
441
     *
442
     * @return Response The response
443
     *
444
     * @throws RateLimitedReached
445
     */
446 6 View Code Duplication
    public function followBoard($username, $boardName)
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...
447
    {
448 6
        if (empty($username)) {
449 2
            throw new InvalidArgumentException('Username is required.');
450
        }
451
452 4
        if (empty($boardName)) {
453 2
            throw new InvalidArgumentException('The board name is required.');
454
        }
455
456 2
        $request = new Request(
457 2
            'POST',
458 2
            'me/following/boards/',
459
            array(
460 2
                'board' => "{$username}/{$boardName}",
461
            )
462 1
        );
463
464 2
        return $this->execute($request);
465
    }
466
467
    /**
468
     * Unfollow a board.
469
     *
470
     * @param string $username The username of the user that owns the board
471
     * @param string $boardName The name of the board
472
     *
473
     * @return Response The response
474
     *
475
     * @throws RateLimitedReached
476
     */
477 6 View Code Duplication
    public function unfollowBoard($username, $boardName)
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...
478
    {
479 6
        if (empty($username)) {
480 2
            throw new InvalidArgumentException('Username is required.');
481
        }
482
483 4
        if (empty($boardName)) {
484 2
            throw new InvalidArgumentException('The board name is required.');
485
        }
486
487 2
        $request = new Request(
488 2
            'DELETE',
489 2
            "me/following/boards/{$username}/{$boardName}"
490 1
        );
491
492 2
        return $this->execute($request);
493
    }
494
495
    /**
496
     * Create a board.
497
     *
498
     * @param string $name The board name.
499
     * @param string $description The board description.
500
     *
501
     * @throws RateLimitedReached
502
     *
503
     * @return Response The response
504
     */
505 2
    public function createBoard($name, $description = null)
506
    {
507 2
        if (empty($name)) {
508
            throw new InvalidArgumentException('The name should not be empty.');
509
        }
510
511
        $params = array(
512 2
            'name' => (string) $name,
513 1
        );
514
515 2
        if (empty($description) === false) {
516 2
            $params['description'] = (string) $description;
517 1
        }
518
519 2
        $request = new Request('POST', 'boards/', $params);
520
521 2
        return $this->fetchBoard($request);
522
    }
523
524
    /**
525
     * Delete a board.
526
     *
527
     * @param int $boardId The board id.
528
     *
529
     * @throws RateLimitedReached
530
     *
531
     * @return Response The response
532
     */
533 View Code Duplication
    public function deleteBoard($boardId)
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...
534
    {
535
        if (empty($boardId)) {
536
            throw new InvalidArgumentException('The board id should not be empty.');
537
        }
538
539
        $request = new Request('DELETE', "boards/{$boardId}/");
540
541
        return $this->execute($request);
542
    }
543
544
    /**
545
     * Create a pin on a board.
546
     *
547
     * @param string $boardId The board id.
548
     * @param string $note The note.
549
     * @param Image $image The image.
550
     * @param string|null $link The link (Optional).
551
     *
552
     * @throws RateLimitedReached
553
     *
554
     * @return Response The response
555
     */
556 8
    public function createPin($boardId, $note, Image $image, $link = null)
557
    {
558 8
        if (empty($boardId)) {
559
            throw new InvalidArgumentException('The board id should not be empty.');
560
        }
561
562 8
        if (empty($note)) {
563
            throw new InvalidArgumentException('The note should not be empty.');
564
        }
565
566
        $params = array(
567 8
            'board' => $boardId,
568 8
            'note' => (string) $note,
569 4
        );
570
571 8
        if (empty($link) === false) {
572
            $params['link'] = (string) $link;
573
        }
574
575 8
        $imageKey = $image->isUrl() ? 'image_url' : ($image->isBase64() ? 'image_base64' : 'image');
576
577 8
        if ($image->isFile()) {
578 2
            $params[$imageKey] = $image;
579 1
        } else {
580 6
            $params[$imageKey] = $image->getData();
581
        }
582
583 8
        $request = new Request('POST', 'pins/', $params);
584
585 8
        return $this->fetchPin($request);
586
    }
587
588
    /**
589
     * Delete a Pin.
590
     *
591
     * @param string $pinId The id of the pin to delete.
592
     *
593
     * @throws RateLimitedReached
594
     *
595
     * @return Response The response
596
     */
597 View Code Duplication
    public function deletePin($pinId)
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...
598
    {
599
        if (empty($pinId)) {
600
            throw new InvalidArgumentException('The pin id should not be empty.');
601
        }
602
603
        $request = new Request('DELETE', "pins/{$pinId}/");
604
605
        return $this->execute($request);
606
    }
607
608
    /**
609
     * Get the next items for a paged list.
610
     *
611
     * @param PagedList $pagedList
612
     *
613
     * @throws RateLimitedReached
614
     *
615
     * @return Response The response
616
     */
617 2
    public function getNextItems(PagedList $pagedList)
618
    {
619 2
        if (!$pagedList->hasNext()) {
620 2
            throw new InvalidArgumentException('The list has no more items');
621
        }
622
623
        $items = $pagedList->items();
624
625
        if (empty($items)) {
626
            throw new InvalidArgumentException(
627
                'Unable to detect object type because the list contains no items'
628
            );
629
        }
630
631
        $item = reset($items);
632
        $objectClassName = get_class($item);
633
        $objectInstance = new $objectClassName();
634
635
        $request = $this->buildRequestForPagedList($pagedList);
636
637
        return $this->execute($request, function (Response $response) use ($objectInstance) {
638
            $mapper = new Mapper($objectInstance);
639
640
            return $mapper->toList($response);
641
        });
642
    }
643
644
    /**
645
     * Build a request to get the next items of a paged list.
646
     *
647
     * @param PagedList $pagedList
648
     *
649
     * @return Request
650
     */
651
    private function buildRequestForPagedList(PagedList $pagedList)
652
    {
653
        $nextItemsUri = $pagedList->getNextUrl();
654
655
        $params = array();
656
        $components = parse_url($nextItemsUri);
657
        parse_str($components['query'], $params);
658
659
        $path = $components['path'];
660
        $versionPath = sprintf('/%s/', Authentication::API_VERSION);
661
        $versionPathLength = strlen($versionPath);
662
        $path = substr($path, $versionPathLength);
663
664
        return new Request('GET', $path, $params);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type null; however, Pinterest\Http\Request::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
665
    }
666
667
    /**
668
     * Get the pins of a board.
669
     *
670
     * @param string $boardId
671
     *
672
     * @throws RateLimitedReached
673
     *
674
     * @return Response The response
675
     */
676 2 View Code Duplication
    public function getBoardPins($boardId)
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...
677
    {
678 2
        if (empty($boardId)) {
679
            throw new InvalidArgumentException('The board id should not be empty.');
680
        }
681
682 2
        $endpoint = sprintf('boards/%s/pins/', $boardId);
683 2
        $request = new Request('GET', $endpoint);
684
685 2
        return $this->fetchMultiplePins($request);
686
    }
687
688
    /**
689
     * Get a single pin.
690
     *
691
     * @param string $pinId
692
     *
693
     * @throws RateLimitedReached
694
     *
695
     * @return Response The Response
696
     */
697 View Code Duplication
    public function getPin($pinId)
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...
698
    {
699
        if (empty($pinId)) {
700
            throw new InvalidArgumentException('The pin id should not be empty.');
701
        }
702
703
        $endpoint = sprintf('pins/%s/', $pinId);
704
        $request = new Request('GET', $endpoint);
705
706
        return $this->fetchPin($request);
707
    }
708
709
    /**
710
     * Update a pin.
711
     *
712
     * @param Pin $pin
713
     *
714
     * @throws RateLimitedReached
715
     *
716
     * @return Response The response
717
     */
718 4
    public function updatePin(Pin $pin)
719
    {
720 4
        if (empty($pin->id)) {
721 2
            throw new InvalidArgumentException('The pin id is required.');
722
        }
723
724 2
        $params = array();
725
726 2 View Code Duplication
        if (isset($pin->note) && empty($pin->note) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
727
            $params['note'] = (string) $pin->note;
728
        }
729
730 2 View Code Duplication
        if (isset($pin->link) && empty($pin->link) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
731
            $params['link'] = (string) $pin->link;
732
        }
733
734
        if (
735 2
            isset($pin->board, $pin->board->name, $pin->board->creator, $pin->board->creator->username)
736 2
            && (empty($pin->board->name) === false && empty($pin->board->creator->username) === false)
737 1
        ) {
738
            $params['board'] = "{$pin->board->creator->username}/{$pin->board->name}";
0 ignored issues
show
Bug introduced by
The property creator does not seem to exist in Pinterest\Objects\Board.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
739
        }
740
741 2
        if (empty($params)) {
742 2
            throw new InvalidArgumentException(
743 1
                "You're not changing any values. You can update a pin's note, link and/or board."
744 1
            );
745
        }
746
747
        $endpoint = sprintf('pins/%s/', $pin->id);
748
        $request = new Request('PATCH', $endpoint, $params);
749
750
        return $this->fetchPin($request);
751
    }
752
}
753