Completed
Push — master ( a95c92...e40b6a )
by Beñat
04:36
created

CommentCountTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setCommentCount() 0 6 1
A getCommentCount() 0 4 1
A addComment() 0 6 1
A removeComment() 0 6 1
A getComments() 0 4 1
A loadCommentCount() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2015 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\Model\Traits;
13
14
use BenatEspina\StackExchangeApiClient\Model\Comment;
15
use BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface;
16
use BenatEspina\StackExchangeApiClient\Util\Util;
17
18
/**
19
 * Class CommentCountTrait.
20
 *
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
trait CommentCountTrait
24
{
25
    /**
26
     * Number of comments.
27
     *
28
     * @var int
29
     */
30
    protected $commentCount;
31
32
    /**
33
     * An array of comments.
34
     *
35
     * @var array<\BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface>|null
36
     */
37
    protected $comments = [];
38
39
    /**
40
     * Sets number of comments.
41
     *
42
     * @param int $commentCount The number of comments
43
     *
44
     * @return $this self Object
45
     */
46
    public function setCommentCount($commentCount)
47
    {
48
        $this->commentCount = $commentCount;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Gets number of comments.
55
     *
56
     * @return int
57
     */
58
    public function getCommentCount()
59
    {
60
        return $this->commentCount;
61
    }
62
63
    /**
64
     * Adds comment.
65
     *
66
     * @param \BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface|null $comment The comment object
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $comment a bit more specific; maybe use CommentInterface.
Loading history...
67
     *
68
     * @return $this self Object
69
     */
70
    public function addComment(CommentInterface $comment)
71
    {
72
        $this->comments[] = $comment;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Removes comment.
79
     *
80
     * @param \BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface|null $comment The comment object
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $comment a bit more specific; maybe use CommentInterface.
Loading history...
81
     *
82
     * @return $this self Object
83
     */
84
    public function removeComment(CommentInterface $comment)
85
    {
86
        $this->comments = Util::removeElement($comment, $this->comments);
0 ignored issues
show
Documentation Bug introduced by
It seems like \BenatEspina\StackExchan...mment, $this->comments) of type array<integer,*> is incompatible with the declared type array<integer,object<Ben...CommentInterface>>|null of property $comments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
88
        return $this;
89
    }
90
91
    /**
92
     * Gets array of comments.
93
     *
94
     * @return array<\BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface>|null
95
     */
96
    public function getComments()
97
    {
98
        return $this->comments;
99
    }
100
101
    /**
102
     * Loads the variables if the data exist into resource. It works like a constructor.
103
     *
104
     * @param null|mixed[] $resource The resource
105
     */
106
    protected function loadCommentCount($resource)
107
    {
108
        $this->commentCount = Util::setIfIntegerExists($resource, 'comment_count');
109
        $comments = Util::setIfArrayExists($resource, 'comments');
110
        foreach ($comments as $comment) {
111
            $this->comments[] = new Comment($comment);
112
        }
113
    }
114
}
115