Passed
Push — master ( 3d0840...13d157 )
by Nico
48:12 queued 22:32
created

request::getStringFatal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
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 3
    public static function getvars(): array
14
    {
15 3
        if (self::$mockVars !== null) {
16 3
            return self::$mockVars;
17
        }
18
19
        global $_GET;
20
        return $_GET;
21
    }
22
23
    /**
24
     * @return array<string, mixed>
25
     */
26 1
    public static function postvars(): array
27
    {
28 1
        global $_POST;
29 1
        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
    public static function has(string $key): bool
47
    {
48
        return self::getvars()[$key] ?? self::postvars()[$key] ?? false;
49
    }
50
51
    /**
52
     * @param array<int|string, mixed> $method
53
     *
54
     * @return mixed
55
     */
56 3
    public static function getVarByMethod(array $method, string $var, bool $fatal = false)
57
    {
58 3
        if (!@array_key_exists($var, $method)) {
59 1
            if ($fatal === true) {
60
                throw new InvalidParamException($var);
61
            }
62 1
            return false;
63
        }
64 3
        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 2
    public static function getIntFatal(string $var): int
77
    {
78 2
        $int = self::getVarByMethod(self::getvars(), $var, true);
79 2
        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 1
    public static function indInt(string $var): int
114
    {
115 1
        $value = self::getVarByMethod(self::postvars(), $var);
116 1
        if ($value) {
117
            return self::returnInt($value);
118
        }
119 1
        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
    public static function getStringFatal(string $var): string
128
    {
129
        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 3
    public static function returnInt($result): int
149
    {
150
        if (
151 3
            !$result
152 3
            || $result < 0
153
        ) {
154
            return 0;
155
        }
156 3
        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 1182
    public static function setMockVars(?array $mockVars): void
190
    {
191 1182
        self::$mockVars = $mockVars;
192
    }
193
}
194