Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

request   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 40.79%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 187
ccs 31
cts 76
cp 0.4079
rs 9.68
c 0
b 0
f 0
wmc 34

23 Methods

Rating   Name   Duplication   Size   Complexity  
A postArray() 0 3 1
A isRequest() 0 3 1
A postInt() 0 4 1
A returnInt() 0 9 3
A postArrayFatal() 0 3 1
A returnArray() 0 6 2
A postvars() 0 4 1
A isPost() 0 7 2
A isAjaxRequest() 0 4 2
A postStringFatal() 0 3 1
A setVar() 0 5 1
A setMockVars() 0 3 1
A postIntFatal() 0 4 1
A postString() 0 3 1
A getInt() 0 7 2
A indInt() 0 7 2
A getString() 0 3 1
A indString() 0 7 2
A getIntFatal() 0 4 1
A getvars() 0 8 2
A has() 0 3 1
A getVarByMethod() 0 9 3
A getStringFatal() 0 3 1
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 14
    public static function getvars(): array
14
    {
15 14
        if (self::$mockVars !== null) {
16 12
            return self::$mockVars;
17
        }
18
19 2
        global $_GET;
20 2
        return $_GET;
21
    }
22
23
    /**
24
     * @return array<string, mixed>
25
     */
26 7
    public static function postvars(): array
27
    {
28 7
        global $_POST;
29 7
        return $_POST;
30
    }
31
32
    public static function isRequest(): bool
33
    {
34
        return array_key_exists('REQUEST_METHOD', $_SERVER);
35
    }
36
37
    public static function isPost(): bool
38
    {
39
        if (!static::isRequest()) {
40
            return false;
41
        }
42
43
        return $_SERVER['REQUEST_METHOD'] === 'POST';
44
    }
45
46 6
    public static function has(string $key): bool
47
    {
48 6
        return self::getvars()[$key] ?? self::postvars()[$key] ?? false;
49
    }
50
51
    /**
52
     * @param array<int|string, mixed> $method
53
     *
54
     * @return mixed
55
     */
56 12
    public static function getVarByMethod(array $method, string $var, bool $fatal = false)
57
    {
58 12
        if (!@array_key_exists($var, $method)) {
59 6
            if ($fatal === true) {
60 1
                throw new InvalidParamException(sprintf('request parameter "%s" does not exist', $var));
61
            }
62 5
            return false;
63
        }
64 11
        return $method[$var];
65
    }
66
67
    public static function getInt(string $var, int $std = 0): int
68
    {
69
        $int = self::getVarByMethod(self::getvars(), $var);
70
        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

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