Date::modify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * Copyright (c) DateTime-Contributors
7
 *
8
 * Licensed under the MIT License. See LICENSE.md file in the project root
9
 * for full license information.
10
 */
11
12
namespace DateTime;
13
14
use DateTime\Exception\NotYetImplemented;
15
use DateTimeImmutable;
16
use DateTimeInterface;
17
use DateTimeZone;
18
19
class Date
20
{
21
    private $datetime;
22
23
    /**
24
     * Date constructor.
25
     *
26
     * @param string $date A date-string in an ISO-format.
27
     *
28
     * @throws \Exception
29
     */
30
    public function __construct(string $date)
31
    {
32
        $tempDate = new DateTimeImmutable($date);
33
        $this->datetime = new DateTimeImmutable(
34
            $tempDate->format('Y-m-d') . 'T12:00:00.0',
35
            new DateTimeZone('UTC')
36
        );
37
        $this->datetime = $this->datetime->setTime(
38
            12,
39
            00,
40
            00,
41
            0
42
        );
43
    }
44
45
    public function format(string $format) : string
46
    {
47
        $format = $this->sanitizeFormatString($format);
48
49
        return $this->datetime->format($format);
50
    }
51
52
    public function add(DateInterval $interval) : Date
53
    {
54
        $self = clone($this);
55
        $self->datetime = $this->datetime->add($interval->getDateTimeInterval());
56
57
        return $self;
58
    }
59
60
    public function sub(DateInterval $interval) : Date
61
    {
62
        $self = clone($this);
63
        $self->datetime = $this->datetime->sub($interval->getDateTimeInterval());
64
65
        return $self;
66
    }
67
68
    public function modify(string $modification) : Date
0 ignored issues
show
Unused Code introduced by
The parameter $modification is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
    public function modify(/** @scrutinizer ignore-unused */ string $modification) : Date

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        throw new NotYetImplemented('"modify" is not yet implemented');
71
        // TODO Remove all time-information from modification-string
72
        $self = clone($this);
0 ignored issues
show
Unused Code introduced by
$self = clone $this is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
73
        $self->datetime = $this->datetime->modify($modification);
74
75
        return $self;
76
    }
77
78
    public function diff(Date $date) : DateInterval
79
    {
80
        return DateInterval::fromDateTimeInterval(
81
            $this->datetime->diff($date->datetime)
0 ignored issues
show
Bug introduced by
It seems like $date->datetime can also be of type boolean; however, parameter $datetime2 of DateTimeImmutable::diff() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            $this->datetime->diff(/** @scrutinizer ignore-type */ $date->datetime)
Loading history...
82
        );
83
    }
84
85
    public static function fromDateTimeInterface(DateTimeInterface $datetime)
86
    {
87
        return new self($datetime->format('Y-m-d'));
88
    }
89
90
    public function getYear() : int
91
    {
92
        return (int) $this->datetime->format('Y');
93
    }
94
95
    public function getMonth() : int
96
    {
97
        return (int) $this->datetime->format('n');
98
    }
99
100
    public function getDay() : int
101
    {
102
        return (int) $this->datetime->format('j');
103
    }
104
105
    private function sanitizeFormatString(string $format) : string
106
    {
107
        return preg_replace('/(?<!\\\\)([aABgGhHisuveIOPTZcrU])/', '\\\\$1', $format);
108
    }
109
}
110