GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch php72 (57c34e)
by Joni
05:01
created

TimeType::fromString()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 5
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\ASN1\Type;
6
7
use Sop\ASN1\Element;
8
9
/**
10
 * Base class for all types representing a point in time.
11
 */
12
abstract class TimeType extends Element
13
{
14
    /**
15
     * UTC timezone.
16
     *
17
     * @var string
18
     */
19
    const TZ_UTC = 'UTC';
20
21
    /**
22
     * Date and time.
23
     *
24
     * @var \DateTimeImmutable
25
     */
26
    protected $_dateTime;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param \DateTimeImmutable $dt
32
     */
33 20
    public function __construct(\DateTimeImmutable $dt)
34
    {
35 20
        $this->_dateTime = $dt;
36 20
    }
37
38
    /**
39
     * Initialize from datetime string.
40
     *
41
     * @see http://php.net/manual/en/datetime.formats.php
42
     *
43
     * @param string      $time Time string
44
     * @param null|string $tz   timezone, if null use default
45
     *
46
     * @throws \RuntimeException
47
     *
48
     * @return self
49
     */
50 6
    public static function fromString(string $time, ?string $tz = null): self
51
    {
52
        try {
53 6
            if (!isset($tz)) {
54 4
                $tz = date_default_timezone_get();
55
            }
56 6
            return new static(
57 6
                new \DateTimeImmutable($time, self::_createTimeZone($tz)));
58 2
        } catch (\Exception $e) {
59 2
            throw new \RuntimeException(
60
                'Failed to create DateTime: ' .
61 2
                self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
62
        }
63
    }
64
65
    /**
66
     * Get the date and time.
67
     *
68
     * @return \DateTimeImmutable
69
     */
70 7
    public function dateTime(): \DateTimeImmutable
71
    {
72 7
        return $this->_dateTime;
73
    }
74
75
    /**
76
     * Create DateTimeZone object from string.
77
     *
78
     * @param string $tz
79
     *
80
     * @throws \UnexpectedValueException If timezone is invalid
81
     *
82
     * @return \DateTimeZone
83
     */
84 25
    protected static function _createTimeZone(string $tz): \DateTimeZone
85
    {
86
        try {
87 25
            return new \DateTimeZone($tz);
88 1
        } catch (\Exception $e) {
89 1
            throw new \UnexpectedValueException('Invalid timezone.', 0, $e);
90
        }
91
    }
92
93
    /**
94
     * Get last error caused by DateTimeImmutable.
95
     *
96
     * @return string
97
     */
98 3
    protected static function _getLastDateTimeImmutableErrorsStr(): string
99
    {
100 3
        $errors = \DateTimeImmutable::getLastErrors()['errors'];
101 3
        return implode(', ', $errors);
102
    }
103
}
104