Completed
Push — master ( aca645...6c67bd )
by Jan Philip
06:33 queued 04:28
created

Day::getDish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace JPBernius\FMeat\Entities;
4
5
use ArrayIterator;
6
use DateTime;
7
use DateTimeInterface;
8
use IteratorAggregate;
9
use JPBernius\FMeat\Exeptions\DishNotFoundException;
10
11
class Day implements Entity, IteratorAggregate
12
{
13
14
    /** @var DateTimeInterface */
15
    private $date;
16
17
    /** @var array */
18
    private $dishes = [];
19
20
    public function __construct(DateTimeInterface $date)
21
    {
22
        $this->date = $date;
23
    }
24
25
    /**
26
     * @param Dish $dish
27
     */
28
    public function addDish(Dish $dish): void
29
    {
30
        if (!in_array($dish, $this->dishes)) {
31
            $this->dishes[] = $dish;
32
        }
33
    }
34
35
    /**
36
     * @param int $index
37
     * @return Dish
38
     * @throws DishNotFoundException
39
     */
40
    public function getDish(int $index): Dish
41
    {
42
        if (empty($this->dishes[$index])) {
43
            throw new DishNotFoundException();
44
        }
45
46
        return $this->dishes[$index];
47
    }
48
49
    public function getDate(): DateTimeInterface
50
    {
51
        return $this->date;
52
    }
53
54
    /**
55
     * @param \stdClass $jsonObject
56
     * @return Day
57
     */
58
    public static function fromJson(\stdClass $jsonObject): self
59
    {
60
        $date = DateTime::createFromFormat('Y-m-d', $jsonObject->date);
61
        $day = new Day($date);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor...-d', $jsonObject->date) on line 60 can also be of type false; however, JPBernius\FMeat\Entities\Day::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
62
63
        foreach ($jsonObject->dishes as $jsonDishObject) {
64
            $dish = Dish::fromJson($jsonDishObject);
65
            $day->addDish($dish);
66
        }
67
68
        return $day;
69
    }
70
71
    public function getDayOfWeek(): int
72
    {
73
        return intval($this->date->format('N'));
74
    }
75
76
    public function getIterator()
77
    {
78
        return new ArrayIterator($this->dishes);
79
    }
80
}