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
Push — master ( a6a1be...553b87 )
by Dan
09:17
created

Request::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 7
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
			return $var[$index];
93
		}
94
		return self::get($index, $default);
95
	}
96
97
	/**
98
	 * Like getVar, but returns an int instead of a string.
99
	 */
100
	public static function getVarInt(string $index, int $default = null) : int {
101
		global $var;
102
		if (isset($var[$index])) {
103
			return $var[$index];
104
		}
105
		return self::getInt($index, $default);
106
	}
107
108
}
109