Completed
Pull Request — master (#478)
by Luc
02:16
created

Timestamp::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Model\ValueObject\Calendar\DateRange;
7
use DateTime;
8
use DateTimeInterface;
9
use InvalidArgumentException;
10
11
final class Timestamp implements SerializableInterface
12
{
13
    /**
14
     * @var DateTimeInterface
15
     */
16
    protected $startDate;
17
18
    /**
19
     * @var DateTimeInterface
20
     */
21
    protected $endDate;
22
23
    final public function __construct(
24
        DateTimeInterface $startDate,
25
        DateTimeInterface $endDate
26
    ) {
27
        if ($endDate < $startDate) {
28
            throw new InvalidArgumentException('End date can not be earlier than start date.');
29
        }
30
31
        $this->startDate = $startDate;
32
        $this->endDate = $endDate;
33
    }
34
35
    public function getStartDate(): DateTimeInterface
36
    {
37
        return $this->startDate;
38
    }
39
40
    public function getEndDate(): DateTimeInterface
41
    {
42
        return $this->endDate;
43
    }
44
45
    public static function deserialize(array $data): Timestamp
46
    {
47
        return new static(
48
            DateTime::createFromFormat(DateTime::ATOM, $data['startDate']),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...OM, $data['startDate']) targeting DateTime::createFromFormat() can also be of type false; however, CultuurNet\UDB3\Timestamp::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
49
            DateTime::createFromFormat(DateTime::ATOM, $data['endDate'])
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...ATOM, $data['endDate']) targeting DateTime::createFromFormat() can also be of type false; however, CultuurNet\UDB3\Timestamp::__construct() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
50
        );
51
    }
52
53
    public function serialize(): array
54
    {
55
        return [
56
            'startDate' => $this->startDate->format(DateTime::ATOM),
57
            'endDate' => $this->endDate->format(DateTime::ATOM),
58
        ];
59
    }
60
61
    public static function fromUdb3ModelDateRange(DateRange $dateRange): Timestamp
62
    {
63
        return new Timestamp(
64
            $dateRange->getFrom(),
0 ignored issues
show
Bug introduced by
It seems like $dateRange->getFrom() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
65
            $dateRange->getTo()
0 ignored issues
show
Bug introduced by
It seems like $dateRange->getTo() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66
        );
67
    }
68
69
    public function equals(Timestamp $otherTimestamp): bool
70
    {
71
        return $this->serialize() === $otherTimestamp->serialize();
72
    }
73
}
74