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 (880eb0)
by Joni
05:58
created

Base64::urlDecode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\Util;
6
7
/**
8
 * Class offering Base64 encoding and decoding.
9
 */
10
class Base64
11
{
12
    /**
13
     * Encode a string using base64url variant.
14
     *
15
     * @see https://en.wikipedia.org/wiki/Base64#URL_applications
16
     *
17
     * @param string $data
18
     *
19
     * @return string
20
     */
21 170
    public static function urlEncode(string $data): string
22
    {
23 170
        return strtr(rtrim(self::encode($data), '='), '+/', '-_');
24
    }
25
26
    /**
27
     * Decode a string using base64url variant.
28
     *
29
     * @see https://en.wikipedia.org/wiki/Base64#URL_applications
30
     *
31
     * @param string $data
32
     *
33
     * @throws \UnexpectedValueException
34
     *
35
     * @return string
36
     */
37 130
    public static function urlDecode(string $data): string
38
    {
39 130
        $data = strtr($data, '-_', '+/');
40 130
        switch (strlen($data) % 4) {
41 130
            case 0:
42 78
                break;
43 112
            case 2:
44 80
                $data .= '==';
45 80
                break;
46 68
            case 3:
47 67
                $data .= '=';
48 67
                break;
49
            default:
50 1
                throw new \UnexpectedValueException(
51 1
                    'Malformed base64url encoding.');
52
        }
53 129
        return self::decode($data);
54
    }
55
56
    /**
57
     * Check whether string is validly base64url encoded.
58
     *
59
     * @see https://en.wikipedia.org/wiki/Base64#URL_applications
60
     *
61
     * @param string $data
62
     *
63
     * @return bool
64
     */
65 141
    public static function isValidURLEncoding(string $data): bool
66
    {
67 141
        return 1 === preg_match('#^[A-Za-z0-9\-_]*$#', $data);
68
    }
69
70
    /**
71
     * Encode a string in base64.
72
     *
73
     * @see https://tools.ietf.org/html/rfc4648#section-4
74
     *
75
     * @param string $data
76
     *
77
     * @return string
78
     */
79 173
    public static function encode(string $data): string
80
    {
81 173
        return base64_encode($data);
82
    }
83
84
    /**
85
     * Decode a string from base64 encoding.
86
     *
87
     * @see https://tools.ietf.org/html/rfc4648#section-4
88
     *
89
     * @param string $data
90
     *
91
     * @throws \RuntimeException If decoding fails
92
     *
93
     * @return string
94
     */
95 131
    public static function decode(string $data): string
96
    {
97 131
        $ret = base64_decode($data, true);
98 131
        if (!is_string($ret)) {
1 ignored issue
show
introduced by
The condition is_string($ret) is always true.
Loading history...
99 1
            $err = error_get_last();
100 1
            $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null;
101 1
            throw new \RuntimeException($msg ?? 'base64_decode() failed.');
102
        }
103 130
        return $ret;
104
    }
105
106
    /**
107
     * Check whether string is validly base64 encoded.
108
     *
109
     * @see https://tools.ietf.org/html/rfc4648#section-4
110
     *
111
     * @param string $data
112
     *
113
     * @return bool
114
     */
115 7
    public static function isValid(string $data): bool
116
    {
117 7
        return 1 === preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data);
118
    }
119
}
120