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

GeneralizedTime   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 112
c 0
b 0
f 0
ccs 42
cts 42
cp 1
rs 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 <i>GeneralizedTime</i> 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 12
    public function __construct(\DateTimeImmutable $dt)
54
    {
55 12
        $this->_typeTag = self::TYPE_GENERALIZED_TIME;
56 12
        parent::__construct($dt);
57 12
    }
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 5
    protected function _encodedContentDER(): string
71
    {
72 5
        if (!isset($this->_formatted)) {
73 5
            $dt = $this->_dateTime->setTimezone(
74 5
                self::_createTimeZone(self::TZ_UTC));
75 5
            $this->_formatted = $dt->format('YmdHis');
76
            // if fractions were used
77 5
            $frac = $dt->format('u');
78 5
            if (0 != $frac) {
79 3
                $frac = rtrim($frac, '0');
80 3
                $this->_formatted .= ".${frac}";
81
            }
82
            // timezone
83 5
            $this->_formatted .= 'Z';
84
        }
85 5
        return $this->_formatted;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_formatted could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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