Completed
Pull Request — master (#68)
by
unknown
06:56
created

Card::getLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Trello\Model;
4
5
use Trello\Events;
6
use Trello\Exception\InvalidArgumentException;
7
use Trello\Exception\RuntimeException;
8
9
/**
10
 * @codeCoverageIgnore
11
 */
12
class Card extends AbstractObject implements CardInterface
13
{
14
    protected $apiName = 'card';
15
16
    protected $loadParams = array(
17
        'fields'          => 'all',
18
        'board'           => true,
19
        'list'            => true,
20
        'stickers'        => true,
21
        'members'         => true,
22
        'membersVoted'    => true,
23
        'attachments'     => true,
24
        'checklists'      => 'all',
25
        'checkItemStates' => true,
26
        'labels'          => true,
27
        'actions'         => Events::CARD_COMMENT,
28
    );
29
30
    protected $newChecklists = array();
31
    protected $newComments = array();
32
    protected $commentsToBeRemoved = array();
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getShortId()
38
    {
39
        return $this->data['idShort'];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function setName($name)
46
    {
47
        $this->data['name'] = $name;
48
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getName()
56
    {
57
        return $this->data['name'];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function setDescription($desc)
64
    {
65
        $this->data['desc'] = $desc;
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getDescription()
74
    {
75
        return $this->data['desc'];
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getDescriptionData()
82
    {
83
        return $this->data['descData'];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getUrl()
90
    {
91
        return $this->data['url'];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getShortUrl()
98
    {
99
        return $this->data['shortUrl'];
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getShortLink()
106
    {
107
        return $this->data['shortLink'];
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function setPosition($pos)
114
    {
115
        $this->data['pos'] = $pos;
116
117
        return $this;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getPosition()
124
    {
125
        return $this->data['pos'];
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function setDueDate(\DateTime $due = null)
132
    {
133
        $this->data['due'] = $due;
134
135
        return $this;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getDueDate()
142
    {
143
        if ($this->data['due'] instanceof \DateTime) {
144
            return $this->data['due'];
145
        }
146
147
        return new \DateTime($this->data['due']);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function setEmail($email)
154
    {
155
        $this->data['email'] = $email;
156
157
        return $this;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getEmail()
164
    {
165
        return $this->data['email'];
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function setClosed($closed)
172
    {
173
        $this->data['closed'] = $closed;
174
175
        return $this;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function isClosed()
182
    {
183
        return $this->data['closed'];
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function setSubscribed($subscribed)
190
    {
191
        $this->data['subscribed'] = $subscribed;
192
193
        return $this;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function isSubscribed()
200
    {
201
        return $this->data['subscribed'];
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function setCheckItemStates(array $checkItemStates)
208
    {
209
        $this->data['checkItemStates'] = $checkItemStates;
210
211
        return $this;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getCheckItemStates()
218
    {
219
        return $this->data['checkItemStates'];
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function setBoardId($boardId)
226
    {
227
        $this->data['idBoard'] = $boardId;
228
229
        return $this;
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function getBoardId()
236
    {
237
        return $this->data['idBoard'];
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function setBoard(BoardInterface $board)
244
    {
245
        return $this->setBoardId($board->getId());
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function getBoard()
252
    {
253
        return new Board($this->client, $this->getBoardId());
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function setListId($listId)
260
    {
261
        $this->data['idList'] = $listId;
262
263
        return $this;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function getListId()
270
    {
271
        return $this->data['idList'];
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function setList(CardlistInterface $list)
278
    {
279
        return $this->setListId($list->getId());
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285
    public function getList()
286
    {
287
        return new Cardlist($this->client, $this->getListId());
288
    }
289
290
    public function moveToList($name)
291
    {
292
        foreach ($this->getBoard()->getLists() as $list) {
293
            if ($list->getName() === $name) {
294
                $this->setList($list);
295
296
                return $this;
297
            }
298
        }
299
300
        throw new InvalidArgumentException(sprintf(
301
            'Card "%s" could not be moved to list "%s" as no list with that name exists on the board named "%s"',
302
            $this->getName(),
303
            $name,
304
            $this->getBoard()->getName()
305
        ));
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    public function setChecklistIds(array $checklistIds)
312
    {
313
        $this->data['idChecklists'] = $checklistIds;
314
315
        return $this;
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function getChecklistIds()
322
    {
323
        return $this->data['idChecklists'];
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function setChecklists(array $checklists)
330
    {
331
        $ids = array();
332
333
        foreach ($checklists as $checklist) {
334
            $ids[] = $checklist->getId();
335
        }
336
337
        return $this->setChecklistIds($ids);
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function getChecklists()
344
    {
345
        $checklists = array();
346
347
        foreach ($this->getChecklistIds() as $id) {
348
            $checklists[] = new Checklist($this->client, $id);
349
        }
350
351
        $checklists = array_merge($checklists, $this->newChecklists);
352
353
        return $checklists;
354
    }
355
356
    /**
357
     * {@inheritdoc}
358
     * @param string $name
359
     */
360
    public function getChecklist($name)
361
    {
362
        foreach ($this->getChecklists() as $checklist) {
363
            if ($checklist->getName() === $name) {
364
                return $checklist;
365
            }
366
        }
367
368
        throw new InvalidArgumentException(sprintf(
369
            'There is no checklist named "%s"  on this card (%s).',
370
            $name,
371
            $this->getName()
372
        ));
373
    }
374
375
    /**
376
     * {@inheritdoc}
377
     */
378
    public function hasChecklist($checklist)
379
    {
380
        if ($checklist instanceof ChecklistInterface) {
381
            return in_array($checklist->getId(), $this->data['idChecklists']);
382
        }
383
384
        foreach ($this->getChecklists() as $existingList) {
385
            if ($existingList->getName() === $checklist) {
386
                return true;
387
            }
388
        }
389
390
        return false;
391
    }
392
393
    /**
394
     * {@inheritdoc}
395
     */
396
    public function addChecklist($checklist)
397
    {
398
        if (!$this->id) {
399
            throw new RuntimeException("You can't add checklists to a new card, you have to save it first.");
400
        }
401
402
        if (!$checklist instanceof ChecklistInterface) {
403
            $name = $checklist;
404
            $checklist = new Checklist($this->client);
405
            $checklist->setName($name);
406
        }
407
408
        $checklist->setCard($this);
409
410
        if (!$checklist->getId()) {
411
            $this->newChecklists[] = $checklist;
412
413
            return $this;
414
        }
415
416
        if ($this->hasChecklist($checklist)) {
417
            throw new InvalidArgumentException(sprintf(
418
                'Checklist %s is already on this card (%s).',
419
                $checklist->getName(),
420
                $this->getName()
421
            ));
422
        }
423
424
        $this->data['idChecklists'][] = $checklist->getId();
425
426
        return $this;
427
    }
428
429
    /**
430
     * {@inheritdoc}
431
     */
432
    public function removeChecklist($checklist)
433
    {
434
        if (!$this->hasChecklist($checklist)) {
435
            throw new InvalidArgumentException(sprintf(
436
                "Checklist %s is not on this card (%s), so you can't remove it.",
437
                is_object($checklist) ? $checklist->getName() : $checklist,
438
                $this->getName()
439
            ));
440
        }
441
442
        if (!$checklist instanceof ChecklistInterface) {
443
            $checklist = $this->getChecklist($checklist);
444
        }
445
446
        foreach ($this->data['idChecklists'] as $key => $checklistId) {
447
            if ($checklistId === $checklist->getId()) {
448
                unset($this->data['idChecklists'][$key]);
449
                $checklist->remove();
450
            }
451
        }
452
453
        return $this;
454
    }
455
456
    /**
457
     * {@inheritdoc}
458
     */
459
    public function setMemberIds(array $memberIds)
460
    {
461
        $this->data['idMembers'] = $memberIds;
462
463
        return $this;
464
    }
465
466
    /**
467
     * {@inheritdoc}
468
     */
469
    public function getMemberIds()
470
    {
471
        return $this->data['idMembers'];
472
    }
473
474
    /**
475
     * {@inheritdoc}
476
     */
477
    public function hasMember(MemberInterface $member)
478
    {
479
        return in_array($member->getId(), $this->data['idMembers']);
480
    }
481
482
    /**
483
     * {@inheritdoc}
484
     */
485 View Code Duplication
    public function addMember(MemberInterface $member)
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...
486
    {
487
        if ($this->hasMember($member)) {
488
            throw new InvalidArgumentException(sprintf(
489
                'Member %s is already on this card (%s).',
490
                $member->getFullName(),
491
                $this->getName()
492
            ));
493
        }
494
495
        $this->data['idMembers'][] = $member->getId();
496
497
        return $this;
498
    }
499
500
    /**
501
     * {@inheritdoc}
502
     */
503 View Code Duplication
    public function removeMember(MemberInterface $member)
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...
504
    {
505
        if (!$this->hasMember($member)) {
506
            throw new InvalidArgumentException(sprintf(
507
                "Member %s is not on this card (%s), so you can't remove him.",
508
                $member->getFullName(),
509
                $this->getName()
510
            ));
511
        }
512
513
        foreach ($this->data['idMembers'] as $key => $memberArray) {
514
            if ($memberArray['id'] === $member->getId()) {
515
                unset($this->data['idMembers'][$key]);
516
            }
517
        }
518
519
        return $this;
520
    }
521
522
    /**
523
     * {@inheritdoc}
524
     */
525
    public function setMembers(array $members)
526
    {
527
        $ids = array();
528
529
        foreach ($members as $member) {
530
            $ids[] = $member->getId();
531
        }
532
533
        return $this->setMemberIds($ids);
534
    }
535
536
    /**
537
     * {@inheritdoc}
538
     */
539
    public function getMembers()
540
    {
541
        $members = array();
542
543
        foreach ($this->getMemberIds() as $id) {
544
            $members[] = new Member($this->client, $id);
545
        }
546
547
        return $members;
548
    }
549
550
    /**
551
     * {@inheritdoc}
552
     */
553
    public function setMembersVotedIds(array $membersVotedIds)
554
    {
555
        $this->data['idMembersVoted'] = $membersVotedIds;
556
557
        return $this;
558
    }
559
560
    /**
561
     * {@inheritdoc}
562
     */
563
    public function getMembersVotedIds()
564
    {
565
        return $this->data['idMembersVoted'];
566
    }
567
568
    /**
569
     * {@inheritdoc}
570
     */
571
    public function hasMemberVoted(MemberInterface $member)
572
    {
573
        return in_array($member->getId(), $this->data['idMembersVoted']);
574
    }
575
576
    /**
577
     * {@inheritdoc}
578
     */
579 View Code Duplication
    public function addMemberVoted(MemberInterface $member)
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...
580
    {
581
        if ($this->hasMemberVoted($member)) {
582
            throw new InvalidArgumentException(sprintf(
583
                'Member %s has already voted this card (%s).',
584
                $member->getFullName(),
585
                $this->getName()
586
            ));
587
        }
588
589
        $this->data['idMembersVoted'][] = $member->getId();
590
591
        return $this;
592
    }
593
594
    /**
595
     * {@inheritdoc}
596
     */
597 View Code Duplication
    public function removeMemberVoted(MemberInterface $member)
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 (!$this->hasMemberVoted($member)) {
600
            throw new InvalidArgumentException(sprintf(
601
                "Member %s hasn't voted this card (%s), so you can't remove his vote.",
602
                $member->getFullName(),
603
                $this->getName()
604
            ));
605
        }
606
607
        foreach ($this->data['idMembersVoted'] as $key => $memberArray) {
608
            if ($memberArray['id'] === $member->getId()) {
609
                unset($this->data['idMembersVoted'][$key]);
610
            }
611
        }
612
613
        return $this;
614
    }
615
616
    /**
617
     * {@inheritdoc}
618
     */
619
    public function setMembersVoted(array $members)
620
    {
621
        $ids = array();
622
623
        foreach ($members as $member) {
624
            $ids[] = $member->getId();
625
        }
626
627
        return $this->setMembersVotedIds($ids);
628
    }
629
630
    /**
631
     * {@inheritdoc}
632
     */
633
    public function getMembersVoted()
634
    {
635
        $members = array();
636
637
        foreach ($this->getMembersVotedIds() as $id) {
638
            $members[] = new Member($this->client, $id);
639
        }
640
641
        return $members;
642
    }
643
644
    /**
645
     * {@inheritdoc}
646
     */
647
    public function setAttachmentCoverId($attachmentCoverId)
648
    {
649
        $this->data['idAttachmentCover'] = $attachmentCoverId;
650
651
        return $this;
652
    }
653
654
    /**
655
     * {@inheritdoc}
656
     */
657
    public function getAttachmentCoverId()
658
    {
659
        return $this->data['idAttachmentCover'];
660
    }
661
662
    /**
663
     * {@inheritdoc}
664
     */
665
    public function setManualCoverAttachment($coverAttachment)
666
    {
667
        $this->data['manualCoverAttachment'] = $coverAttachment;
668
669
        return $this;
670
    }
671
672
    /**
673
     * {@inheritdoc}
674
     */
675
    public function getManualCoverAttachment()
676
    {
677
        return $this->data['manualCoverAttachment'];
678
    }
679
680
    /**
681
     * {@inheritdoc}
682
     */
683
    public function setLabels(array $labels)
684
    {
685
        $this->data['idLabels'] = $labels;
686
687
        return $this;
688
    }
689
690
    /**
691
     * {@inheritdoc}
692
     */
693
    public function getLabelIds()
694
    {
695
        return $this->data['idLabels'];
696
    }
697
698
    /**
699
     * {@inheritdoc}
700
     */
701
    public function getLabels()
702
    {
703
        return $this->data['labels'];
704
    }
705
706
    /**
707
     * {@inheritdoc}
708
     */
709
    public function hasLabel(LabelInterface $label)
710
    {
711
        return in_array($label->getId(), $this->data['idLabels']);
712
    }
713
714
    /**
715
     * {@inheritdoc}
716
     */
717
    public function addLabel(LabelInterface $label)
718
    {
719
        $this->data['idLabels'][] = $label->getId();
720
        return $this;
721
    }
722
723
    /**
724
     * {@inheritdoc}
725
     */
726
    public function removeLabel(LabelInterface $label)
727
    {
728
        foreach ($this->data['idLabels'] as $key => $id) {
729
            if ($id === $label->getId()) {
730
                unset($this->data['idLabels'][$key]);
731
            }
732
        }
733
        return $this;
734
    }
735
736
    /**
737
     * {@inheritdoc}
738
     */
739
    public function setBadges(array $badges)
740
    {
741
        $this->data['badges'] = $badges;
742
743
        return $this;
744
    }
745
746
    /**
747
     * {@inheritdoc}
748
     */
749
    public function getBadges()
750
    {
751
        return $this->data['badges'];
752
    }
753
754
    /**
755
     * {@inheritdoc}
756
     */
757
    public function getDateOfLastActivity()
758
    {
759
        return new \DateTime($this->data['dateLastActivity']);
760
    }
761
762
    /**
763
     * {@inheritdoc}
764
     */
765
    public function getActions($params = array())
766
    {
767
        return $this->api->actions()->all($this->id, $params);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Trello\Api\ApiInterface as the method actions() does only exist in the following implementations of said interface: Trello\Api\Board, Trello\Api\Card, Trello\Api\Cardlist, Trello\Api\Member.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
768
    }
769
770
    /**
771
     * {@inheritdoc}
772
     */
773
    public function addComment($text)
774
    {
775
        $this->newComments[] = $text;
776
777
        return $this;
778
    }
779
780
    /**
781
     * {@inheritdoc}
782
     */
783
    public function removeComment($commentId)
784
    {
785
        $this->commentsToBeRemoved[] = $commentId;
786
787
        return $this;
788
    }
789
790
    /**
791
     * {@inheritdoc}
792
     */
793
    protected function preSave()
794
    {
795
        foreach ($this->newChecklists as $checklist) {
796
            $checklist->save();
797
            $this->addChecklist($checklist);
798
        }
799
    }
800
801
    /**
802
     * {@inheritdoc}
803
     */
804
    protected function postSave()
805
    {
806
        foreach ($this->newComments as $key => $text) {
807
            $this->api->actions()->addComment($this->id, $text);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Trello\Api\ApiInterface as the method actions() does only exist in the following implementations of said interface: Trello\Api\Board, Trello\Api\Card, Trello\Api\Cardlist, Trello\Api\Member.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
808
            unset($this->newComments[$key]);
809
        }
810
811
        foreach ($this->commentsToBeRemoved as $key => $commentId) {
812
            $this->api->actions()->removeComment($this->id, $commentId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Trello\Api\ApiInterface as the method actions() does only exist in the following implementations of said interface: Trello\Api\Board, Trello\Api\Card, Trello\Api\Cardlist, Trello\Api\Member.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
813
            unset($this->commentsToBeRemoved[$key]);
814
        }
815
    }
816
}
817