|
5 | 5 | namespace PhpStubs\WordPress\Core\Tests; |
6 | 6 |
|
7 | 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 | | - * } |
| 8 | + * Class that provides fake types via docBlocks for type testing. |
| 9 | + * |
| 10 | + * @method static bool bool() |
| 11 | + * @method static true true() |
| 12 | + * @method static false false() |
| 13 | + * @method static int int() |
| 14 | + * @method static positive-int positiveInt() |
| 15 | + * @method static negative-int negativeInt() |
| 16 | + * @method static non-positive-int nonPositiveInt() |
| 17 | + * @method static non-negative-int nonNegativeInt() |
| 18 | + * @method static non-zero-int nonZeroInt() |
| 19 | + * @method static float float() |
| 20 | + * @method static string string() |
| 21 | + * @method static non-empty-string nonEmptyString() |
| 22 | + * @method static numeric-string numericString() |
| 23 | + * @method static resource resource() |
| 24 | + * @method static object object() |
| 25 | + * @method static mixed mixed() |
| 26 | + * @method static callable callable() |
| 27 | + * @method static \stdClass stdClass() |
| 28 | + * @method static \WP_Post wpPost() |
| 29 | + * @method static \WP_Term wpTerm() |
| 30 | + * @method static \WP_Comment wpComment() |
| 31 | + * @method static \WP_REST_Request wpRestRequest() |
| 32 | + * @method static \WP_Theme wpTheme() |
| 33 | + * @method static \WP_Translations wpTranslations() |
| 34 | + * @method static \WP_Query wpQuery() |
| 35 | + * @method static \WP_Widget_Factory wpWidgetFactory() |
30 | 36 | */ |
31 | 37 | class Faker |
32 | 38 | { |
33 | 39 | /** |
34 | | - * @var Types $types |
35 | | - * @phpstan-ignore-next-line |
| 40 | + * Fakes `array<Type>`. If `$type` is `null`, fakes `array<mixed>`. |
| 41 | + * |
| 42 | + * @template T |
| 43 | + * @param T $type |
| 44 | + * @return ($type is null ? array<array-key, mixed> : array<array-key, T>) |
36 | 45 | */ |
37 | | - private static $types; |
| 46 | + public static function array($type = null): array |
| 47 | + { |
| 48 | + return [$type]; |
| 49 | + } |
38 | 50 |
|
39 | 51 | /** |
40 | | - * @template T of string |
41 | | - * @param T $type |
42 | | - * @return Types[T] |
| 52 | + * Fakes `array<int, Type>`. If `$type` is `null`, fakes `array<int, mixed>`. |
| 53 | + * |
| 54 | + * @template T |
| 55 | + * @param T|null $type |
| 56 | + * @return ($type is null ? array<int, mixed> : array<int, T>) |
| 57 | + */ |
| 58 | + public static function intArray($type = null): array |
| 59 | + { |
| 60 | + return [$type]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Fakes `array<string, Type>`. If `$type` is `null`, fakes `array<string, mixed>`. |
| 65 | + * |
| 66 | + * @template T |
| 67 | + * @param T|null $type |
| 68 | + * @return ($type is null ? array<string, mixed>: array<string, T>) |
43 | 69 | */ |
44 | | - public static function fake(string $type): mixed |
| 70 | + public static function strArray($type = null): array |
45 | 71 | { |
46 | | - return self::$types[$type]; |
| 72 | + return [self::string() => $type]; |
47 | 73 | } |
48 | 74 |
|
49 | 75 | /** |
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]> |
| 76 | + * Fakes `list<Type>`. If `$type` is `null`, fakes `list<mixed>`. |
| 77 | + * |
| 78 | + * @template T |
| 79 | + * @param T|null $type |
| 80 | + * @return ($type is null ? list<mixed> : list<T>) |
55 | 81 | */ |
56 | | - public static function fakeArray(string $valueType, string $keyType = 'array-key'): mixed |
| 82 | + public static function list($type = null): array |
57 | 83 | { |
58 | | - return [$_GET[$keyType], $_GET[$valueType]]; |
| 84 | + return [$type]; |
59 | 85 | } |
60 | 86 |
|
61 | 87 | /** |
62 | | - * @template T of non-empty-array<key-of<Types>> |
63 | | - * @param T $types |
64 | | - * @return Types[value-of<T>] |
| 88 | + * @template T |
| 89 | + * @param T ...$types |
| 90 | + * @return T |
65 | 91 | */ |
66 | | - public static function or(array $types): mixed |
| 92 | + public static function union(...$types): mixed |
67 | 93 | { |
68 | | - foreach ($types as $type) { |
69 | | - if ($_GET['thing'] === $type) { |
70 | | - return self::fake($type); |
71 | | - } |
72 | | - } |
73 | | - return self::fake($types[0]); |
| 94 | + return $types[0]; |
74 | 95 | } |
75 | 96 | } |
0 commit comments