|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Tests\Type; |
| 15 | + |
| 16 | +use CodeIgniter\HTTP\CLIRequest; |
| 17 | +use CodeIgniter\HTTP\IncomingRequest; |
| 18 | + |
| 19 | +use function PHPStan\Testing\assertType; |
| 20 | + |
| 21 | +/** |
| 22 | + * @param list<string> $keys |
| 23 | + */ |
| 24 | +function test_incoming_request(IncomingRequest $request, string $key, array $keys, ?string $maybe): void |
| 25 | +{ |
| 26 | + // A null or absent index returns the whole global as a keyed array. |
| 27 | + assertType('array<string, mixed>', $request->getGet()); |
| 28 | + assertType('array<string, mixed>', $request->getPost(null)); |
| 29 | + assertType('array<string, mixed>', $request->getCookie()); |
| 30 | + assertType('array<string, mixed>', $request->getPostGet()); |
| 31 | + assertType('array<string, mixed>', $request->getGetPost()); |
| 32 | + assertType('array<string, mixed>', $request->getRawInputVar()); |
| 33 | + |
| 34 | + // An array of keys returns an array keyed by those names. |
| 35 | + assertType('array<string, mixed>', $request->getGet(['a', 'b'])); |
| 36 | + assertType('array<string, mixed>', $request->getPost($keys)); |
| 37 | + assertType('array<string, mixed>', $request->getRawInputVar(['x'])); |
| 38 | + |
| 39 | + // A string index returns the scalar value, a nested array, or null. |
| 40 | + assertType('array<mixed>|string|null', $request->getGet('q')); |
| 41 | + assertType('array<mixed>|string|null', $request->getPost($key)); |
| 42 | + assertType('array<mixed>|string|null', $request->getCookie('session')); |
| 43 | + assertType('array<mixed>|string|null', $request->getPostGet('id')); |
| 44 | + assertType('array<mixed>|string|null', $request->getGetPost('id')); |
| 45 | + |
| 46 | + // getRawInputVar's string leaf is left as the declared union. |
| 47 | + assertType('array|bool|float|int|object|string|null', $request->getRawInputVar('field')); |
| 48 | + |
| 49 | + // A filter argument re-types the value, so the declared union is left in place. |
| 50 | + assertType('array|bool|float|int|object|string|null', $request->getGet('q', FILTER_VALIDATE_INT)); |
| 51 | + |
| 52 | + // An ambiguous index (string or null) cannot be resolved. |
| 53 | + assertType('array|bool|float|int|object|string|null', $request->getGet($maybe)); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @param list<string> $keys |
| 58 | + */ |
| 59 | +function test_cli_request(CLIRequest $request, string $key, array $keys, ?string $maybe): void |
| 60 | +{ |
| 61 | + // A null or array index yields an empty array. |
| 62 | + assertType('array{}', $request->getGet()); |
| 63 | + assertType('array{}', $request->getPost(null)); |
| 64 | + assertType('array{}', $request->getCookie()); |
| 65 | + assertType('array{}', $request->getGet(['a'])); |
| 66 | + assertType('array{}', $request->getPostGet($keys)); |
| 67 | + |
| 68 | + // A string index always returns null on the CLI. |
| 69 | + assertType('null', $request->getGet('q')); |
| 70 | + assertType('null', $request->getPost($key)); |
| 71 | + assertType('null', $request->getCookie('session')); |
| 72 | + |
| 73 | + // An ambiguous index is left as the declared union. |
| 74 | + assertType('array|null', $request->getGet($maybe)); |
| 75 | +} |
0 commit comments