Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#833)
by Dan
03:33
created

Request::getVarIntArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 9
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * Should be used for getting request data for processing pages.
5
 * For display pages, see SmrSession::getRequestVar.
6
 */
7
class Request {
8
9
	/**
10
	 * Returns true if index is set.
11
	 * Note that this must be used for checkboxes, since the element is not
12
	 * posted if a box is unchecked.
13
	 */
14
	public static function has(string $index) : bool {
15
		return isset($_REQUEST[$index]);
16
	}
17
18
	/**
19
	 * Returns index value as an integer.
20
	 */
21
	public static function getInt(string $index, int $default = null) : int {
22
		if (self::has($index)) {
23
			return (int)$_REQUEST[$index];
24
		} elseif (!is_null($default)) {
25
			return $default;
26
		}
27
		throw new Exception('No request variable "' . $index . '"');
28
	}
29
30
	/**
31
	 * Returns index value as a float.
32
	 */
33
	public static function getFloat(string $index, float $default = null) : float {
34
		if (self::has($index)) {
35
			return (float)$_REQUEST[$index];
36
		} elseif (!is_null($default)) {
37
			return $default;
38
		}
39
		throw new Exception('No request variable "' . $index . '"');
40
	}
41
42
	/**
43
	 * Returns index value as an array of strings.
44
	 */
45
	public static function getArray(string $index, array $default = null) : array {
46
		if (self::has($index)) {
47
			return $_REQUEST[$index];
48
		} elseif (!is_null($default)) {
49
			return $default;
50
		}
51
		throw new Exception('No request variable "' . $index . '"');
52
	}
53
54
	/**
55
	 * Returns index value as an array of integers.
56
	 */
57
	public static function getIntArray(string $index, array $default = null) : array {
58
		if (self::has($index)) {
59
			$result = [];
60
			foreach ($_REQUEST[$index] as $key => $value) {
61
				$result[$key] = (int)$value;
62
			}
63
			return $result;
64
		} elseif (!is_null($default)) {
65
			return $default;
66
		}
67
		throw new Exception('No request variable "' . $index . '"');
68
	}
69
70
	/**
71
	 * Returns index value as a string.
72
	 */
73
	public static function get(string $index, string $default = null) : string {
74
		if (self::has($index)) {
75
			return $_REQUEST[$index];
76
		} elseif (!is_null($default)) {
77
			return $default;
78
		}
79
		throw new Exception('No request variable "' . $index . '"');
80
	}
81
82
	/**
83
	 * Returns index value as a string from either $_REQUEST or $var.
84
	 * This is useful for processing pages that need to handle data both from
85
	 * posted form inputs and from container variables.
86
	 *
87
	 * Note that this does not save the result in $var (see SmrSession).
88
	 */
89
	public static function getVar(string $index, string $default = null) : string {
90
		global $var;
91
		if (isset($var[$index])) {
92
			if (self::has($index)) {
93
				throw new Exception('Index "' . $index . '" must not be in both $var and $_REQUEST!');
94
			}
95
			return $var[$index];
96
		}
97
		return self::get($index, $default);
98
	}
99
100
	/**
101
	 * Like getVar, but returns an int instead of a string.
102
	 */
103
	public static function getVarInt(string $index, int $default = null) : int {
104
		global $var;
105
		if (isset($var[$index])) {
106
			if (self::has($index)) {
107
				throw new Exception('Index "' . $index . '" must not be in both $var and $_REQUEST!');
108
			}
109
			return $var[$index];
110
		}
111
		return self::getInt($index, $default);
112
	}
113
114
	/**
115
	 * Like getVar, but returns an array of ints instead of a string.
116
	 */
117
	public static function getVarIntArray(string $index, array $default = null) : array {
118
		global $var;
119
		if (isset($var[$index])) {
120
			if (self::has($index)) {
121
				throw new Exception('Index "' . $index . '" must not be in both $var and $_REQUEST!');
122
			}
123
			return $var[$index];
124
		}
125
		return self::getIntArray($index, $default);
126
	}
127
128
129
}
130