-
Notifications
You must be signed in to change notification settings - Fork 430
/
filter-input.php
37 lines (30 loc) · 1.75 KB
/
filter-input.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php declare(strict_types=1);
namespace FilterInput;
use function PHPStan\Testing\assertType;
class FilterInput
{
public function invalidTypesOrVarNames($mixed): void
{
assertType('mixed', filter_input(INPUT_GET, $mixed, FILTER_VALIDATE_INT));
assertType('*NEVER*', filter_input(INPUT_GET, 17, FILTER_VALIDATE_INT));
assertType('*NEVER*', filter_input(-1, 'foo', FILTER_VALIDATE_INT));
}
public function supportedSuperGlobals(): void
{
assertType('int|false|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT));
assertType('int|false|null', filter_input(INPUT_POST, 'foo', FILTER_VALIDATE_INT));
assertType('int|false|null', filter_input(INPUT_COOKIE, 'foo', FILTER_VALIDATE_INT));
assertType('int|false|null', filter_input(INPUT_SERVER, 'foo', FILTER_VALIDATE_INT));
assertType('int|false|null', filter_input(INPUT_ENV, 'foo', FILTER_VALIDATE_INT));
}
public function doFoo(string $foo): void
{
assertType('int|false|null', filter_input(INPUT_GET, $foo, FILTER_VALIDATE_INT));
assertType('int|false|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT));
assertType('int|false|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['flags' => FILTER_NULL_ON_FAILURE]));
assertType("'invalid'|int|null", filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['options' => ['default' => 'invalid']]));
assertType('array<int|false>|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY]));
assertType('array<int|null>|false', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY|FILTER_NULL_ON_FAILURE]));
assertType('0|int<17, 19>|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['options' => ['default' => 0, 'min_range' => 17, 'max_range' => 19]]));
}
}