Skip to content

Commit

Permalink
Add cond. return types for WP_Translations
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar committed Sep 3, 2024
1 parent f5231f6 commit 11a7d32
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,6 @@
'wp_parse_list' => ['($input_list is array ? array<scalar> : list<string>)'],
'wp_parse_str' => [null, '@phpstan-param-out' => 'array<int|string, array|string> $result'],
'size_format' => ["(\$bytes is not numeric ? false : (\$bytes is negative-int|'0' ? false : string))"],
'WP_Translations::translate' => ['($singular is null ? null : string)'],
'WP_Translations::translate_plural' => ['($singular is null ? null : ($plural is null ? T : string))', '@phpstan-template T' => 'of string|null', 'singular' => 'T', 'count' => 'int'],
];
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
</rule>

<!-- Rules not applied to tests -->
<rule ref="NeutronStandard.Functions.LongFunction.LongFunction">
<exclude-pattern>tests/TypeInferenceTest.php</exclude-pattern>
</rule>
<rule ref="PSR12NeutronRuleset.NamingConventions.MeaningfulVariableName">
<exclude-pattern>tests/</exclude-pattern>
</rule>
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function dataFileAsserts(): iterable
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/wp_translations.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wpdb.php');
}

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

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function PHPStan\Testing\assertType;

/** @var \WP_Translations */
$translations = $_GET['translations'];

$string = (string)$_GET['string'];
$singular = $_GET['singular'] ? $string : null;

// WP_Translation::translate()
assertType('null', $translations->translate(null));
assertType('string', $translations->translate($string));

// WP_Translation::translate_plural()
assertType('null', $translations->translate_plural(null, null));
assertType('string', $translations->translate_plural($string, null));
assertType('null', $translations->translate_plural(null, $string));
assertType('string', $translations->translate_plural($string, $string));
assertType('string|null', $translations->translate_plural($singular, $string));
5 changes: 5 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -68875,6 +68875,10 @@ public function __get(string $name)
* @param int|float $count Count. Should be an integer, but some plugins pass floats.
* @param string|null $context Context.
* @return string|null Translation if it exists, or the unchanged singular string.
* @phpstan-template T of string|null
* @phpstan-param T $singular
* @phpstan-param int $count
* @phpstan-return ($singular is null ? null : ($plural is null ? T : string))
*/
public function translate_plural($singular, $plural, $count = 1, $context = '')
{
Expand All @@ -68887,6 +68891,7 @@ public function translate_plural($singular, $plural, $count = 1, $context = '')
* @param string|null $singular Singular string.
* @param string|null $context Context.
* @return string|null Translation if it exists, or the unchanged singular string
* @phpstan-return ($singular is null ? null : string)
*/
public function translate($singular, $context = '')
{
Expand Down

0 comments on commit 11a7d32

Please sign in to comment.