Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

request::indInt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Stu\Exception\InvalidParamException;
4
5
class request
6
{
7
    /** @var array<string, mixed> */
8
    private static ?array $mockVars = null;
9
10
    /**
11
     * @return array<string, mixed>
12
     */
13 149
    public static function getvars(): array
14
    {
15 149
        if (self::$mockVars !== null) {
16 147
            return self::$mockVars;
17
        }
18
19 2
        global $_GET;
20 2
        return $_GET;
21
    }
22
23
    /**
24
     * @return array<string, mixed>
25
     */
26 49
    public static function postvars(): array
27
    {
28 49
        if (self::$mockVars !== null) {
29 48
            return self::$mockVars;
30
        }
31
32 1
        global $_POST;
33 1
        return $_POST;
34
    }
35
36
    public static function isRequest(): bool
37
    {
38
        return array_key_exists('REQUEST_METHOD', $_SERVER);
39
    }
40
41
    public static function isPost(): bool
42
    {
43
        if (!static::isRequest()) {
44
            return false;
45
        }
46
47
        return $_SERVER['REQUEST_METHOD'] === 'POST';
48
    }
49
50 84
    public static function has(string $key): bool
51
    {
52 84
        return self::getvars()[$key] ?? self::postvars()[$key] ?? false;
53
    }
54
55
    /**
56
     * @param array<int|string, mixed> $method
57
     *
58
     * @return mixed
59
     */
60 94
    public static function getVarByMethod(array $method, string $var, bool $fatal = false)
61
    {
62 94
        if (!@array_key_exists($var, $method)) {
63 10
            if ($fatal === true) {
64 1
                throw new InvalidParamException(sprintf('request parameter "%s" does not exist', $var));
65
            }
66 9
            return false;
67
        }
68 89
        return $method[$var];
69
    }
70
71 8
    public static function getInt(string $var, int $std = 0): int
72
    {
73 8
        $int = self::getVarByMethod(self::getvars(), $var);
74 8
        if (strlen($int) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $int can also be of type false; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        if (strlen(/** @scrutinizer ignore-type */ $int) == 0) {
Loading history...
75 4
            return $std;
76
        }
77 4
        return self::returnInt($int);
78
    }
79
80 36
    public static function getIntFatal(string $var): int
81
    {
82 36
        $int = self::getVarByMethod(self::getvars(), $var, true);
83 36
        return self::returnInt($int);
84
    }
85
86 5
    public static function postInt(string $var): int
87
    {
88 5
        $int = self::getVarByMethod(self::postvars(), $var);
89 5
        return self::returnInt($int);
90
    }
91
92
    public static function postIntFatal(string $var): int
93
    {
94
        $int = self::getVarByMethod(self::postvars(), $var, true);
95
        return self::returnInt($int);
96
    }
97
98
    public static function getString(string $var): string|false
99
    {
100
        return self::getVarByMethod(self::getvars(), $var);
101
    }
102
103
    public static function postString(string $var): string|false
104
    {
105
        return self::getVarByMethod(self::postvars(), $var);
106
    }
107
108 2
    public static function indString(string $var): string|false
109
    {
110 2
        $value = self::getVarByMethod(self::postvars(), $var);
111 2
        if ($value) {
112
            return $value;
113
        }
114 2
        return self::getVarByMethod(self::getvars(), $var);
115
    }
116
117 42
    public static function indInt(string $var): int
118
    {
119 42
        $value = self::getVarByMethod(self::postvars(), $var);
120 42
        if ($value) {
121 40
            return self::returnInt($value);
122
        }
123 2
        return self::returnInt(self::getVarByMethod(self::getvars(), $var));
124
    }
125
126
    public static function postStringFatal(string $var): string
127
    {
128
        return self::getVarByMethod(self::postvars(), $var, true);
129
    }
130
131 32
    public static function getStringFatal(string $var): string
132
    {
133 32
        return self::getVarByMethod(self::getvars(), $var, true);
134
    }
135
136
    /**
137
     * @return array<int|string, mixed>
138
     */
139
    public static function postArrayFatal(string $var): array
140
    {
141
        return self::returnArray(self::getVarByMethod(self::postvars(), $var, true));
142
    }
143
144
    /**
145
     * @return array<int|string, mixed>
146
     */
147
    public static function getArrayFatal(string $var): array
148
    {
149
        return self::returnArray(self::getVarByMethod(self::getvars(), $var, true));
150
    }
151
152
    /**
153
     * @return array<int|string, mixed>
154
     */
155
    public static function postArray(string $var): array
156
    {
157
        return self::returnArray(self::getVarByMethod(self::postvars(), $var));
158
    }
159
160 75
    public static function returnInt($result): int
161
    {
162
        if (
163 75
            !$result
164 75
            || $result < 0
165
        ) {
166 11
            return 0;
167
        }
168 72
        return (int) $result;
169
    }
170
171
    /**
172
     * @return array<int|string, mixed>
173
     */
174
    public static function returnArray(mixed $result): array
175
    {
176
        if (!is_array($result)) {
177
            return [];
178
        }
179
        return $result;
180
    }
181
182
    public static function setVar(string $var, mixed $value): void
183
    {
184
        global $_GET, $_POST;
185
        $_GET[$var] = $value;
186
        $_POST[$var] = $value;
187
    }
188
189
    public static function isAjaxRequest(): bool
190
    {
191
        return !empty($_SERVER['HTTP_X_REQUESTED_WITH'])
192
            && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
193
    }
194
195
    /** @param array<string, mixed> $mockVars */
196 1571
    public static function setMockVars(?array $mockVars): void
197
    {
198 1571
        self::$mockVars = $mockVars;
199
    }
200
201 149
    public static function isMocked(): bool
202
    {
203 149
        return self::$mockVars !== null;
204
    }
205
}
206