Skip to content

Commit

Permalink
Narrow return type for __return_zero, __return_empty_array, __return_…
Browse files Browse the repository at this point in the history
…empty_string (#230)
  • Loading branch information
IanDelMar authored Sep 26, 2024
1 parent 199f37b commit 7d957a9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* @link https://github.com/phpstan/phpstan-src/blob/1.10.x/resources/functionMap.php
*/
return [
'__return_empty_array' => ['array{}'],
'__return_empty_string' => ["''"],
'__return_zero' => ['0'],
'_wp_json_sanity_check' => ['T', '@phpstan-template' => 'T', 'value' => 'T', 'depth' => 'positive-int'],
'_get_list_table' => ["(\$class_name is 'WP_Posts_List_Table'|'WP_Media_List_Table'|'WP_Terms_List_Table'|'WP_Users_List_Table'|'WP_Comments_List_Table'|'WP_Post_Comments_List_Table'|'WP_Links_List_Table'|'WP_Plugin_Install_List_Table'|'WP_Themes_List_Table'|'WP_Theme_Install_List_Table'|'WP_Plugins_List_Table'|'WP_Application_Passwords_List_Table'|'WP_MS_Sites_List_Table'|'WP_MS_Users_List_Table'|'WP_MS_Themes_List_Table'|'WP_Privacy_Data_Export_Requests_List_Table'|'WP_Privacy_Data_Removal_Requests_List_Table' ? T : false)", '@phpstan-template' => 'T', 'class_name' => 'class-string<T>', 'args' => 'array{screen?: string}'],
'absint' => ['($maybeint is T&non-negative-int ? T : ($maybeint is negative-int ? positive-int : ($maybeint is empty ? 0 : ($maybeint is numeric-string ? non-negative-int : ($maybeint is string ? 0 : ($maybeint is true|non-empty-array ? 1 : ($maybeint is bool ? 0|1 : non-negative-int)))))))', '@phpstan-template T' => 'of int', 'maybeint' => 'T|scalar|array|resource|null'],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TypeInferenceTest extends \PHPStan\Testing\TypeInferenceTestCase
/** @return iterable<mixed> */
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/__return.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/_get_list_table.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/_wp_json_sanity_check.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/absint.php');
Expand Down
14 changes: 14 additions & 0 deletions tests/data/__return.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 function __return_empty_array;
use function __return_empty_string;
use function __return_zero;
use function PHPStan\Testing\assertType;

assertType('0', __return_zero());
assertType('array{}', __return_empty_array());
assertType("''", __return_empty_string());
3 changes: 3 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -109993,6 +109993,7 @@ function __return_false()
* @since 3.0.0
*
* @return int 0.
* @phpstan-return 0
*/
function __return_zero()
{
Expand All @@ -110005,6 +110006,7 @@ function __return_zero()
* @since 3.0.0
*
* @return array Empty array.
* @phpstan-return array{}
*/
function __return_empty_array()
{
Expand All @@ -110031,6 +110033,7 @@ function __return_null()
* @see __return_null()
*
* @return string Empty string.
* @phpstan-return ''
*/
function __return_empty_string()
{
Expand Down

0 comments on commit 7d957a9

Please sign in to comment.