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.
Completed
Push — master ( 70b15e...4c8633 )
by Joni
04:39
created

Base64::urlDecode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 16
ccs 15
cts 15
cp 1
rs 9.2
cc 4
eloc 14
nc 4
nop 1
crap 4
1
<?php
2
3
namespace JWX\Util;
4
5
6
/**
7
 * Class offering Base64 encoding and decoding.
8
 */
9
class Base64
10
{
11
	/**
12
	 * Encode a string using base64url variant.
13
	 *
14
	 * @link https://en.wikipedia.org/wiki/Base64#URL_applications
15
	 * @param string $data
16
	 * @return string
17
	 */
18 164
	public static function urlEncode($data) {
19 164
		return strtr(rtrim(self::encode($data), "="), "+/", "-_");
20
	}
21
	
22
	/**
23
	 * Decode a string using base64url variant.
24
	 *
25
	 * @link https://en.wikipedia.org/wiki/Base64#URL_applications
26
	 * @param string $data
27
	 * @throws \UnexpectedValueException
28
	 * @return string
29
	 */
30 130
	public static function urlDecode($data) {
31 130
		$data = strtr($data, "-_", "+/");
32 130
		switch (strlen($data) % 4) {
33 130
		case 0:
34 78
			break;
35 112
		case 2:
36 80
			$data .= "==";
37 80
			break;
38 68
		case 3:
39 67
			$data .= "=";
40 67
			break;
41 1
		default:
42 1
			throw new \UnexpectedValueException("Malformed base64url encoding.");
43 1
		}
44 129
		return self::decode($data);
45
	}
46
	
47
	/**
48
	 * Check whether string is validly base64url encoded.
49
	 *
50
	 * @link https://en.wikipedia.org/wiki/Base64#URL_applications
51
	 * @param string $data
52
	 * @return bool
53
	 */
54 112
	public static function isValidURLEncoding($data) {
55 112
		return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1;
56
	}
57
	
58
	/**
59
	 * Encode a string in base64.
60
	 *
61
	 * @link https://tools.ietf.org/html/rfc4648#section-4
62
	 * @param string $data
63
	 * @throws \RuntimeException If encoding fails
64
	 * @return string
65
	 */
66 167 View Code Duplication
	public static function encode($data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67 167
		$ret = @base64_encode($data);
68 167
		if (!is_string($ret)) {
69 1
			$err = error_get_last();
70 1
			$msg = isset($err) ? $err["message"] : "base64_encode() failed.";
71 1
			throw new \RuntimeException($msg);
72
		}
73 166
		return $ret;
74
	}
75
	
76
	/**
77
	 * Decode a string from base64 encoding.
78
	 *
79
	 * @link https://tools.ietf.org/html/rfc4648#section-4
80
	 * @param string $data
81
	 * @throws \RuntimeException If decoding fails
82
	 * @return string
83
	 */
84 131
	public static function decode($data) {
85 131
		$ret = base64_decode($data, true);
86 131
		if (!is_string($ret)) {
87 1
			$err = error_get_last();
88 1
			$msg = isset($err) ? $err["message"] : "base64_decode() failed.";
89 1
			throw new \RuntimeException($msg);
90
		}
91 130
		return $ret;
92
	}
93
	
94
	/**
95
	 * Check whether string is validly base64 encoded.
96
	 *
97
	 * @link https://tools.ietf.org/html/rfc4648#section-4
98
	 * @param string $data
99
	 * @return bool
100
	 */
101 4
	public static function isValid($data) {
102 4
		return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1;
103
	}
104
}
105