Skip to content

Commit

Permalink
Set conditional return type for rest_ensure_response (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored May 15, 2024
1 parent 86b646c commit cd5f253
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'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],
'rest_ensure_response' => ['($response is WP_Error ? WP_Error : WP_REST_Response)'],
'WP_REST_Request' => [null, '@phpstan-template' => 'T of array', '@phpstan-implements' => 'ArrayAccess<key-of<T>, value-of<T>>'],
'WP_REST_Request::offsetExists' => [null, 'offset' => 'key-of<T>'],
'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of<T>', 'offset' => 'TOffset'],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_error_parameter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_theme.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wpdb.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/rest_ensure_response.php');
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/data/rest_ensure_response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use WP_Error;
use WP_REST_Response;
use function get_term;
use function PHPStan\Testing\assertType;

assertType('WP_Error', rest_ensure_response(new WP_Error()));
assertType('WP_REST_Response', rest_ensure_response([]));
assertType('WP_REST_Response', rest_ensure_response(new WP_REST_Response()));
1 change: 1 addition & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -134212,6 +134212,7 @@ function rest_ensure_request($request)
* @return WP_REST_Response|WP_Error If response generated an error, WP_Error, if response
* is already an instance, WP_REST_Response, otherwise
* returns a new WP_REST_Response instance.
* @phpstan-return ($response is WP_Error ? WP_Error : WP_REST_Response)
*/
function rest_ensure_response($response)
{
Expand Down

0 comments on commit cd5f253

Please sign in to comment.