Completed
Pull Request — dev (#46)
by
unknown
45:04
created

Survey::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 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\NotBlank()
49
     * @Assert\Type("bool")
50
     * @ORM\Column(type="boolean")
51
     */
52
    private $reviewed;
53
54
    /**
55
     * @var User
56
     * @Assert\Type("object")
57
     * @Assert\Valid
58
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="surveys")
59
     * @ORM\JoinColumn(onDelete="CASCADE")
60
     */
61
    private $user;
62
63
    /**
64
     * @var ArrayCollection[SurveyAnswer]
65
     * @ORM\OneToMany(targetEntity="SurveyAnswer", mappedBy="survey", cascade={"persist", "remove"})
66
     */
67
    private $answers;
68
69 1
    public function __construct()
70
    {
71 1
        $this->answers = new ArrayCollection();
72 1
        $this->status = 'current';
73 1
        $this->reviewed = false;
74 1
    }
75
76
    /**
77
     * Get id.
78
     *
79
     * @return int
80
     */
81 3
    public function getId()
82
    {
83 3
        return $this->id;
84
    }
85
86
    /**
87
     * Set type.
88
     *
89
     * @param SurveyType $type
90
     *
91
     * @return Survey
92
     */
93 1
    public function setType(SurveyType $type)
94
    {
95 1
        $this->type = $type;
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * Get type.
102
     *
103
     * @return SurveyType
104
     */
105 7
    public function getType()
106
    {
107 7
        return $this->type;
108
    }
109
110
    /**
111
     * Set status.
112
     *
113
     * @param string $status
114
     *
115
     * @return Survey
116
     */
117 1
    public function setStatus($status)
118
    {
119 1
        $this->status = $status;
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * Get status.
126
     *
127
     * @return string
128
     */
129 3
    public function getStatus()
130
    {
131 3
        return $this->status;
132
    }
133
134
    /**
135
     * Set review.
136
     *
137
     * @param bool $review
138
     *
139
     * @return Survey
140
     */
141 1
    public function setReviewed($review)
142
    {
143 1
        $this->reviewed = $review;
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * Get review.
150
     *
151
     * @return bool
152
     */
153 2
    public function isReviewed()
154
    {
155 2
        return $this->reviewed;
156
    }
157
158
    /**
159
     * Set user.
160
     *
161
     * @param User $user
162
     *
163
     * @return Survey
164
     */
165 1
    public function setUser(User $user)
166
    {
167 1
        $this->user = $user;
168
169 1
        return $this;
170
    }
171
172
    /**
173
     * Get user.
174
     *
175
     * @return User
176
     */
177 5
    public function getUser()
178
    {
179 5
        return $this->user;
180
    }
181
182
    /**
183
     * @param SurveyAnswer $answer
184
     *
185
     * @return Survey
186
     */
187
    public function addSurveyAnswer(SurveyAnswer $answer)
188
    {
189
        if (!$this->answers->contains($answer)) {
190
            $this->answers->add($answer);
191
            $answer->setSurvey($this);
192
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * Get answers.
199
     *
200
     * @return ArrayCollection
201
     */
202
    public function getAnswers()
203
    {
204
        return $this->answers;
205
    }
206
207
    /**
208
     * Get questions.
209
     *
210
     * @return array
211
     */
212 1
    public function getQuestions()
213
    {
214 1
        $sections = $this->getType()->getSections();
215 1
        foreach ($sections as $section) {
216 1
            foreach ($section->getQuestions() as $quest) {
217 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...
218
            }
219
        }
220
221 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...
222
    }
223
}
224