Skip to content

Commit

Permalink
Add cond. return type for wp_tag_cloud() (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Aug 27, 2024
1 parent f0a3175 commit 9a2b025
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'wp_schedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
'wp_schedule_single_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
'wp_slash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'wp_tag_cloud' => ["(\$args is array{format: 'array'} ? array<int, string>|void : (\$args is array{echo: false|0} ? string|void : void))"],
'wp_unschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
'wp_unslash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'wp_widget_rss_form' => ['void', 'args' => $wpWidgetRssFormArgsType, 'inputs' => $wpWidgetRssFormInputsType],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_pages.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_parse_list.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_rest_request.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_tag_cloud.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_theme.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wpdb.php');
}
Expand Down
48 changes: 48 additions & 0 deletions tests/data/wp_tag_cloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Note:
* Starting from PHPStan 1.10.49, void types, including void in unions, are
* transformed into null.
*
* @link https://github.com/phpstan/phpstan-src/pull/2778
*/

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function PHPStan\Testing\assertType;
use function wp_tag_cloud;

// Default value
assertType('null', wp_tag_cloud());

// Echo true
assertType('null', wp_tag_cloud(['echo' => true]));
assertType('null', wp_tag_cloud(['format' => 'flat', 'echo' => true]));
assertType('null', wp_tag_cloud(['format' => 'list', 'echo' => true]));
assertType('null', wp_tag_cloud(['format' => 'unexpected', 'echo' => true]));

// Echo true, but format (maybe) array
assertType('array<int, string>|null', wp_tag_cloud(['format' => 'array', 'echo' => true]));
assertType('array<int, string>|null', wp_tag_cloud(['format' => (string)$_GET['format'], 'echo' => true]));

// Echo false
assertType('string|null', wp_tag_cloud(['echo' => false]));
assertType('string|null', wp_tag_cloud(['format' => 'flat', 'echo' => false]));
assertType('string|null', wp_tag_cloud(['format' => 'list', 'echo' => false]));
assertType('array<int, string>|null', wp_tag_cloud(['format' => 'array', 'echo' => false]));
assertType('string|null', wp_tag_cloud(['format' => 'unexpected', 'echo' => false]));
assertType('array<int, string>|string|null', wp_tag_cloud(['format' => (string)$_GET['format'], '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<int, string>|null', wp_tag_cloud(['format' => 'array', 'echo' => $boolZeroOne]));
assertType('string|null', wp_tag_cloud(['format' => 'unexpected', 'echo' => $boolZeroOne]));
assertType('array<int, string>|string|null', wp_tag_cloud(['format' => (string)$_GET['format'], 'echo' => $boolZeroOne]));
1 change: 1 addition & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -102180,6 +102180,7 @@ function wp_list_categories($args = '')
* topic_count_scale_callback?: callable,
* show_count?: bool|int,
* } $args
* @phpstan-return ($args is array{format: 'array'} ? array<int, string>|void : ($args is array{echo: false|0} ? string|void : void))
*/
function wp_tag_cloud($args = '')
{
Expand Down

0 comments on commit 9a2b025

Please sign in to comment.