|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpStubs\WordPress\Core\Tests; |
| 6 | + |
| 7 | +/** |
| 8 | + * @phpstan-type Types array{ |
| 9 | + * bool: bool, |
| 10 | + * int: int, |
| 11 | + * float: float, |
| 12 | + * string: string, |
| 13 | + * array: array<mixed>, |
| 14 | + * resource: resource, |
| 15 | + * object: object, |
| 16 | + * numeric-string: numeric-string, |
| 17 | + * null: null, |
| 18 | + * mixed: mixed, |
| 19 | + * true: true, |
| 20 | + * false: false, |
| 21 | + * callable: callable, |
| 22 | + * iterable: iterable<mixed>, |
| 23 | + * array-key: array-key, |
| 24 | + * positive-int: positive-int, |
| 25 | + * negative-int: negative-int, |
| 26 | + * non-positive-int: non-positive-int, |
| 27 | + * non-negative-int: non-negative-int, |
| 28 | + * non-zero-int: non-zero-int, |
| 29 | + * } |
| 30 | + */ |
| 31 | +class Faker |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var Types $types |
| 35 | + * @phpstan-ignore-next-line |
| 36 | + */ |
| 37 | + private static $types; |
| 38 | + |
| 39 | + /** |
| 40 | + * @template T of string |
| 41 | + * @param T $type |
| 42 | + * @return Types[T] |
| 43 | + */ |
| 44 | + public static function fake(string $type): mixed |
| 45 | + { |
| 46 | + return self::$types[$type]; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @template T of string |
| 51 | + * @template K of string |
| 52 | + * @param T $valueType |
| 53 | + * @param K $keyType |
| 54 | + * @return array<Types[K], Types[T]> |
| 55 | + */ |
| 56 | + public static function fakeArray(string $valueType, string $keyType = 'array-key'): mixed |
| 57 | + { |
| 58 | + return [$_GET[$keyType], $_GET[$valueType]]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @template T of non-empty-array<key-of<Types>> |
| 63 | + * @param T $types |
| 64 | + * @return Types[value-of<T>] |
| 65 | + */ |
| 66 | + public static function or(array $types): mixed |
| 67 | + { |
| 68 | + foreach ($types as $type) { |
| 69 | + if ($_GET['thing'] === $type) { |
| 70 | + return self::fake($type); |
| 71 | + } |
| 72 | + } |
| 73 | + return self::fake($types[0]); |
| 74 | + } |
| 75 | +} |
0 commit comments