Skip to content

Commit 1ce8886

Browse files
authored
Narrow return type of wp_extract_urls (#293)
1 parent 9c8e22e commit 1ce8886

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,5 @@
232232
'wp_nonce_field' => [null, 'action' => '-1|string'],
233233
'did_action' => ['int<0, max>'],
234234
'get_current_blog_id' => ['int<0, max>'],
235+
'wp_extract_urls' => ['($content is empty ? array{} : list<string>)'],
235236
];

tests/TypeInferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function dataFileAsserts(): iterable
5757
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_die.php');
5858
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_dropdown_languages.php');
5959
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_error_parameter.php');
60+
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_extract_urls.php');
6061
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_generate_tag_cloud.php');
6162
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_archives.php');
6263
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_post_categories.php');

tests/data/wp_extract_urls.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function wp_extract_urls;
8+
use function PHPStan\Testing\assertType;
9+
10+
assertType('array{}', wp_extract_urls(''));
11+
12+
assertType('list<string>', wp_extract_urls('content with no URLs'));
13+
assertType('list<string>', wp_extract_urls('content with one URL: https://example.com'));
14+
assertType('list<string>', wp_extract_urls('content with multiple URLs: https://example.com and http://another-example.com'));
15+
16+
assertType('list<string>', wp_extract_urls(Faker::string()));

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111116,6 +111116,7 @@ function xmlrpc_removepostdata($content)
111116111116
*
111117111117
* @param string $content Content to extract URLs from.
111118111118
* @return string[] Array of URLs found in passed string.
111119+
* @phpstan-return ($content is empty ? array{} : list<string>)
111119111120
*/
111120111121
function wp_extract_urls($content)
111121111122
{

0 commit comments

Comments
 (0)