Completed
Pull Request — dev (#54)
by nonanerz
04:42
created

FormRequest::getDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
7
use Symfony\Component\Serializer\Annotation\Groups;
8
use Symfony\Component\Validator\Constraints as Assert;
9
10
/**
11
 * FormRequest.
12
 *
13
 * @ORM\Entity(repositoryClass="AppBundle\Repository\FormRequestRepository")
14
 */
15
class FormRequest
16
{
17
    use ORMBehaviors\Timestampable\Timestampable;
18
19
    const STATUS = ['pending', 'approved', 'rejected'];
20
    const TYPE = ['personal day', 'overnight guest', 'sick day'];
21
22
    /**
23
     * @var int
24
     *
25
     * @ORM\Column(name="id", type="integer")
26
     * @ORM\Id
27
     * @ORM\GeneratedValue(strategy="AUTO")
28
     */
29
    private $id;
30
31
    /**
32
     * @var string
33
     * @Assert\NotBlank()
34
     * @Assert\Choice(FormRequest::TYPE)
35
     * @ORM\Column(type="string", length=25)
36
     * @Groups({"Detail"})
37
     */
38
    private $type;
39
40
    /**
41
     * @var string
42
     * @Assert\NotBlank()
43
     * @Assert\Type("string")
44
     * @Assert\Length(
45
     *      max = 255
46
     * )
47
     * @ORM\Column(type="string", length=255)
48
     * @Groups({"Detail"})
49
     */
50
    private $status;
51
52
    /**
53
     * @var User
54
     * @Assert\Type("object")
55
     * @Assert\Valid
56
     * @ORM\ManyToOne(targetEntity="User", inversedBy="formRequests")
57
     */
58
    private $user;
59
60
    /**
61
     * @var \DateTime
62
     * @Assert\DateTime()
63
     * @ORM\Column(type="datetime")
64
     * @Groups({"Detail"})
65
     */
66
    private $date;
67
68
    /**
69
     * @var string
70
     *
71
     * @ORM\Column(type="string", nullable=true)
72
     * @Assert\Length(min="5", max="100")
73
     * @Groups({"Detail"})
74
     */
75
    private $reason;
76
77
    public function __construct()
78
    {
79
        $this->setStatus('pending');
80
    }
81
82
    /**
83
     * Get id.
84
     *
85
     * @return int
86
     */
87 1
    public function getId()
88
    {
89 1
        return $this->id;
90
    }
91
92
    /**
93
     * Set type.
94
     *
95
     * @param $type
96
     *
97
     * @return FormRequest
98
     */
99
    public function setType($type)
100
    {
101
        if (in_array($type, self::TYPE)) {
102
            $this->type = $type;
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * Get type.
110
     *
111
     * @return string
112
     */
113 1
    public function getType()
114
    {
115 1
        return $this->type;
116
    }
117
118
    /**
119
     * Set status.
120
     *
121
     * @param string $status
122
     *
123
     * @return FormRequest
124
     */
125
    public function setStatus($status)
126
    {
127
        if (in_array($status, self::STATUS)) {
128
            $this->status = $status;
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Get status.
136
     *
137
     * @return string
138
     */
139 1
    public function getStatus()
140
    {
141 1
        return $this->status;
142
    }
143
144
    /**
145
     * Set user.
146
     *
147
     * @param User $user
148
     *
149
     * @return FormRequest
150
     */
151
    public function setUser(User $user)
152
    {
153
        $this->user = $user;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Get user.
160
     *
161
     * @return User
162
     */
163
    public function getUser()
164
    {
165
        return $this->user;
166
    }
167
168
    /**
169
     * Set date.
170
     *
171
     * @param \DateTime $date
172
     *
173
     * @return FormRequest
174
     */
175
    public function setDate($date)
176
    {
177
        try {
178
            $this->date = \DateTime::createFromFormat(\DATE_RFC3339, $date);
0 ignored issues
show
Documentation Bug introduced by
It seems like \DateTime::createFromFormat(\DATE_RFC3339, $date) can also be of type false. However, the property $date is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
179
        } catch (\Exception $e) {
180
            $this->date = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<DateTime> of property $date.

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...
181
        }
182
183
        return $this;
184
    }
185
    /**
186
     * Get date.
187
     *
188
     * @return \DateTime
189
     */
190 1
    public function getDate()
191
    {
192 1
        return $this->date;
193
    }
194
195
    /**
196
     * @return string
197
     */
198 1
    public function getReason()
199
    {
200 1
        return $this->reason;
201
    }
202
203
    /**
204
     * @param string $reason
205
     *
206
     * @return $this
207
     */
208
    public function setReason($reason)
209
    {
210
        $this->reason = $reason;
211
212
        return $this;
213
    }
214
}
215