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 ( c4f121...b9594d )
by Joni
05:22
created

Base64   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 91.18%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
lcom 0
cbo 0
dl 0
loc 103
ccs 31
cts 34
cp 0.9118
rs 10
c 3
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A urlEncode() 0 4 1
A urlDecode() 0 18 4
A isValidURLEncoding() 0 4 1
A encode() 0 10 4
A decode() 0 10 4
A isValid() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace 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
     * @link https://en.wikipedia.org/wiki/Base64#URL_applications
16
     * @param string $data
17
     * @return string
18
     */
19 170
    public static function urlEncode(string $data): string
20
    {
21 170
        return strtr(rtrim(self::encode($data), "="), "+/", "-_");
22
    }
23
    
24
    /**
25
     * Decode a string using base64url variant.
26
     *
27
     * @link https://en.wikipedia.org/wiki/Base64#URL_applications
28
     * @param string $data
29
     * @throws \UnexpectedValueException
30
     * @return string
31
     */
32 130
    public static function urlDecode(string $data): string
33
    {
34 130
        $data = strtr($data, "-_", "+/");
35 130
        switch (strlen($data) % 4) {
36 130
            case 0:
37 78
                break;
38 112
            case 2:
39 80
                $data .= "==";
40 80
                break;
41 68
            case 3:
42 67
                $data .= "=";
43 67
                break;
44
            default:
45 1
                throw new \UnexpectedValueException(
46 1
                    "Malformed base64url encoding.");
47
        }
48 129
        return self::decode($data);
49
    }
50
    
51
    /**
52
     * Check whether string is validly base64url encoded.
53
     *
54
     * @link https://en.wikipedia.org/wiki/Base64#URL_applications
55
     * @param string $data
56
     * @return bool
57
     */
58 128
    public static function isValidURLEncoding(string $data): bool
59
    {
60 128
        return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1;
61
    }
62
    
63
    /**
64
     * Encode a string in base64.
65
     *
66
     * @link https://tools.ietf.org/html/rfc4648#section-4
67
     * @param string $data
68
     * @throws \RuntimeException If encoding fails
69
     * @return string
70
     */
71 173
    public static function encode(string $data): string
72
    {
73 173
        $ret = @base64_encode($data);
74 173
        if (!is_string($ret)) {
75
            $err = error_get_last();
76
            $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null;
77
            throw new \RuntimeException($msg ?? "base64_encode() failed.");
78
        }
79 173
        return $ret;
80
    }
81
    
82
    /**
83
     * Decode a string from base64 encoding.
84
     *
85
     * @link https://tools.ietf.org/html/rfc4648#section-4
86
     * @param string $data
87
     * @throws \RuntimeException If decoding fails
88
     * @return string
89
     */
90 131
    public static function decode(string $data): string
91
    {
92 131
        $ret = base64_decode($data, true);
93 131
        if (!is_string($ret)) {
94 1
            $err = error_get_last();
95 1
            $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null;
96 1
            throw new \RuntimeException($msg ?? "base64_decode() failed.");
97
        }
98 130
        return $ret;
99
    }
100
    
101
    /**
102
     * Check whether string is validly base64 encoded.
103
     *
104
     * @link https://tools.ietf.org/html/rfc4648#section-4
105
     * @param string $data
106
     * @return bool
107
     */
108 6
    public static function isValid(string $data): bool
109
    {
110 6
        return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1;
111
    }
112
}
113