Completed
Push — dev ( f52950...8c0d10 )
by
unknown
05:00 queued 04:55
created

Survey   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 71.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 231
ccs 33
cts 46
cp 0.7174
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A setType() 0 6 1
A getType() 0 4 1
A setStatus() 0 6 1
A getStatus() 0 4 1
A setReviewed() 0 6 1
A isReviewed() 0 4 1
A setUser() 0 6 1
A getUser() 0 4 1
A addSurveyAnswer() 0 9 2
A getAnswers() 0 4 1
A getQuestions() 0 11 3
A __construct() 0 6 1
A addAnswer() 0 6 1
A removeAnswer() 0 4 1
1
<?php
2
3
namespace AppBundle\Entity\Survey;
4
5
use AppBundle\Entity\User;
6
use Doctrine\ORM\Mapping as ORM;
7
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Symfony\Component\Validator\Constraints as Assert;
10
11
/**
12
 * Survey.
13
 *
14
 * @ORM\Entity(repositoryClass="AppBundle\Repository\SurveyRepository")
15
 */
16
class Survey
17
{
18
    use ORMBehaviors\Timestampable\Timestampable;
19
20
    /**
21
     * @var int
22
     *
23
     * @ORM\Column(type="integer")
24
     * @ORM\Id
25
     * @ORM\GeneratedValue(strategy="AUTO")
26
     */
27
    private $id;
28
29
    /**
30
     * @var SurveyType
31
     * @Assert\Type("object")
32
     * @Assert\Valid
33
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Survey\SurveyType", inversedBy="surveys")
34
     * @ORM\JoinColumn(onDelete="SET NULL")
35
     */
36
    private $type;
37
38
    /**
39
     * @var string
40
     * @Assert\NotBlank()
41
     * @Assert\Type("string")
42
     * @ORM\Column(type="string")
43
     */
44
    private $status;
45
46
    /**
47
     * @var bool
48
     * @Assert\Type("bool")
49
     * @ORM\Column(type="boolean")
50
     */
51
    private $reviewed;
52
53
    /**
54
     * @var User
55
     * @Assert\Type("object")
56
     * @Assert\Valid
57
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="surveys")
58
     * @ORM\JoinColumn(onDelete="CASCADE")
59
     */
60
    private $user;
61
62
    /**
63
     * @var ArrayCollection[SurveyAnswer]
64
     * @ORM\OneToMany(targetEntity="SurveyAnswer", mappedBy="survey", cascade={"persist", "remove"})
65
     */
66
    private $answers;
67
68 1
    public function __construct()
69
    {
70 1
        $this->answers = new ArrayCollection();
71 1
        $this->status = 'current';
72 1
        $this->reviewed = false;
73 1
    }
74
75
    /**
76
     * Get id.
77
     *
78
     * @return int
79
     */
80 2
    public function getId()
81
    {
82 2
        return $this->id;
83
    }
84
85
    /**
86
     * Set type.
87
     *
88
     * @param SurveyType $type
89
     *
90
     * @return Survey
91
     */
92 1
    public function setType(SurveyType $type)
93
    {
94 1
        $this->type = $type;
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * Get type.
101
     *
102
     * @return SurveyType
103
     */
104 6
    public function getType()
105
    {
106 6
        return $this->type;
107
    }
108
109
    /**
110
     * Set status.
111
     *
112
     * @param string $status
113
     *
114
     * @return Survey
115
     */
116 1
    public function setStatus($status)
117
    {
118 1
        $this->status = $status;
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * Get status.
125
     *
126
     * @return string
127
     */
128 3
    public function getStatus()
129
    {
130 3
        return $this->status;
131
    }
132
133
    /**
134
     * Set review.
135
     *
136
     * @param bool $review
137
     *
138
     * @return Survey
139
     */
140 1
    public function setReviewed($review)
141
    {
142 1
        $this->reviewed = $review;
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * Get review.
149
     *
150
     * @return bool
151
     */
152 1
    public function isReviewed()
153
    {
154 1
        return $this->reviewed;
155
    }
156
157
    /**
158
     * Set user.
159
     *
160
     * @param User $user
161
     *
162
     * @return Survey
163
     */
164 1
    public function setUser(User $user)
165
    {
166 1
        $this->user = $user;
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * Get user.
173
     *
174
     * @return User
175
     */
176 4
    public function getUser()
177
    {
178 4
        return $this->user;
179
    }
180
181
    /**
182
     * @param SurveyAnswer $answer
183
     *
184
     * @return Survey
185
     */
186
    public function addSurveyAnswer(SurveyAnswer $answer)
187
    {
188
        if (!$this->answers->contains($answer)) {
189
            $this->answers->add($answer);
190
            $answer->setSurvey($this);
191
        }
192
193
        return $this;
194
    }
195
196
    /**
197
     * Get answers.
198
     *
199
     * @return ArrayCollection
200
     */
201
    public function getAnswers()
202
    {
203
        return $this->answers;
204
    }
205
206
    /**
207
     * Get questions.
208
     *
209
     * @return array
210
     */
211 1
    public function getQuestions()
212
    {
213 1
        $sections = $this->getType()->getSections();
214 1
        foreach ($sections as $section) {
215 1
            foreach ($section->getQuestions() as $quest) {
216 1
                $questions[] = $quest;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$questions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $questions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
217
            }
218
        }
219
220 1
        return $questions;
0 ignored issues
show
Bug introduced by
The variable $questions does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
221
    }
222
223
    /**
224
     * Add answer
225
     *
226
     * @param \AppBundle\Entity\Survey\SurveyAnswer $answer
227
     *
228
     * @return Survey
229
     */
230
    public function addAnswer(\AppBundle\Entity\Survey\SurveyAnswer $answer)
231
    {
232
        $this->answers[] = $answer;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Remove answer
239
     *
240
     * @param \AppBundle\Entity\Survey\SurveyAnswer $answer
241
     */
242
    public function removeAnswer(\AppBundle\Entity\Survey\SurveyAnswer $answer)
243
    {
244
        $this->answers->removeElement($answer);
245
    }
246
}
247