Passed
Push — master ( c8b764...e7a993 )
by Ryan
11:17
created

Date::__construct()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 6
nop 3
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2017–2018 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2018 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Parser;
12
13
use DateTime;
14
use DateTimeZone;
15
16
class Date
17
{
18
    /**
19
     * The input datestamp.
20
     *
21
     * @var string
22
     */
23
    protected $datestamp;
24
25
    /**
26
     * The desired output timezone.
27
     *
28
     * @var string|null
29
     */
30
    protected $outputTimezone;
31
32
    /**
33
     * The format used to assist date string parsing.
34
     *
35
     * @var string|null
36
     */
37
    protected $createFromFormat;
38
39
    /**
40
     * The resulting `DateTime` object.
41
     *
42
     * @var DateTime
43
     */
44
    protected $dateTime;
45
46
    /**
47
     * Constructs a new instance of this class.
48
     *
49
     * Timezone calculation is performed on a _best-effort_ basis and is not guaranteed. Factors which may affect the
50
     * calculation include:
51
     *
52
     * * the version of glibc/musl that your OS relies on.
53
     * * the freshness of the timestamp data your OS relies on.
54
     * * the format of the datestamp inside of the feed and PHP's ability to parse it.
55
     *
56
     * @param string      $datestamp        The datestamp to handle, as a string.
57
     * @param string|null $outputTimezone   The timezone identifier to use. Must be compatible with `DateTimeZone`.
58
     *                                      The default value is `UTC`.
59
     * @param string|null $createFromFormat Allows the user to assist the date parser by providing the input format of
60
     *                                      the datestamp. This will be passed into `DateTime::createFromFormat()`
61
     *                                      at parse-time.
62
     *
63
     * @see http://php.net/manual/en/datetime.createfromformat.php
64
     */
65
    public function __construct(string $datestamp, ?string $outputTimezone = 'UTC', ?string $createFromFormat = null)
66
    {
67
        $this->datestamp        = $datestamp;
68
        $this->createFromFormat = $createFromFormat;
69
70
        // Convert null to UTC; Convert Z to UTC.
71
        if (null === $outputTimezone) {
72
            $this->outputTimezone = 'UTC';
73
        } elseif ('Z' === strtoupper($outputTimezone)) {
74
            $this->outputTimezone = 'UTC';
75
        } else {
76
            $this->outputTimezone = $outputTimezone;
77
        }
78
79
        // Use the custom formatter, if available
80
        if (null !== $this->createFromFormat) {
81
            $this->dateTime = DateTime::createFromFormat(
0 ignored issues
show
Documentation Bug introduced by
It seems like DateTime::createFromForm...$this->outputTimezone)) can also be of type false. However, the property $dateTime is declared as type 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...
82
                $this->createFromFormat,
83
                $this->datestamp,
84
                new DateTimeZone($this->outputTimezone)
85
            );
86
        } else {
87
            $this->dateTime = new DateTime(
88
                $this->datestamp,
89
                new DateTimeZone($this->outputTimezone)
90
            );
91
        }
92
93
        // Sometimes, `createFromFormat()` doesn't set this correctly.
94
        $this->dateTime->setTimezone(new DateTimeZone($this->outputTimezone));
95
    }
96
97
    /**
98
     * Get the input datestamp.
99
     *
100
     * @return string
101
     */
102
    public function getDatestamp(): string
103
    {
104
        return $this->datestamp;
105
    }
106
107
    /**
108
     * Get the requested output timezone.
109
     *
110
     * @return string
111
     */
112
    public function getOutputTimezone(): string
113
    {
114
        return $this->outputTimezone;
115
    }
116
117
    /**
118
     * Get the format used to assist date string parsing.
119
     *
120
     * @return string|null
121
     */
122
    public function getCreateFromFormat(): ?string
123
    {
124
        return $this->createFromFormat;
125
    }
126
127
    /**
128
     * Get the resulting `DateTime` object.
129
     *
130
     * @return DateTime
131
     */
132
    public function getDateTime()
133
    {
134
        return $this->dateTime;
135
    }
136
}
137