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
Push — master ( 95c197...564e5e )
by Joni
04:32
created

GeneralizedTime   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 107
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __clone() 0 3 1
A _encodedContentDER() 0 16 3
A _decodeFromDER() 0 34 5
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\ASN1\Type\Primitive;
6
7
use Sop\ASN1\Component\Identifier;
8
use Sop\ASN1\Component\Length;
9
use Sop\ASN1\Exception\DecodeException;
10
use Sop\ASN1\Feature\ElementBase;
11
use Sop\ASN1\Type\PrimitiveType;
12
use Sop\ASN1\Type\TimeType;
13
use Sop\ASN1\Type\UniversalClass;
14
15
/**
16
 * Implements *GeneralizedTime* type.
17
 */
18
class GeneralizedTime extends TimeType
19
{
20
    use UniversalClass;
21
    use PrimitiveType;
22
23
    /**
24
     * Regular expression to parse date.
25
     *
26
     * DER restricts format to UTC timezone (Z suffix).
27
     *
28
     * @var string
29
     */
30
    const REGEX = '#^' .
31
        '(\d\d\d\d)' . // YYYY
32
        '(\d\d)' . // MM
33
        '(\d\d)' . // DD
34
        '(\d\d)' . // hh
35
        '(\d\d)' . // mm
36
        '(\d\d)' . // ss
37
        '(?:\.(\d+))?' . // frac
38
        'Z' . // TZ
39
        '$#';
40
41
    /**
42
     * Cached formatted date.
43
     *
44
     * @var null|string
45
     */
46
    private $_formatted;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param \DateTimeImmutable $dt
52
     */
53 14
    public function __construct(\DateTimeImmutable $dt)
54
    {
55 14
        $this->_typeTag = self::TYPE_GENERALIZED_TIME;
56 14
        parent::__construct($dt);
57 14
    }
58
59
    /**
60
     * Clear cached variables on clone.
61
     */
62 1
    public function __clone()
63
    {
64 1
        $this->_formatted = null;
65 1
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 7
    protected function _encodedContentDER(): string
71
    {
72 7
        if (!isset($this->_formatted)) {
73 7
            $dt = $this->_dateTime->setTimezone(
74 7
                self::_createTimeZone(self::TZ_UTC));
75 7
            $this->_formatted = $dt->format('YmdHis');
76
            // if fractions were used
77 7
            $frac = $dt->format('u');
78 7
            if (0 !== intval($frac)) {
79 4
                $frac = rtrim($frac, '0');
80 4
                $this->_formatted .= ".{$frac}";
81
            }
82
            // timezone
83 7
            $this->_formatted .= 'Z';
84
        }
85 7
        return $this->_formatted;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 10
    protected static function _decodeFromDER(Identifier $identifier,
92
        string $data, int &$offset): ElementBase
93
    {
94 10
        $idx = $offset;
95 10
        $length = Length::expectFromDER($data, $idx)->intLength();
96 10
        $str = substr($data, $idx, $length);
97 10
        $idx += $length;
98
        /** @var string[] $match */
99 10
        if (!preg_match(self::REGEX, $str, $match)) {
100 2
            throw new DecodeException('Invalid GeneralizedTime format.');
101
        }
102 8
        [, $year, $month, $day, $hour, $minute, $second] = $match;
103 8
        if (isset($match[7])) {
104 5
            $frac = $match[7];
105
            // DER restricts trailing zeroes in fractional seconds component
106 5
            if ('0' === $frac[strlen($frac) - 1]) {
107 2
                throw new DecodeException(
108 2
                    'Fractional seconds must omit trailing zeroes.');
109
            }
110 3
            $frac = (int) $frac;
111
        } else {
112 3
            $frac = 0;
113
        }
114 6
        $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac .
115 6
            self::TZ_UTC;
116 6
        $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time,
117 6
            self::_createTimeZone(self::TZ_UTC));
118 6
        if (!$dt) {
119 1
            throw new DecodeException(
120
                'Failed to decode GeneralizedTime: ' .
121 1
                self::_getLastDateTimeImmutableErrorsStr());
122
        }
123 5
        $offset = $idx;
124 5
        return new self($dt);
125
    }
126
}
127