Skip to content

Commit

Permalink
Add cond. return type for wp_generate_tag_cloud() (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Aug 27, 2024
1 parent 4582a67 commit c1e8f57
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'wp_die' => ['($args is array{exit: false} ? void : never))'],
'wp_dropdown_languages' => ["(\$args is array{id: null|''} ? void : (\$args is array{name: null|''} ? void : string))"],
'wp_clear_scheduled_hook' => ['(0|positive-int|($wp_error is false ? false : \WP_Error))', 'args' => $cronArgsType],
'wp_generate_tag_cloud' => ["(\$args is array{format: 'array'} ? array<int, string> : string)"],
'wp_get_schedule' => [null, 'args' => $cronArgsType],
'wp_get_scheduled_event' => [null, 'args' => $cronArgsType],
'wp_get_archives' => ['($args is array{echo: false|0} ? string : void)'],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_die.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_dropdown_languages.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_error_parameter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_generate_tag_cloud.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_archives.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_is_numeric_array.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_bookmarks.php');
Expand Down
28 changes: 28 additions & 0 deletions tests/data/wp_generate_tag_cloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function wp_generate_tag_cloud;
use function PHPStan\Testing\assertType;

/** @var array<\WP_Term> $args */
$tags = $_GET['tags'];

// Default $args['format] value.
assertType('string', wp_generate_tag_cloud($tags));
assertType('string', wp_generate_tag_cloud($tags, []));

// Requesting array
assertType('array<int, string>', wp_generate_tag_cloud($tags, ['format' => 'array', 'key' => 'value']));

// Requesting string
assertType('string', wp_generate_tag_cloud($tags, ['format' => 'list', 'key' => 'value']));
assertType('string', wp_generate_tag_cloud($tags, ['format' => 'flat', 'key' => 'value']));

// Unexpected $args['format] value
assertType('string', wp_generate_tag_cloud($tags, ['format' => 'unexpected', 'key' => 'value']));

// Unknown $args['format] value
assertType('array<int, string>|string', wp_generate_tag_cloud($tags, ['format' => (string)$_GET['format'], 'key' => 'value']));
1 change: 1 addition & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -102255,6 +102255,7 @@ function default_topic_count_scale($count)
* topic_count_scale_callback?: callable,
* show_count?: bool|int,
* } $args
* @phpstan-return ($args is array{format: 'array'} ? array<int, string> : string)
*/
function wp_generate_tag_cloud($tags, $args = '')
{
Expand Down

0 comments on commit c1e8f57

Please sign in to comment.