diff --git a/tests/Faker.php b/tests/Faker.php index 42a6ff1..96fd0d6 100644 --- a/tests/Faker.php +++ b/tests/Faker.php @@ -5,71 +5,92 @@ namespace PhpStubs\WordPress\Core\Tests; /** - * @phpstan-type Types array{ - * bool: bool, - * int: int, - * float: float, - * string: string, - * array: array, - * resource: resource, - * object: object, - * numeric-string: numeric-string, - * null: null, - * mixed: mixed, - * true: true, - * false: false, - * callable: callable, - * iterable: iterable, - * array-key: array-key, - * positive-int: positive-int, - * negative-int: negative-int, - * non-positive-int: non-positive-int, - * non-negative-int: non-negative-int, - * non-zero-int: non-zero-int, - * } + * Class that provides fake types via docBlocks for type testing. + * + * @method static bool bool() + * @method static true true() + * @method static false false() + * @method static int int() + * @method static positive-int positiveInt() + * @method static negative-int negativeInt() + * @method static non-positive-int nonPositiveInt() + * @method static non-negative-int nonNegativeInt() + * @method static non-zero-int nonZeroInt() + * @method static float float() + * @method static string string() + * @method static non-empty-string nonEmptyString() + * @method static numeric-string numericString() + * @method static resource resource() + * @method static object object() + * @method static mixed mixed() + * @method static callable callable() + * @method static \stdClass stdClass() + * @method static \WP_Post wpPost() + * @method static \WP_Term wpTerm() + * @method static \WP_Comment wpComment() + * @method static \WP_REST_Request wpRestRequest() + * @method static \WP_Theme wpTheme() + * @method static \WP_Translations wpTranslations() + * @method static \WP_Query wpQuery() + * @method static \WP_Widget_Factory wpWidgetFactory() */ class Faker { /** - * @var Types $types - * @phpstan-ignore-next-line + * Fakes `array`. If `$type` is `null`, fakes `array`. + * + * @template T + * @param T $type + * @return ($type is null ? array : array) */ - private static $types; + public static function array($type = null): array + { + return [$type]; + } /** - * @template T of string - * @param T $type - * @return Types[T] + * Fakes `array`. If `$type` is `null`, fakes `array`. + * + * @template T + * @param T|null $type + * @return ($type is null ? array : array) + */ + public static function intArray($type = null): array + { + return [$type]; + } + + /** + * Fakes `array`. If `$type` is `null`, fakes `array`. + * + * @template T + * @param T|null $type + * @return ($type is null ? array: array) */ - public static function fake(string $type): mixed + public static function strArray($type = null): array { - return self::$types[$type]; + return [self::string() => $type]; } /** - * @template T of string - * @template K of string - * @param T $valueType - * @param K $keyType - * @return array + * Fakes `list`. If `$type` is `null`, fakes `list`. + * + * @template T + * @param T|null $type + * @return ($type is null ? list : list) */ - public static function fakeArray(string $valueType, string $keyType = 'array-key'): mixed + public static function list($type = null): array { - return [$_GET[$keyType], $_GET[$valueType]]; + return [$type]; } /** - * @template T of non-empty-array> - * @param T $types - * @return Types[value-of] + * @template T + * @param T ...$types + * @return T */ - public static function or(array $types): mixed + public static function union(...$types): mixed { - foreach ($types as $type) { - if ($_GET['thing'] === $type) { - return self::fake($type); - } - } - return self::fake($types[0]); + return $types[0]; } } diff --git a/tests/TypeInferenceTest.php b/tests/TypeInferenceTest.php index 92c12f0..26ba090 100644 --- a/tests/TypeInferenceTest.php +++ b/tests/TypeInferenceTest.php @@ -16,6 +16,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bool_from_yn.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/current_time.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/echo_parameter.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/Faker.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/get_approved_comments.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/get_attachment_taxonomies.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/get_bookmark.php'); diff --git a/tests/data/Faker.php b/tests/data/Faker.php new file mode 100644 index 0000000..a89f7df --- /dev/null +++ b/tests/data/Faker.php @@ -0,0 +1,63 @@ +', Faker::positiveInt()); +assertType('int', Faker::negativeInt()); +assertType('int', Faker::nonPositiveInt()); +assertType('int<0, max>', Faker::nonNegativeInt()); +assertType('int|int<1, max>', Faker::nonZeroInt()); +assertType('float', Faker::float()); + +// Strings +assertType('string', Faker::string()); +assertType('numeric-string', Faker::numericString()); +assertType('non-empty-string', Faker::nonEmptyString()); + +// Arrays with default values +assertType('array', Faker::array()); +assertType('array', Faker::intArray()); +assertType('array', Faker::strArray()); +assertType('list', Faker::list()); + +// Arrays with specific values +assertType('array', Faker::array(Faker::int())); +assertType('array', Faker::intArray(Faker::string())); +assertType('array', Faker::strArray(Faker::bool())); +assertType('list', Faker::list()); + +// Unions +assertType('bool', Faker::union(Faker::bool())); +assertType('bool|int', Faker::union(Faker::bool(), Faker::int())); +assertType('bool|int|string', Faker::union(Faker::bool(), Faker::int(), Faker::string())); +assertType('array|bool|int|string', Faker::union(Faker::bool(), Faker::int(), Faker::string(), Faker::intArray(Faker::int()))); +assertType('array', Faker::union(Faker::intArray(Faker::int()), Faker::strArray(Faker::string()))); +assertType('array', Faker::union(Faker::array(Faker::int()), Faker::strArray(Faker::string()))); +assertType('array', Faker::union(Faker::array(), Faker::strArray())); +assertType('array', Faker::union(Faker::array(), Faker::intArray())); +assertType('string|null', Faker::union(Faker::string(), null)); + +// Other +assertType('resource', Faker::resource()); +assertType('object', Faker::object()); +assertType('stdClass', Faker::stdClass()); +assertType('WP_Post', Faker::wpPost()); +assertType('WP_Term', Faker::wpTerm()); +assertType('WP_Comment', Faker::wpComment()); +assertType('WP_REST_Request', Faker::wpRestRequest()); +assertType('WP_Theme', Faker::wpTheme()); +assertType('WP_Translations', Faker::wpTranslations()); +assertType('WP_Query', Faker::wpQuery()); +assertType('WP_Widget_Factory', Faker::wpWidgetFactory()); +assertType('mixed', Faker::mixed()); diff --git a/tests/data/_wp_json_sanity_check.php b/tests/data/_wp_json_sanity_check.php index ec06d94..ceab47f 100644 --- a/tests/data/_wp_json_sanity_check.php +++ b/tests/data/_wp_json_sanity_check.php @@ -4,15 +4,13 @@ namespace PhpStubs\WordPress\Core\Tests; -use stdClass; - use function PHPStan\Testing\assertType; use function _wp_json_sanity_check; assertType('null', _wp_json_sanity_check(null, 1)); -assertType('bool', _wp_json_sanity_check((bool)$_GET['value'], 1)); -assertType('int', _wp_json_sanity_check((int)$_GET['value'], 1)); -assertType('string', _wp_json_sanity_check((string)$_GET['value'], 1)); -assertType('array', _wp_json_sanity_check((array)$_GET['value'], 1)); -assertType('stdClass', _wp_json_sanity_check(new stdClass(), 1)); -assertType('mixed', _wp_json_sanity_check($_GET['value'], 1)); +assertType('bool', _wp_json_sanity_check(Faker::bool(), 1)); +assertType('int', _wp_json_sanity_check(Faker::int(), 1)); +assertType('string', _wp_json_sanity_check(Faker::string(), 1)); +assertType('array', _wp_json_sanity_check(Faker::array(), 1)); +assertType('stdClass', _wp_json_sanity_check(Faker::stdClass(), 1)); +assertType('mixed', _wp_json_sanity_check(Faker::mixed(), 1)); diff --git a/tests/data/absint.php b/tests/data/absint.php index ec11e6d..f549b00 100644 --- a/tests/data/absint.php +++ b/tests/data/absint.php @@ -4,26 +4,15 @@ namespace PhpStubs\WordPress\Core\Tests; -use stdClass; - use function absint; use function PHPStan\Testing\assertType; -/** @var int<1, max> $posInt */ -$posInt = $_GET['posInt']; - -/** @var int<0, max> $nonNegInt */ -$nonNegInt = $_GET['nonNegInt']; - -/** @var resource $resource */ -$resource = $_GET['resource']; - // Returns input for non-negative integers assertType('0', absint(0)); assertType('1', absint(1)); assertType('10', absint(10)); -assertType('int<1, max>', absint($posInt)); -assertType('int<0, max>', absint($nonNegInt)); +assertType('int<1, max>', absint(Faker::positiveInt())); +assertType('int<0, max>', absint(Faker::nonNegativeInt())); // Returns 0 for "empty" input assertType('0', absint(null)); @@ -40,7 +29,7 @@ assertType('1', absint(['key' => 'value'])); // Returns 0 or 1 for booleans -assertType('0|1', absint((bool)$_GET['bool'])); +assertType('0|1', absint(Faker::bool())); // Returns positive integer for strictly negative integer input assertType('int<1, max>', absint(-1)); @@ -48,7 +37,9 @@ // Returns non-negative integer for floats, numeric strings, ressources assertType('int<0, max>', absint(1.0)); +assertType('int<0, max>', absint(Faker::float())); assertType('int<0, max>', absint('-10')); -assertType('int<0, max>', absint($resource)); +assertType('int<0, max>', absint(Faker::numericString())); +assertType('int<0, max>', absint(Faker::resource())); // and any other type that is not a subtype of `$maybeint` -assertType('int<0, max>', absint(new stdClass())); +assertType('int<0, max>', absint(Faker::stdClass())); diff --git a/tests/data/bool_from_yn.php b/tests/data/bool_from_yn.php index 915eb3d..b946cf1 100644 --- a/tests/data/bool_from_yn.php +++ b/tests/data/bool_from_yn.php @@ -10,4 +10,4 @@ assertType('true', bool_from_yn('y')); assertType('false', bool_from_yn('n')); assertType('false', bool_from_yn('thing')); -assertType('bool', bool_from_yn((string)$_GET['string'])); +assertType('bool', bool_from_yn(Faker::string())); diff --git a/tests/data/current_time.php b/tests/data/current_time.php index 275bbdb..e954744 100644 --- a/tests/data/current_time.php +++ b/tests/data/current_time.php @@ -14,6 +14,4 @@ // String types assertType('string', current_time('mysql')); assertType('string', current_time('Hello')); - -// Unknown string -assertType('int|string', current_time((string)$_GET['unknown_string'])); +assertType('int|string', current_time(Faker::string())); diff --git a/tests/data/echo_parameter.php b/tests/data/echo_parameter.php index f351992..881f2fc 100644 --- a/tests/data/echo_parameter.php +++ b/tests/data/echo_parameter.php @@ -83,8 +83,8 @@ assertType('string', wp_register('', '', $value)); assertType('string', wp_title('', $value)); -// Unknown value -$value = isset($_GET['foo']); +// Unknown boolean value +$value = Faker::bool(); assertType('string|null', comment_class('', null, null, $value)); assertType('string|null', edit_term_link('', '', '', null, $value)); assertType('string|null', get_calendar(true, $value)); diff --git a/tests/data/get_attachment_taxonomies.php b/tests/data/get_attachment_taxonomies.php index 453de9c..85e6642 100644 --- a/tests/data/get_attachment_taxonomies.php +++ b/tests/data/get_attachment_taxonomies.php @@ -8,19 +8,19 @@ use function PHPStan\Testing\assertType; // Default -assertType('array', get_attachment_taxonomies((int)$_GET['id'])); -assertType('array', get_attachment_taxonomies((int)$_GET['id'], 'names')); +assertType('array', get_attachment_taxonomies(Faker::int())); +assertType('array', get_attachment_taxonomies(Faker::int(), 'names')); // Objects -assertType('array', get_attachment_taxonomies((int)$_GET['id'], 'objects')); +assertType('array', get_attachment_taxonomies(Faker::int(), 'objects')); // Unexpected -assertType('array', get_attachment_taxonomies((int)$_GET['id'], 'Hello')); +assertType('array', get_attachment_taxonomies(Faker::int(), 'Hello')); // Unknown -assertType('array', get_attachment_taxonomies((int)$_GET['id'], (string)$_GET['string'])); +assertType('array', get_attachment_taxonomies(Faker::int(), Faker::string())); // Unions -assertType('array', get_attachment_taxonomies((int)$_GET['id'], $_GET['foo'] ? 'names' : 'objects')); -assertType('array', get_attachment_taxonomies((int)$_GET['id'], $_GET['foo'] ? (string)$_GET['string'] : 'names')); -assertType('array', get_attachment_taxonomies((int)$_GET['id'], $_GET['foo'] ? (string)$_GET['string'] : 'objects')); +assertType('array', get_attachment_taxonomies(Faker::int(), Faker::bool() ? 'names' : 'objects')); +assertType('array', get_attachment_taxonomies(Faker::int(), Faker::bool() ? Faker::string() : 'names')); +assertType('array', get_attachment_taxonomies(Faker::int(), Faker::bool() ? Faker::string() : 'objects')); diff --git a/tests/data/get_bookmark.php b/tests/data/get_bookmark.php index 8f5ea80..739abad 100644 --- a/tests/data/get_bookmark.php +++ b/tests/data/get_bookmark.php @@ -7,10 +7,9 @@ use function get_bookmark; use function PHPStan\Testing\assertType; -/** @var \stdClass|int $bookmark */ -$bookmark = $_GET['bookmark']; +$stdClassOrInt = Faker::union(Faker::stdClass(), Faker::int()); -assertType('stdClass|null', get_bookmark($bookmark)); -assertType('stdClass|null', get_bookmark($bookmark, 'OBJECT')); -assertType('array|null', get_bookmark($bookmark, 'ARRAY_A')); -assertType('array|null', get_bookmark($bookmark, 'ARRAY_N')); +assertType('stdClass|null', get_bookmark($stdClassOrInt)); +assertType('stdClass|null', get_bookmark($stdClassOrInt, 'OBJECT')); +assertType('array|null', get_bookmark($stdClassOrInt, 'ARRAY_A')); +assertType('array|null', get_bookmark($stdClassOrInt, 'ARRAY_N')); diff --git a/tests/data/get_categories.php b/tests/data/get_categories.php index e225ce0..e12b0a8 100644 --- a/tests/data/get_categories.php +++ b/tests/data/get_categories.php @@ -34,4 +34,4 @@ // Unknown fields value assertType('array', get_categories(['fields' => 'foo'])); -assertType('array', get_categories(['fields' => (string)$_GET['fields']])); +assertType('array', get_categories(['fields' => Faker::string()])); diff --git a/tests/data/get_category.php b/tests/data/get_category.php index fe43d14..6005b6c 100644 --- a/tests/data/get_category.php +++ b/tests/data/get_category.php @@ -7,18 +7,13 @@ use function get_category; use function PHPStan\Testing\assertType; -/** @var object $category */ -$category = $_GET['category']; +assertType('WP_Term', get_category(Faker::object())); +assertType('WP_Term', get_category(Faker::object(), 'OBJECT')); +assertType('array', get_category(Faker::object(), 'ARRAY_A')); +assertType('array', get_category(Faker::object(), 'ARRAY_N')); -assertType('WP_Term', get_category($category)); -assertType('WP_Term', get_category($category, 'OBJECT')); -assertType('array', get_category($category, 'ARRAY_A')); -assertType('array', get_category($category, 'ARRAY_N')); - -/** @var int|object $category */ -$category = $_GET['category']; - -assertType('WP_Error|WP_Term|null', get_category($category)); -assertType('WP_Error|WP_Term|null', get_category($category, 'OBJECT')); -assertType('array|WP_Error|null', get_category($category, 'ARRAY_A')); -assertType('array|WP_Error|null', get_category($category, 'ARRAY_N')); +$intOrObject = Faker::union(Faker::int(), Faker::object()); +assertType('WP_Error|WP_Term|null', get_category($intOrObject)); +assertType('WP_Error|WP_Term|null', get_category($intOrObject, 'OBJECT')); +assertType('array|WP_Error|null', get_category($intOrObject, 'ARRAY_A')); +assertType('array|WP_Error|null', get_category($intOrObject, 'ARRAY_N')); diff --git a/tests/data/get_category_by_path.php b/tests/data/get_category_by_path.php index d109010..9366173 100644 --- a/tests/data/get_category_by_path.php +++ b/tests/data/get_category_by_path.php @@ -7,7 +7,7 @@ use function get_category_by_path; use function PHPStan\Testing\assertType; -assertType('WP_Error|WP_Term|null', get_category_by_path('', (bool)$_GET['full_match'])); -assertType('WP_Error|WP_Term|null', get_category_by_path('', (bool)$_GET['full_match'], 'OBJECT')); -assertType('array|WP_Error|null', get_category_by_path('', (bool)$_GET['full_match'], 'ARRAY_A')); -assertType('array|WP_Error|null', get_category_by_path('', (bool)$_GET['full_match'], 'ARRAY_N')); +assertType('WP_Error|WP_Term|null', get_category_by_path('', Faker::bool())); +assertType('WP_Error|WP_Term|null', get_category_by_path('', Faker::bool(), 'OBJECT')); +assertType('array|WP_Error|null', get_category_by_path('', Faker::bool(), 'ARRAY_A')); +assertType('array|WP_Error|null', get_category_by_path('', Faker::bool(), 'ARRAY_N')); diff --git a/tests/data/get_comment.php b/tests/data/get_comment.php index c986f11..a4942b9 100644 --- a/tests/data/get_comment.php +++ b/tests/data/get_comment.php @@ -7,24 +7,25 @@ use function get_comment; use function PHPStan\Testing\assertType; -/** @var \WP_Comment|int|string|null $comment */ -$comment = $_GET['comment']; +$commentIntStringNull = Faker::union( + Faker::wpComment(), + Faker::int(), + Faker::string(), + null +); // Default output assertType('WP_Comment|null', get_comment()); -assertType('WP_Comment|null', get_comment($comment)); -assertType('WP_Comment|null', get_comment($comment, 'OBJECT')); +assertType('WP_Comment|null', get_comment($commentIntStringNull)); +assertType('WP_Comment|null', get_comment($commentIntStringNull, 'OBJECT')); // Associative array output -assertType('array|null', get_comment($comment, 'ARRAY_A')); +assertType('array|null', get_comment($commentIntStringNull, 'ARRAY_A')); // Numeric array output -assertType('array|null', get_comment($comment, 'ARRAY_N')); +assertType('array|null', get_comment($commentIntStringNull, 'ARRAY_N')); -/** @var \WP_Comment $comment */ -$comment = $_GET['comment']; - -assertType('WP_Comment', get_comment($comment)); -assertType('WP_Comment', get_comment($comment, 'OBJECT')); -assertType('array', get_comment($comment, 'ARRAY_A')); -assertType('array', get_comment($comment, 'ARRAY_N')); +assertType('WP_Comment', get_comment(Faker::wpComment())); +assertType('WP_Comment', get_comment(Faker::wpComment(), 'OBJECT')); +assertType('array', get_comment(Faker::wpComment(), 'ARRAY_A')); +assertType('array', get_comment(Faker::wpComment(), 'ARRAY_N')); diff --git a/tests/data/get_object_taxonomies.php b/tests/data/get_object_taxonomies.php index 3d99206..829b432 100644 --- a/tests/data/get_object_taxonomies.php +++ b/tests/data/get_object_taxonomies.php @@ -18,9 +18,9 @@ assertType('array', get_object_taxonomies('post', 'Hello')); // Unknown string -assertType('array', get_object_taxonomies('post', (string)$_GET['unknown_string'])); +assertType('array', get_object_taxonomies('post', Faker::string())); // Unions -assertType('array', get_object_taxonomies('post', $_GET['foo'] ? 'names' : 'objects')); -assertType('array', get_object_taxonomies('post', $_GET['foo'] ? (string)$_GET['string'] : 'names')); -assertType('array', get_object_taxonomies('post', $_GET['foo'] ? (string)$_GET['string'] : 'objects')); +assertType('array', get_object_taxonomies('post', Faker::bool() ? 'names' : 'objects')); +assertType('array', get_object_taxonomies('post', Faker::bool() ? Faker::string() : 'names')); +assertType('array', get_object_taxonomies('post', Faker::bool() ? Faker::string() : 'objects')); diff --git a/tests/data/get_page_by_path.php b/tests/data/get_page_by_path.php index 44a7c58..23aceda 100644 --- a/tests/data/get_page_by_path.php +++ b/tests/data/get_page_by_path.php @@ -18,7 +18,7 @@ assertType('array|null', get_page_by_path('page/path', ARRAY_N)); // Unknown output -assertType('array|WP_Post|null', get_page_by_path('page/path', (string)$_GET['unknown_string'])); +assertType('array|WP_Post|null', get_page_by_path('page/path', Faker::string())); // Unexpected output assertType('WP_Post|null', get_page_by_path('page/path', 'Hello')); diff --git a/tests/data/get_permalink.php b/tests/data/get_permalink.php index 42bb70e..a2d5fb7 100644 --- a/tests/data/get_permalink.php +++ b/tests/data/get_permalink.php @@ -9,23 +9,20 @@ use function get_the_permalink; use function PHPStan\Testing\assertType; -/** @var \WP_Post $post */ -$post = $_GET['post']; - // get_permalink() assertType('string|false', get_permalink()); assertType('string|false', get_permalink(1)); -assertType('string|false', get_permalink($_GET['foo'])); -assertType('string', get_permalink($post)); +assertType('string|false', get_permalink(Faker::int())); +assertType('string', get_permalink(Faker::wpPost())); // get_the_permalink() assertType('string|false', get_the_permalink()); assertType('string|false', get_the_permalink(1)); -assertType('string|false', get_the_permalink($_GET['foo'])); -assertType('string', get_the_permalink($post)); +assertType('string|false', get_the_permalink(Faker::int())); +assertType('string', get_the_permalink(Faker::wpPost())); // get_post_permalink() assertType('string|false', get_post_permalink()); assertType('string|false', get_post_permalink(1)); -assertType('string|false', get_post_permalink($_GET['foo'])); -assertType('string', get_post_permalink($post)); +assertType('string|false', get_post_permalink(Faker::int())); +assertType('string', get_post_permalink(Faker::wpPost())); diff --git a/tests/data/get_post.php b/tests/data/get_post.php index 3bedfb0..2a11040 100644 --- a/tests/data/get_post.php +++ b/tests/data/get_post.php @@ -7,24 +7,20 @@ use function get_post; use function PHPStan\Testing\assertType; -/** @var \WP_Post|int|null $post */ -$post = $_GET['post']; +$postIntNull = Faker::union(Faker::wpPost(), Faker::int(), null); // Default output assertType('WP_Post|null', get_post()); -assertType('WP_Post|null', get_post($post)); -assertType('WP_Post|null', get_post($post, 'OBJECT')); +assertType('WP_Post|null', get_post($postIntNull)); +assertType('WP_Post|null', get_post($postIntNull, 'OBJECT')); // Associative array output -assertType('array|null', get_post($post, 'ARRAY_A')); +assertType('array|null', get_post($postIntNull, 'ARRAY_A')); // Numeric array output -assertType('array|null', get_post($post, 'ARRAY_N')); +assertType('array|null', get_post($postIntNull, 'ARRAY_N')); -/** @var \WP_Post $post */ -$post = $_GET['post']; - -assertType('WP_Post', get_post($post)); -assertType('WP_Post', get_post($post, 'OBJECT')); -assertType('array', get_post($post, 'ARRAY_A')); -assertType('array', get_post($post, 'ARRAY_N')); +assertType('WP_Post', get_post(Faker::wpPost())); +assertType('WP_Post', get_post(Faker::wpPost(), 'OBJECT')); +assertType('array', get_post(Faker::wpPost(), 'ARRAY_A')); +assertType('array', get_post(Faker::wpPost(), 'ARRAY_N')); diff --git a/tests/data/get_post_stati.php b/tests/data/get_post_stati.php index 65baed4..ea7a4e7 100644 --- a/tests/data/get_post_stati.php +++ b/tests/data/get_post_stati.php @@ -19,9 +19,9 @@ assertType('array', get_post_stati([], 'Hello')); // Unknown -assertType('array', get_post_stati([], (string)$_GET['string'])); +assertType('array', get_post_stati([], Faker::string())); // Unions -assertType('array', get_post_stati([], $_GET['foo'] ? 'names' : 'objects')); -assertType('array', get_post_stati([], $_GET['foo'] ? (string)$_GET['string'] : 'names')); -assertType('array', get_post_stati([], $_GET['foo'] ? (string)$_GET['string'] : 'objects')); +assertType('array', get_post_stati([], Faker::bool() ? 'names' : 'objects')); +assertType('array', get_post_stati([], Faker::bool() ? Faker::string() : 'names')); +assertType('array', get_post_stati([], Faker::bool() ? Faker::string() : 'objects')); diff --git a/tests/data/get_post_types.php b/tests/data/get_post_types.php index e57ae32..7fc9d0a 100644 --- a/tests/data/get_post_types.php +++ b/tests/data/get_post_types.php @@ -15,7 +15,7 @@ assertType('array', get_post_types([], 'objects')); // Unknown string -assertType('array', get_post_types([], (string)$_GET['unknown_string'])); +assertType('array', get_post_types([], Faker::string())); // Unexpected output assertType('array', get_post_types([], 'Hello')); diff --git a/tests/data/get_posts.php b/tests/data/get_posts.php index 7cbb151..786272e 100644 --- a/tests/data/get_posts.php +++ b/tests/data/get_posts.php @@ -15,53 +15,53 @@ assertType('array', get_posts(['fields' => 'Hello'])); // Nonconstant array -assertType('array', get_posts((array)$_GET['array'])); +assertType('array', get_posts(Faker::array())); // Unions -$union = $_GET['foo'] ? ['key' => 'value'] : ['some' => 'thing']; +$union = Faker::bool() ? ['key' => 'value'] : ['some' => 'thing']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'ids']; +$union = Faker::bool() ? ['key' => 'value'] : ['fields' => 'ids']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => '']; +$union = Faker::bool() ? ['key' => 'value'] : ['fields' => '']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'id=>parent']; +$union = Faker::bool() ? ['key' => 'value'] : ['fields' => 'id=>parent']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'ids']; +$union = Faker::bool() ? ['fields' => ''] : ['fields' => 'ids']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'id=>parent']; +$union = Faker::bool() ? ['fields' => ''] : ['fields' => 'id=>parent']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? ['fields' => 'ids'] : ['fields' => 'id=>parent']; +$union = Faker::bool() ? ['fields' => 'ids'] : ['fields' => 'id=>parent']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => '']; +$union = Faker::bool() ? Faker::array() : ['fields' => '']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'ids']; +$union = Faker::bool() ? Faker::array() : ['fields' => 'ids']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'id=>parent']; +$union = Faker::bool() ? Faker::array() : ['fields' => 'id=>parent']; assertType('array', get_posts($union)); -$union = $_GET['foo'] ? (string)$_GET['string'] : ''; +$union = Faker::bool() ? Faker::string() : ''; assertType('array', get_posts(['fields' => $union])); -$union = $_GET['foo'] ? (string)$_GET['string'] : 'ids'; +$union = Faker::bool() ? Faker::string() : 'ids'; assertType('array', get_posts(['fields' => $union])); -$union = $_GET['foo'] ? (string)$_GET['string'] : 'id=>parent'; +$union = Faker::bool() ? Faker::string() : 'id=>parent'; assertType('array', get_posts(['fields' => $union])); -$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; +$union = Faker::bool() ? Faker::string() : 'fields'; assertType('array', get_posts([$union => ''])); -$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; +$union = Faker::bool() ? Faker::string() : 'fields'; assertType('array', get_posts([$union => 'ids'])); -$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; +$union = Faker::bool() ? Faker::string() : 'fields'; assertType('array', get_posts([$union => 'id=>parent'])); diff --git a/tests/data/get_sites.php b/tests/data/get_sites.php index a4a809b..afb5903 100644 --- a/tests/data/get_sites.php +++ b/tests/data/get_sites.php @@ -11,10 +11,8 @@ assertType('array', get_sites()); assertType('array', get_sites([])); -// Non constant array parameter -/** @var array $value */ -$value = $_GET['foo']; -assertType('array|int', get_sites($value)); +// Non constant array parameter. +assertType('array|int', get_sites(Faker::array())); // Array parameter with explicit fields value and default count value. assertType('array', get_sites(['fields' => 'ids'])); diff --git a/tests/data/get_tags.php b/tests/data/get_tags.php index 85a74dd..b1fd5e4 100644 --- a/tests/data/get_tags.php +++ b/tests/data/get_tags.php @@ -34,4 +34,4 @@ assertType('array|WP_Error', get_tags(['fields' => 'foo'])); // Unknown fields value -assertType('array|numeric-string|WP_Error', get_tags(['fields' => (string)$_GET['fields']])); +assertType('array|numeric-string|WP_Error', get_tags(['fields' => Faker::string()])); diff --git a/tests/data/get_taxonomies.php b/tests/data/get_taxonomies.php index b97e73e..5b94a64 100644 --- a/tests/data/get_taxonomies.php +++ b/tests/data/get_taxonomies.php @@ -18,4 +18,4 @@ assertType('array', get_taxonomies([], 'Hello')); // Unknown string -assertType('array', get_taxonomies([], (string)$_GET['unknown_string'])); +assertType('array', get_taxonomies([], Faker::string())); diff --git a/tests/data/get_taxonomies_for_attachments.php b/tests/data/get_taxonomies_for_attachments.php index 311c5b2..9e53455 100644 --- a/tests/data/get_taxonomies_for_attachments.php +++ b/tests/data/get_taxonomies_for_attachments.php @@ -18,9 +18,9 @@ assertType('array', get_taxonomies_for_attachments('Hello')); // Unknown -assertType('array', get_taxonomies_for_attachments((string)$_GET['string'])); +assertType('array', get_taxonomies_for_attachments(Faker::string())); // Unions -assertType('array', get_taxonomies_for_attachments($_GET['foo'] ? 'names' : 'objects')); -assertType('array', get_taxonomies_for_attachments($_GET['foo'] ? (string)$_GET['string'] : 'names')); -assertType('array', get_taxonomies_for_attachments($_GET['foo'] ? (string)$_GET['string'] : 'objects')); +assertType('array', get_taxonomies_for_attachments(Faker::bool() ? 'names' : 'objects')); +assertType('array', get_taxonomies_for_attachments(Faker::bool() ? Faker::string() : 'names')); +assertType('array', get_taxonomies_for_attachments(Faker::bool() ? Faker::string() : 'objects')); diff --git a/tests/data/get_terms.php b/tests/data/get_terms.php index dd83fb2..4f3e128 100644 --- a/tests/data/get_terms.php +++ b/tests/data/get_terms.php @@ -34,4 +34,4 @@ assertType('array|WP_Error', get_terms(['fields' => 'foo'])); // Unknown fields value -assertType('array|numeric-string|WP_Error', get_terms(['fields' => (string)$_GET['fields']])); +assertType('array|numeric-string|WP_Error', get_terms(['fields' => Faker::string()])); diff --git a/tests/data/has_filter.php b/tests/data/has_filter.php index ddf8b39..d591891 100644 --- a/tests/data/has_filter.php +++ b/tests/data/has_filter.php @@ -21,7 +21,6 @@ assertType('int|false', has_action('', 'intval')); // Maybe false callback -/** @var callable|string|array|false $callback */ -$callback = $_GET['callback']; +$callback = Faker::union(Faker::callable(), Faker::string(), Faker::array(), Faker::false()); assertType('bool|int', has_filter('', $callback)); assertType('bool|int', has_action('', $callback)); diff --git a/tests/data/is_wp_error.php b/tests/data/is_wp_error.php index 39757f8..7f337a2 100644 --- a/tests/data/is_wp_error.php +++ b/tests/data/is_wp_error.php @@ -9,9 +9,11 @@ use function is_wp_error; use function PHPStan\Testing\assertType; -assertType('false', is_wp_error((string)$_GET['thing'])); +assertType('false', is_wp_error(Faker::string())); assertType('true', is_wp_error(new WP_Error())); -assertType('bool', is_wp_error($_GET['thing'])); -if (is_wp_error($_GET['thing'])) { - assertType('WP_Error', $_GET['thing']); +assertType('bool', is_wp_error(Faker::mixed())); + +$mixed = Faker::mixed(); +if (is_wp_error($mixed)) { + assertType('WP_Error', $mixed); } diff --git a/tests/data/mysql2date.php b/tests/data/mysql2date.php index da3281b..1f8d409 100644 --- a/tests/data/mysql2date.php +++ b/tests/data/mysql2date.php @@ -15,4 +15,4 @@ assertType('string|false', mysql2date('Y-m-d H:i:s', $time)); // Unknown string -assertType('int|string|false', mysql2date((string)$_GET['unknown_string'], $time)); +assertType('int|string|false', mysql2date(Faker::string(), $time)); diff --git a/tests/data/paginate_links.php b/tests/data/paginate_links.php index 50c1153..d30b058 100644 --- a/tests/data/paginate_links.php +++ b/tests/data/paginate_links.php @@ -15,11 +15,8 @@ use function paginate_links; use function PHPStan\Testing\assertType; -/** @var negative-int $negInt */ -$negInt = $_GET['negInt']; - // Returns void -assertType('null', paginate_links(['total' => $negInt, 'key' => 'value'])); +assertType('null', paginate_links(['total' => Faker::negativeInt(), 'key' => 'value'])); assertType('null', paginate_links(['total' => 0, 'key' => 'value'])); assertType('null', paginate_links(['total' => 1, 'key' => 'value'])); diff --git a/tests/data/stripslashes.php b/tests/data/stripslashes.php index 83da2a5..3d3e8a7 100644 --- a/tests/data/stripslashes.php +++ b/tests/data/stripslashes.php @@ -9,19 +9,19 @@ use function stripslashes_from_strings_only; assertType('null', stripslashes_deep(null)); -assertType('bool', stripslashes_deep(Faker::fake('bool'))); -assertType('int', stripslashes_deep(Faker::fake('int'))); -assertType('float', stripslashes_deep(Faker::fake('float'))); -assertType('string', stripslashes_deep(Faker::fake('string'))); -assertType('array', stripslashes_deep(Faker::fake('array'))); -assertType('resource', stripslashes_deep(Faker::fake('resource'))); -assertType('object', stripslashes_deep(Faker::fake('object'))); +assertType('bool', stripslashes_deep(Faker::bool())); +assertType('int', stripslashes_deep(Faker::int())); +assertType('float', stripslashes_deep(Faker::float())); +assertType('string', stripslashes_deep(Faker::string())); +assertType('array', stripslashes_deep(Faker::array())); +assertType('resource', stripslashes_deep(Faker::resource())); +assertType('object', stripslashes_deep(Faker::object())); assertType('null', stripslashes_from_strings_only(null)); -assertType('bool', stripslashes_from_strings_only(Faker::fake('bool'))); -assertType('int', stripslashes_from_strings_only(Faker::fake('int'))); -assertType('float', stripslashes_from_strings_only(Faker::fake('float'))); -assertType('string', stripslashes_from_strings_only(Faker::fake('string'))); -assertType('array', stripslashes_from_strings_only(Faker::fake('array'))); -assertType('resource', stripslashes_from_strings_only(Faker::fake('resource'))); -assertType('object', stripslashes_from_strings_only(Faker::fake('object'))); +assertType('bool', stripslashes_from_strings_only(Faker::bool())); +assertType('int', stripslashes_from_strings_only(Faker::int())); +assertType('float', stripslashes_from_strings_only(Faker::float())); +assertType('string', stripslashes_from_strings_only(Faker::string())); +assertType('array', stripslashes_from_strings_only(Faker::array())); +assertType('resource', stripslashes_from_strings_only(Faker::resource())); +assertType('object', stripslashes_from_strings_only(Faker::object())); diff --git a/tests/data/validate_file.php b/tests/data/validate_file.php index c2cf4df..e979802 100644 --- a/tests/data/validate_file.php +++ b/tests/data/validate_file.php @@ -7,12 +7,11 @@ use function validate_file; use function PHPStan\Testing\assertType; -/** @var array $allowedFiles */ -$allowedFiles = $_GET['allowedFiles']; +$allowedFiles = Faker::array(Faker::string()); -assertType('0|1|2', validate_file((string)$_GET['file'])); -assertType('0|1|2', validate_file((string)$_GET['file'], [])); -assertType('0|1|2|3', validate_file((string)$_GET['file'], $allowedFiles)); +assertType('0|1|2', validate_file(Faker::string())); +assertType('0|1|2', validate_file(Faker::string(), [])); +assertType('0|1|2|3', validate_file(Faker::string(), $allowedFiles)); assertType('0', validate_file('')); assertType('0', validate_file('', [])); diff --git a/tests/data/wp_die.php b/tests/data/wp_die.php index fa5a1f9..09d7172 100644 --- a/tests/data/wp_die.php +++ b/tests/data/wp_die.php @@ -17,8 +17,8 @@ assertType('null', wp_die('', '', ['exit' => false])); // unknonwn -assertType('null', wp_die('', '', ['exit' => (bool)$_GET['exit']])); -assertType('null', wp_die('', '', (array)$_GET['args'])); +assertType('null', wp_die('', '', ['exit' => Faker::bool()])); +assertType('null', wp_die('', '', Faker::array())); // non-array $args parameter ($args not string type per @phpstan-param) -assertType('never', wp_die('', '', (int)$_GET['args'])); +assertType('never', wp_die('', '', Faker::int())); diff --git a/tests/data/wp_dropdown_languages.php b/tests/data/wp_dropdown_languages.php index 98e4584..41d6517 100644 --- a/tests/data/wp_dropdown_languages.php +++ b/tests/data/wp_dropdown_languages.php @@ -18,8 +18,7 @@ /** @var ''|null $emptyStringOrNull */ $emptyStringOrNull = $_GET['emptyStringOrNull']; -/** @var string|null $stringOrNull */ -$stringOrNull = $_GET['stringOrNull']; +$stringOrNull = Faker::union(Faker::string(), null); // Default value assertType('string', wp_dropdown_languages()); diff --git a/tests/data/wp_error_parameter.php b/tests/data/wp_error_parameter.php index 4d417d0..49ee634 100644 --- a/tests/data/wp_error_parameter.php +++ b/tests/data/wp_error_parameter.php @@ -25,7 +25,7 @@ assertType('int<0, max>', wp_insert_link([])); assertType('int<0, max>', wp_insert_link([], false)); assertType('int<1, max>|WP_Error', wp_insert_link([], true)); -assertType('int<0, max>|WP_Error', wp_insert_link([], $_GET['wp_error'])); +assertType('int<0, max>|WP_Error', wp_insert_link([], Faker::bool())); /* * wp_insert_category() @@ -33,7 +33,7 @@ assertType('int<0, max>', wp_insert_category([])); assertType('int<0, max>', wp_insert_category([], false)); assertType('int<1, max>|WP_Error', wp_insert_category([], true)); -assertType('int<0, max>|WP_Error', wp_insert_category([], $_GET['wp_error'])); +assertType('int<0, max>|WP_Error', wp_insert_category([], Faker::bool())); /* * wp_set_comment_status() @@ -41,7 +41,7 @@ assertType('bool', wp_set_comment_status(1, 'spam')); assertType('bool', wp_set_comment_status(1, 'spam', false)); assertType('WP_Error|true', wp_set_comment_status(1, 'spam', true)); -assertType('bool|WP_Error', wp_set_comment_status(1, 'spam', $_GET['wp_error'])); +assertType('bool|WP_Error', wp_set_comment_status(1, 'spam', Faker::bool())); /* * wp_update_comment() @@ -49,7 +49,7 @@ assertType('0|1|false', wp_update_comment([])); assertType('0|1|false', wp_update_comment([], false)); assertType('0|1|WP_Error', wp_update_comment([], true)); -assertType('0|1|WP_Error|false', wp_update_comment([], $_GET['wp_error'])); +assertType('0|1|WP_Error|false', wp_update_comment([], Faker::bool())); /* * wp_schedule_single_event() @@ -58,7 +58,7 @@ assertType('bool', wp_schedule_single_event(1, 'hook', [])); assertType('bool', wp_schedule_single_event(1, 'hook', [], false)); assertType('WP_Error|true', wp_schedule_single_event(1, 'hook', [], true)); -assertType('bool|WP_Error', wp_schedule_single_event(1, 'hook', [], $_GET['wp_error'])); +assertType('bool|WP_Error', wp_schedule_single_event(1, 'hook', [], Faker::bool())); /* * wp_schedule_event() @@ -67,7 +67,7 @@ assertType('bool', wp_schedule_event(1, 'daily', 'hook', [])); assertType('bool', wp_schedule_event(1, 'daily', 'hook', [], false)); assertType('WP_Error|true', wp_schedule_event(1, 'daily', 'hook', [], true)); -assertType('bool|WP_Error', wp_schedule_event(1, 'daily', 'hook', [], $_GET['wp_error'])); +assertType('bool|WP_Error', wp_schedule_event(1, 'daily', 'hook', [], Faker::bool())); /** * wp_reschedule_event() @@ -76,7 +76,7 @@ assertType('bool', wp_reschedule_event(1, 'daily', 'hook', [])); assertType('bool', wp_reschedule_event(1, 'daily', 'hook', [], false)); assertType('WP_Error|true', wp_reschedule_event(1, 'daily', 'hook', [], true)); -assertType('bool|WP_Error', wp_reschedule_event(1, 'daily', 'hook', [], $_GET['wp_error'])); +assertType('bool|WP_Error', wp_reschedule_event(1, 'daily', 'hook', [], Faker::bool())); /* * wp_unschedule_event() @@ -85,7 +85,7 @@ assertType('bool', wp_unschedule_event(1, 'hook', [])); assertType('bool', wp_unschedule_event(1, 'hook', [], false)); assertType('WP_Error|true', wp_unschedule_event(1, 'hook', [], true)); -assertType('bool|WP_Error', wp_unschedule_event(1, 'hook', [], $_GET['wp_error'])); +assertType('bool|WP_Error', wp_unschedule_event(1, 'hook', [], Faker::bool())); /* * wp_clear_scheduled_hook() @@ -94,7 +94,7 @@ assertType('int<0, max>|false', wp_clear_scheduled_hook('hook', [])); assertType('int<0, max>|false', wp_clear_scheduled_hook('hook', [], false)); assertType('int<0, max>|WP_Error', wp_clear_scheduled_hook('hook', [], true)); -assertType('int<0, max>|WP_Error|false', wp_clear_scheduled_hook('hook', [], $_GET['wp_error'])); +assertType('int<0, max>|WP_Error|false', wp_clear_scheduled_hook('hook', [], Faker::bool())); /* * wp_unschedule_hook() @@ -102,7 +102,7 @@ assertType('int<0, max>|false', wp_unschedule_hook('hook')); assertType('int<0, max>|false', wp_unschedule_hook('hook', false)); assertType('int<0, max>|WP_Error', wp_unschedule_hook('hook', true)); -assertType('int<0, max>|WP_Error|false', wp_unschedule_hook('hook', $_GET['wp_error'])); +assertType('int<0, max>|WP_Error|false', wp_unschedule_hook('hook', Faker::bool())); /* * wp_insert_post() @@ -114,9 +114,9 @@ assertType('int<1, max>|WP_Error', wp_insert_post([], true)); assertType('int<1, max>|WP_Error', wp_insert_post([], true, true)); assertType('int<1, max>|WP_Error', wp_insert_post([], true, false)); -assertType('int<0, max>|WP_Error', wp_insert_post([], $_GET['wp_error'])); -assertType('int<0, max>|WP_Error', wp_insert_post([], $_GET['wp_error'], true)); -assertType('int<0, max>|WP_Error', wp_insert_post([], $_GET['wp_error'], false)); +assertType('int<0, max>|WP_Error', wp_insert_post([], Faker::bool())); +assertType('int<0, max>|WP_Error', wp_insert_post([], Faker::bool(), true)); +assertType('int<0, max>|WP_Error', wp_insert_post([], Faker::bool(), false)); /* * wp_update_post() @@ -128,9 +128,9 @@ assertType('int<1, max>|WP_Error', wp_update_post([], true)); assertType('int<1, max>|WP_Error', wp_update_post([], true, true)); assertType('int<1, max>|WP_Error', wp_update_post([], true, false)); -assertType('int<0, max>|WP_Error', wp_update_post([], $_GET['wp_error'])); -assertType('int<0, max>|WP_Error', wp_update_post([], $_GET['wp_error'], true)); -assertType('int<0, max>|WP_Error', wp_update_post([], $_GET['wp_error'], false)); +assertType('int<0, max>|WP_Error', wp_update_post([], Faker::bool())); +assertType('int<0, max>|WP_Error', wp_update_post([], Faker::bool(), true)); +assertType('int<0, max>|WP_Error', wp_update_post([], Faker::bool(), false)); /* * wp_insert_attachment() @@ -145,6 +145,6 @@ assertType('int<1, max>|WP_Error', wp_insert_attachment([], false, 0, true)); assertType('int<1, max>|WP_Error', wp_insert_attachment([], false, 0, true, true)); assertType('int<1, max>|WP_Error', wp_insert_attachment([], false, 0, true, false)); -assertType('int<0, max>|WP_Error', wp_insert_attachment([], true, 1, $_GET['wp_error'])); -assertType('int<0, max>|WP_Error', wp_insert_attachment([], true, 1, $_GET['wp_error'], true)); -assertType('int<0, max>|WP_Error', wp_insert_attachment([], true, 1, $_GET['wp_error'], false)); +assertType('int<0, max>|WP_Error', wp_insert_attachment([], true, 1, Faker::bool())); +assertType('int<0, max>|WP_Error', wp_insert_attachment([], true, 1, Faker::bool(), true)); +assertType('int<0, max>|WP_Error', wp_insert_attachment([], true, 1, Faker::bool(), false)); diff --git a/tests/data/wp_generate_tag_cloud.php b/tests/data/wp_generate_tag_cloud.php index 9f52eff..029565f 100644 --- a/tests/data/wp_generate_tag_cloud.php +++ b/tests/data/wp_generate_tag_cloud.php @@ -7,8 +7,7 @@ use function wp_generate_tag_cloud; use function PHPStan\Testing\assertType; -/** @var array<\WP_Term> $args */ -$tags = $_GET['tags']; +$tags = Faker::array(Faker::wpTerm()); // Default $args['format] value. assertType('string', wp_generate_tag_cloud($tags)); @@ -25,4 +24,4 @@ assertType('string', wp_generate_tag_cloud($tags, ['format' => 'unexpected', 'key' => 'value'])); // Unknown $args['format] value -assertType('array|string', wp_generate_tag_cloud($tags, ['format' => (string)$_GET['format'], 'key' => 'value'])); +assertType('array|string', wp_generate_tag_cloud($tags, ['format' => Faker::string(), 'key' => 'value'])); diff --git a/tests/data/wp_get_archives.php b/tests/data/wp_get_archives.php index 6760ee7..d647dde 100644 --- a/tests/data/wp_get_archives.php +++ b/tests/data/wp_get_archives.php @@ -27,5 +27,5 @@ assertType('string', wp_get_archives(['echo' => 0, 'key' => 'value'])); // Unknown value -assertType('string|null', wp_get_archives(['echo' => (bool)$_GET['echo'], 'key' => 'value'])); -assertType('string|null', wp_get_archives(['echo' => (int)$_GET['echo'], 'key' => 'value'])); +assertType('string|null', wp_get_archives(['echo' => Faker::bool(), 'key' => 'value'])); +assertType('string|null', wp_get_archives(['echo' => Faker::int(), 'key' => 'value'])); diff --git a/tests/data/wp_get_object_terms.php b/tests/data/wp_get_object_terms.php index 990fb9a..532d327 100644 --- a/tests/data/wp_get_object_terms.php +++ b/tests/data/wp_get_object_terms.php @@ -42,4 +42,4 @@ assertType('numeric-string|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'count'])); // Unknown fields value -assertType('array|numeric-string|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => (string)$_GET['fields']])); +assertType('array|numeric-string|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => Faker::string()])); diff --git a/tests/data/wp_get_post_categories.php b/tests/data/wp_get_post_categories.php index 43827eb..14a1415 100644 --- a/tests/data/wp_get_post_categories.php +++ b/tests/data/wp_get_post_categories.php @@ -38,4 +38,4 @@ assertType('numeric-string|WP_Error', wp_get_post_categories($postID, ['fields' => 'count'])); // Unknown fields value -assertType('array|numeric-string|WP_Error', wp_get_post_categories($postID, ['fields' => (string)$_GET['fields']])); +assertType('array|numeric-string|WP_Error', wp_get_post_categories($postID, ['fields' => Faker::string()])); diff --git a/tests/data/wp_get_post_tags.php b/tests/data/wp_get_post_tags.php index d9ab377..435267e 100644 --- a/tests/data/wp_get_post_tags.php +++ b/tests/data/wp_get_post_tags.php @@ -38,4 +38,4 @@ assertType('numeric-string|WP_Error', wp_get_post_tags($postID, ['fields' => 'count'])); // Unknown fields value -assertType('array|numeric-string|WP_Error', wp_get_post_tags($postID, ['fields' => (string)$_GET['fields']])); +assertType('array|numeric-string|WP_Error', wp_get_post_tags($postID, ['fields' => Faker::string()])); diff --git a/tests/data/wp_get_post_terms.php b/tests/data/wp_get_post_terms.php index 7617280..806ce83 100644 --- a/tests/data/wp_get_post_terms.php +++ b/tests/data/wp_get_post_terms.php @@ -41,4 +41,4 @@ assertType('numeric-string|WP_Error', wp_get_post_terms($postID, $taxonomy, ['fields' => 'count'])); // Unknown fields value -assertType('array|numeric-string|WP_Error', wp_get_post_terms($postID, $taxonomy, ['fields' => (string)$_GET['fields']])); +assertType('array|numeric-string|WP_Error', wp_get_post_terms($postID, $taxonomy, ['fields' => Faker::string()])); diff --git a/tests/data/wp_is_numeric_array.php b/tests/data/wp_is_numeric_array.php index 4c72bac..3684032 100644 --- a/tests/data/wp_is_numeric_array.php +++ b/tests/data/wp_is_numeric_array.php @@ -7,63 +7,40 @@ use function wp_is_numeric_array; use function PHPStan\Testing\assertType; -assertType('false', wp_is_numeric_array((string)$_GET['thing'])); -assertType('false', wp_is_numeric_array((int)$_GET['thing'])); -assertType('false', wp_is_numeric_array((bool)$_GET['value'])); +// No array +assertType('false', wp_is_numeric_array(Faker::string())); +assertType('false', wp_is_numeric_array(Faker::int())); +assertType('false', wp_is_numeric_array(Faker::bool())); assertType('false', wp_is_numeric_array(null)); +// Arrays assertType('true', wp_is_numeric_array([])); - -/** @var list $value */ -$value = $_GET['value']; -assertType('true', wp_is_numeric_array($value)); - -/** @var array $value */ -$value = $_GET['value']; -assertType('true', wp_is_numeric_array($value)); - -/** @var array $value */ -$value = $_GET['value']; -assertType('false', wp_is_numeric_array($value)); - -/** @var array $value */ -$value = $_GET['value']; -assertType('bool', wp_is_numeric_array($value)); - -/** @var array $value */ -$value = $_GET['value']; -assertType('bool', wp_is_numeric_array($value)); - -/** @var array $value */ -$value = $_GET['value']; -assertType('bool', wp_is_numeric_array($value)); - -/** @var array{'key1': 'value1', 'key2': 'value2'} $value */ -$value = $_GET['value']; -assertType('false', wp_is_numeric_array($value)); - -/** @var array{0: 'value0', 1: 'value1'} $value */ -$value = $_GET['value']; -assertType('true', wp_is_numeric_array($value)); - -/** @var array{0: 'value0', key1: 'value1'} $value */ -$value = $_GET['value']; -assertType('bool', wp_is_numeric_array($value)); - -assertType('bool', wp_is_numeric_array($_GET['value'])); - -/** @var list|string */ -$value = $_GET['value']; +assertType('true', wp_is_numeric_array(Faker::list())); +assertType('true', wp_is_numeric_array(Faker::intArray())); +assertType('false', wp_is_numeric_array(Faker::strArray())); +assertType('bool', wp_is_numeric_array(Faker::array())); +assertType('bool', wp_is_numeric_array(Faker::union(Faker::strArray(), Faker::intArray()))); + +// Constant arrays +assertType('false', wp_is_numeric_array(['key1' => 'value1', 'key2' => 'value2'])); +assertType('true', wp_is_numeric_array(['value0', 'value1'])); +assertType('bool', wp_is_numeric_array(['value0', 'key1' => 'value1'])); + +// Maybe array +assertType('bool', wp_is_numeric_array(Faker::mixed())); + +// Test type specifying of wp_is_numeric_array +$value = Faker::union(Faker::string(), Faker::list(Faker::string())); if (wp_is_numeric_array($value)) { assertType('list', $value); } -/** @var array|string */ -$value = $_GET['value']; +$value = Faker::union(Faker::intArray(Faker::string()), Faker::string()); if (wp_is_numeric_array($value)) { assertType('array', $value); } -if (wp_is_numeric_array($_GET['value'])) { - assertType('array', $_GET['value']); +$value = Faker::mixed(); +if (wp_is_numeric_array($value)) { + assertType('array', $value); } diff --git a/tests/data/wp_list_bookmarks.php b/tests/data/wp_list_bookmarks.php index bd6db8b..7676137 100644 --- a/tests/data/wp_list_bookmarks.php +++ b/tests/data/wp_list_bookmarks.php @@ -27,5 +27,5 @@ assertType('string', wp_list_bookmarks(['echo' => 0, 'key' => 'value'])); // Unknown value -assertType('string|null', wp_list_bookmarks(['echo' => (bool)$_GET['echo'], 'key' => 'value'])); -assertType('string|null', wp_list_bookmarks(['echo' => (int)$_GET['echo'], 'key' => 'value'])); +assertType('string|null', wp_list_bookmarks(['echo' => Faker::bool(), 'key' => 'value'])); +assertType('string|null', wp_list_bookmarks(['echo' => Faker::int(), 'key' => 'value'])); diff --git a/tests/data/wp_list_categories.php b/tests/data/wp_list_categories.php index 2fe86e0..fe1bb58 100644 --- a/tests/data/wp_list_categories.php +++ b/tests/data/wp_list_categories.php @@ -27,5 +27,5 @@ assertType('string|false', wp_list_categories(['echo' => 0, 'key' => 'value'])); // Unknown value -assertType('string|false|null', wp_list_categories(['echo' => (bool)$_GET['echo'], 'key' => 'value'])); -assertType('string|false|null', wp_list_categories(['echo' => (int)$_GET['echo'], 'key' => 'value'])); +assertType('string|false|null', wp_list_categories(['echo' => Faker::bool(), 'key' => 'value'])); +assertType('string|false|null', wp_list_categories(['echo' => Faker::int(), 'key' => 'value'])); diff --git a/tests/data/wp_list_pages.php b/tests/data/wp_list_pages.php index 7887889..ed37eb7 100644 --- a/tests/data/wp_list_pages.php +++ b/tests/data/wp_list_pages.php @@ -25,4 +25,4 @@ assertType('string', wp_list_pages(['echo' => false, 'key' => 'value'])); // Unknown value -assertType('string|null', wp_list_pages(['echo' => (bool)$_GET['echo'], 'key' => 'value'])); +assertType('string|null', wp_list_pages(['echo' => Faker::bool(), 'key' => 'value'])); diff --git a/tests/data/wp_parse_list.php b/tests/data/wp_parse_list.php index d697682..7ecc40d 100644 --- a/tests/data/wp_parse_list.php +++ b/tests/data/wp_parse_list.php @@ -4,7 +4,8 @@ namespace PhpStubs\WordPress\Core\Tests; +use function wp_parse_list; use function PHPStan\Testing\assertType; -assertType('list', wp_parse_list((string)$_GET['input_list'])); -assertType('array', wp_parse_list((array)$_GET['input_list'])); +assertType('list', wp_parse_list(Faker::string())); +assertType('array', wp_parse_list(Faker::array())); diff --git a/tests/data/wp_query.php b/tests/data/wp_query.php index 6c557fd..6a64410 100644 --- a/tests/data/wp_query.php +++ b/tests/data/wp_query.php @@ -12,12 +12,8 @@ namespace PhpStubs\WordPress\Core\Tests; -use WP_Query; - use function PHPStan\Testing\assertType; -$wpQuery = new WP_Query(); - -assertType('bool', $wpQuery->query_vars_changed); -assertType('bool|string', $wpQuery->query_vars_hash); -assertType('null', $wpQuery->init_query_flags()); +assertType('bool', Faker::wpQuery()->query_vars_changed); +assertType('bool|string', Faker::wpQuery()->query_vars_hash); +assertType('null', Faker::wpQuery()->init_query_flags()); diff --git a/tests/data/wp_rest_request.php b/tests/data/wp_rest_request.php index 08c0463..7604bc6 100644 --- a/tests/data/wp_rest_request.php +++ b/tests/data/wp_rest_request.php @@ -9,27 +9,21 @@ /** * @var \WP_REST_Request $request */ -$request = new WP_REST_Request(); +$request = Faker::wpRestRequest(); assertType('mixed', $request->get_param('maybeParam')); - assertType('mixed', $request['maybeParam']); - assertType('array', $request->get_params()); - assertType('bool', $request->has_param('maybeParam')); /** * @var \WP_REST_Request> $request */ -$request = new WP_REST_Request(); +$request = Faker::wpRestRequest(); assertType('string|null', $request->get_param('maybeParam')); - assertType('string|null', $request['maybeParam']); - assertType('array', $request->get_params()); - assertType('bool', $request->has_param('maybeParam')); /** @@ -39,7 +33,7 @@ * boolParam: bool * }> $request */ -$request = new WP_REST_Request(); +$request = Faker::wpRestRequest(); assertType('string|null', $request->get_param('stringParam')); assertType('int|null', $request->get_param('intParam')); diff --git a/tests/data/wp_tag_cloud.php b/tests/data/wp_tag_cloud.php index 07a17d3..0cd5369 100644 --- a/tests/data/wp_tag_cloud.php +++ b/tests/data/wp_tag_cloud.php @@ -26,7 +26,7 @@ // Echo true, but format (maybe) array assertType('array|null', wp_tag_cloud(['format' => 'array', 'echo' => true])); -assertType('array|null', wp_tag_cloud(['format' => (string)$_GET['format'], 'echo' => true])); +assertType('array|null', wp_tag_cloud(['format' => Faker::string(), 'echo' => true])); // Echo false assertType('string|null', wp_tag_cloud(['echo' => false])); @@ -34,15 +34,12 @@ assertType('string|null', wp_tag_cloud(['format' => 'list', 'echo' => false])); assertType('array|null', wp_tag_cloud(['format' => 'array', 'echo' => false])); assertType('string|null', wp_tag_cloud(['format' => 'unexpected', 'echo' => false])); -assertType('array|string|null', wp_tag_cloud(['format' => (string)$_GET['format'], 'echo' => false])); +assertType('array|string|null', wp_tag_cloud(['format' => Faker::string(), 'echo' => false])); // Echo unknown -/** @var bool|0|1 $echo */ -$boolZeroOne = $_GET['echo']; - -assertType('string|null', wp_tag_cloud(['echo' => $boolZeroOne])); -assertType('string|null', wp_tag_cloud(['format' => 'flat', 'echo' => $boolZeroOne])); -assertType('string|null', wp_tag_cloud(['format' => 'list', 'echo' => $boolZeroOne])); -assertType('array|null', wp_tag_cloud(['format' => 'array', 'echo' => $boolZeroOne])); -assertType('string|null', wp_tag_cloud(['format' => 'unexpected', 'echo' => $boolZeroOne])); -assertType('array|string|null', wp_tag_cloud(['format' => (string)$_GET['format'], 'echo' => $boolZeroOne])); +assertType('string|null', wp_tag_cloud(['echo' => Faker::bool()])); +assertType('string|null', wp_tag_cloud(['format' => 'flat', 'echo' => Faker::bool()])); +assertType('string|null', wp_tag_cloud(['format' => 'list', 'echo' => Faker::bool()])); +assertType('array|null', wp_tag_cloud(['format' => 'array', 'echo' => Faker::bool()])); +assertType('string|null', wp_tag_cloud(['format' => 'unexpected', 'echo' => Faker::bool()])); +assertType('array|string|null', wp_tag_cloud(['format' => Faker::string(), 'echo' => Faker::bool()])); diff --git a/tests/data/wp_theme.php b/tests/data/wp_theme.php index ed0d0b7..64f9269 100644 --- a/tests/data/wp_theme.php +++ b/tests/data/wp_theme.php @@ -6,8 +6,7 @@ use function PHPStan\Testing\assertType; -/** @var \WP_Theme */ -$theme = $_GET['theme']; +$theme = Faker::wpTheme(); // WP_Theme::__get() assertType('string', $theme->name); @@ -42,7 +41,7 @@ assertType('string', $theme->get('RequiresPHP')); assertType('string', $theme->get('UpdateURI')); assertType('false', $theme->get('NoThemeHeader')); -assertType('array|string|false', $theme->get((string)$_GET['unknown_string'])); +assertType('array|string|false', $theme->get(Faker::string())); // WP_Theme::offsetExists() assertType('false', $theme->offsetExists('NoThemeKey')); @@ -65,7 +64,7 @@ assertType('true', $theme->offsetExists('Theme Root')); assertType('true', $theme->offsetExists('Theme Root URI')); assertType('true', $theme->offsetExists('Parent Theme')); -assertType('bool', $theme->offsetExists((string)$_GET['unknown_string'])); +assertType('bool', $theme->offsetExists(Faker::string())); // WP_Theme::offsetGet() assertType('null', $theme->offsetGet('NoThemeKey')); @@ -88,4 +87,4 @@ assertType('mixed', $theme->offsetGet('Theme Root')); assertType('mixed', $theme->offsetGet('Theme Root URI')); assertType('mixed', $theme->offsetGet('Parent Theme')); -assertType('mixed', $theme->offsetGet((string)$_GET['unknown_string'])); +assertType('mixed', $theme->offsetGet(Faker::string())); diff --git a/tests/data/wp_translations.php b/tests/data/wp_translations.php index 688f458..cac56fc 100644 --- a/tests/data/wp_translations.php +++ b/tests/data/wp_translations.php @@ -6,11 +6,10 @@ use function PHPStan\Testing\assertType; -/** @var \WP_Translations */ -$translations = $_GET['translations']; +$translations = Faker::wpTranslations(); -$string = (string)$_GET['string']; -$singular = $_GET['singular'] ? $string : null; +$string = Faker::string(); +$singular = Faker::union(Faker::string(), null); // WP_Translation::translate() assertType('null', $translations->translate(null)); diff --git a/tests/data/wp_unique_id.php b/tests/data/wp_unique_id.php index 6b497c4..eda982d 100644 --- a/tests/data/wp_unique_id.php +++ b/tests/data/wp_unique_id.php @@ -7,13 +7,10 @@ use function wp_unique_id; use function PHPStan\Testing\assertType; -/** @var numeric-string $numericString */ -$numericString = $_GET['numericString']; - assertType('numeric-string', wp_unique_id()); assertType('numeric-string', wp_unique_id('')); assertType('numeric-string', wp_unique_id('1')); -assertType('numeric-string', wp_unique_id($numericString)); +assertType('numeric-string', wp_unique_id(Faker::numericString())); assertType('string', wp_unique_id('string')); -assertType('string', wp_unique_id($_GET['string'])); +assertType('string', wp_unique_id(Faker::string())); diff --git a/tests/data/wp_widget_factory.php b/tests/data/wp_widget_factory.php index 023f99e..08c1fe2 100644 --- a/tests/data/wp_widget_factory.php +++ b/tests/data/wp_widget_factory.php @@ -4,10 +4,6 @@ namespace PhpStubs\WordPress\Core\Tests; -use WP_Widget_Factory; - use function PHPStan\Testing\assertType; -$factory = new WP_Widget_Factory(); - -assertType('array', $factory->widgets); +assertType('array', Faker::wpWidgetFactory()->widgets);