Completed
Push — master ( 9015f5...88a8da )
by Derek Stephen
08:27
created

Entry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Del\Expenses\Entity;
4
5
use DateTime;
6
use Del\Expenses\Value\Category;
7
use LogicException;
8
9
/**
10
 * @Entity(repositoryClass="Del\Expenses\Repository\EntryRepository")
11
 * @InheritanceType("SINGLE_TABLE")
12
 * @DiscriminatorColumn(name="type", type="string")
13
 * @DiscriminatorMap({"IN" = "Del\Expenses\Entity\Income", "OUT" = "Del\Expenses\Entity\Expenditure"})
14
 */
15
abstract class Entry implements EntryInterface
16
{
17
    const VAT_NONE = 0;
18
    const VAT_EXC = 1;
19
    const VAT_INC = 2;
20
21
    /**
22
     * @Id
23
     * @Column(type="integer")
24
     * @GeneratedValue
25
     */
26
    private $id;
27
28
    /**
29
     * @Column(type="integer")
30
     */
31
    private $userId;
32
33
    /**
34
     * @Column(type="datetime")
35
     * @var DateTime
36
     */
37
    private $date;
38
39
    /** @Column(type="decimal",precision=11,scale=2) */
40
    private $amount;
41
42
    /**
43
     * @Column(type="decimal",precision=11,scale=2)
44
     * @var float $vatRate
45
     */
46
    private $vatRate;
47
48
    /**
49
     * @Column(type="decimal",precision=11,scale=2)
50
     * @var float
51
     */
52
    private $vat;
53
54
    /**
55
     * @Column(type="decimal",precision=11,scale=2)
56
     * @var float
57
     */
58
    private $total;
59
60
    /** @Column(type="string",length=50,nullable=true) */
61
    private $category;
62
63
    /** @Column(type="string",length=50,nullable=true) */
64
    private $description;
65
66
    /** @Column(type="string",length=255,nullable=true) */
67
    private $note;
68
69
    /**
70
     * Entry constructor.
71
     * @param int $vatRate
72
     */
73 16
    public function __construct($vatRate = 0)
74
    {
75 16
        $this->vatRate = $vatRate;
0 ignored issues
show
Documentation Bug introduced by
The property $vatRate was declared of type double, but $vatRate is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
76 16
        $this->amount = 0;
77 16
        $this->vat = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $vat was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
78 16
        $this->total = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $total was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
79 16
    }
80
81
    /**
82
     * @return int
83
     */
84 4
    public function getId()
85
    {
86 4
        return $this->id;
87
    }
88
89
    /**
90
     * @param int $id
91
     * @return Entry
92
     */
93 7
    public function setId($id)
94
    {
95 7
        $this->id = (int) $id;
96 7
        return $this;
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102 4
    public function getUserId()
103
    {
104 4
        return $this->userId;
105
    }
106
107
    /**
108
     * @param mixed $userId
109
     * @return Entry
110
     */
111 7
    public function setUserId($userId)
112
    {
113 7
        $this->userId = $userId;
114 7
        return $this;
115
    }
116
117
118
119
    /**
120
     * @return DateTime
121
     */
122 2
    public function getDate()
123
    {
124 2
        return $this->date;
125
    }
126
127
    /**
128
     * @param DateTime $date
129
     * @return Entry
130
     */
131 7
    public function setDate(DateTime $date)
132
    {
133 7
        $this->date = $date;
134 7
        return $this;
135
    }
136
137
    /**
138
     * @return float
139
     */
140 5
    public function getAmount()
141
    {
142 5
        return $this->amount;
143
    }
144
145
    /**
146
     * @param float $amount
147
     * @return Entry
148
     */
149 10
    public function setAmount($amount, $vatPaid = self::VAT_NONE)
150
    {
151 10
        if ($vatPaid != self::VAT_NONE && $this->vatRate == 0) {
152
153
            throw new LogicException('Set a VAT rate before setting the amount, if inc/ex VAT');
154
155 10
        } elseif ($vatPaid == self::VAT_INC) {
156
157 1
            $fraction = 100 / $this->vatRate;
158 1
            $fraction ++ ;
159 1
            $vatAmount = $amount / $fraction;
160 1
            $total = $amount;
161 1
            $amount = $amount - $vatAmount;
162
163 10
        } elseif ($vatPaid == self::VAT_EXC) {
164
165 1
            $vatAmount = ($amount / 100) * $this->vatRate;
166 1
            $total = $amount + $vatAmount;
167
168 1
        } else {
169
170 8
            $vatAmount = 0;
171 8
            $total = $amount;
172 8
            $this->vatRate = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $vatRate was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
173
174
        }
175 10
        $this->amount = $amount;
176 10
        $this->vat = $vatAmount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $vatAmount can also be of type integer. However, the property $vat is declared as type double. 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...
177 10
        $this->total = $total;
178 10
        return $this;
179
    }
180
181
    /**
182
     * @return Category
183
     */
184 2
    public function getCategory()
185
    {
186 2
        return new Category($this->category);
187
    }
188
189
    /**
190
     * @param Category $category
191
     * @return Entry
192
     */
193 7
    public function setCategory(Category $category)
194
    {
195 7
        $this->category = $category->getValue();
196 7
        return $this;
197
    }
198
199
    /**
200
     * @return string
201
     */
202 2
    public function getDescription()
203
    {
204 2
        return $this->description;
205
    }
206
207
    /**
208
     * @param string $description
209
     * @return Entry
210
     */
211 7
    public function setDescription($description)
212
    {
213 7
        $this->description = $description;
214 7
        return $this;
215
    }
216
217
    /**
218
     * @return string
219
     */
220 2
    public function getNote()
221
    {
222 2
        return $this->note;
223
    }
224
225
    /**
226
     * @param string $note
227
     * @return Entry
228
     */
229 7
    public function setNote($note)
230
    {
231 7
        $this->note = $note;
232 7
        return $this;
233
    }
234
235
    /**
236
     * @return float
237
     */
238 4
    public function getVatRate()
239
    {
240 4
        return $this->vatRate;
241
    }
242
243
    /**
244
     * @param float $vatRate
245
     * @return Entry
246
     */
247
    public function setVatRate($vatRate)
248
    {
249
        $this->vatRate = $vatRate;
250
        return $this;
251
    }
252
253
    /**
254
     * @return float
255
     */
256 4
    public function getVat()
257
    {
258 4
        return $this->vat;
259
    }
260
261
    /**
262
     * @param float $vat
263
     * @return Entry
264
     */
265
    public function setVat($vat)
266
    {
267
        $this->vat = $vat;
268
        return $this;
269
    }
270
271
    /**
272
     * @return float
273
     */
274 4
    public function getTotal()
275
    {
276 4
        return $this->total;
277
    }
278
279
    /**
280
     * @param float $total
281
     * @return Entry
282
     */
283
    public function setTotal($total)
284
    {
285
        $this->total = $total;
286
        return $this;
287
    }
288
}