Skip to content

Commit f750c20

Browse files
authored
Narrow return and parameter type of several wp_robots_*() functions (#402)
1 parent 8335bba commit f750c20

File tree

6 files changed

+99
-15
lines changed

6 files changed

+99
-15
lines changed

functionMap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@
233233
'wp_remote_post' => [$httpReturnType],
234234
'wp_remote_request' => [$httpReturnType],
235235
'wp_reschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
236+
'wp_robots_max_image_preview_large' => ['array<string, bool|string>', 'robots' => 'array<string, bool|string>'],
237+
'wp_robots_no_robots' => ['array<string, bool|string>', 'robots' => 'array<string, bool|string>'],
238+
'wp_robots_noindex' => ['array<string, bool|string>', 'robots' => 'array<string, bool|string>'],
239+
'wp_robots_noindex_embeds' => ['array<string, bool|string>', 'robots' => 'array<string, bool|string>'],
240+
'wp_robots_noindex_search' => ['array<string, bool|string>', 'robots' => 'array<string, bool|string>'],
241+
'wp_robots_sensitive_page' => ['array<string, bool|string>', 'robots' => 'array<string, bool|string>'],
236242
'wp_safe_remote_get' => [$httpReturnType],
237243
'wp_safe_remote_head' => [$httpReturnType],
238244
'wp_safe_remote_post' => [$httpReturnType],

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</rule>
1919
<rule ref="SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable">
2020
<exclude-pattern>tests/data/param/wpdb.php</exclude-pattern>
21+
<exclude-pattern>tests/data/param/wp-robots.php</exclude-pattern>
2122
<exclude-pattern>tests/data/param/absint.php</exclude-pattern>
2223
</rule>
2324
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">

tests/ParameterTypeTest.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ public function testCheckAjaxReferer(): void
155155
);
156156
}
157157

158+
public function testDoAction(): void
159+
{
160+
$this->analyse(
161+
__DIR__ . '/data/param/do-action.php',
162+
[
163+
["Parameter #1 \$hook_name of function do_action expects non-empty-string, '' given.", 8],
164+
["Parameter #1 \$hook_name of function do_action_ref_array expects non-empty-string, '' given.", 9],
165+
["Parameter #1 \$hook_name of function do_action_deprecated expects non-empty-string, '' given.", 10],
166+
['Parameter #1 $hook_name of function do_action expects non-empty-string, string given.', 13],
167+
['Parameter #1 $hook_name of function do_action_ref_array expects non-empty-string, string given.', 14],
168+
['Parameter #1 $hook_name of function do_action_deprecated expects non-empty-string, string given.', 15],
169+
]
170+
);
171+
}
172+
158173
public function testRegisterActivationHook(): void
159174
{
160175
$this->analyse(
@@ -177,21 +192,6 @@ public function testRegisterDeactivationHook(): void
177192
);
178193
}
179194

180-
public function testDoAction(): void
181-
{
182-
$this->analyse(
183-
__DIR__ . '/data/param/do-action.php',
184-
[
185-
["Parameter #1 \$hook_name of function do_action expects non-empty-string, '' given.", 8],
186-
["Parameter #1 \$hook_name of function do_action_ref_array expects non-empty-string, '' given.", 9],
187-
["Parameter #1 \$hook_name of function do_action_deprecated expects non-empty-string, '' given.", 10],
188-
['Parameter #1 $hook_name of function do_action expects non-empty-string, string given.', 13],
189-
['Parameter #1 $hook_name of function do_action_ref_array expects non-empty-string, string given.', 14],
190-
['Parameter #1 $hook_name of function do_action_deprecated expects non-empty-string, string given.', 15],
191-
]
192-
);
193-
}
194-
195195
public function testRegisterNavMenus(): void
196196
{
197197
$this->analyse(
@@ -246,6 +246,21 @@ public function testWpdbGetRow(): void
246246
);
247247
}
248248

249+
public function testWpRobots(): void
250+
{
251+
$this->analyse(
252+
__DIR__ . '/data/param/wp-robots.php',
253+
[
254+
['Parameter #1 $robots of function wp_robots_max_image_preview_large expects array<string, bool|string>, array<string, int> given.', 16],
255+
['Parameter #1 $robots of function wp_robots_no_robots expects array<string, bool|string>, array<string, int> given.', 17],
256+
['Parameter #1 $robots of function wp_robots_noindex expects array<string, bool|string>, array<string, int> given.', 18],
257+
['Parameter #1 $robots of function wp_robots_noindex_embeds expects array<string, bool|string>, array<string, int> given.', 19],
258+
['Parameter #1 $robots of function wp_robots_noindex_search expects array<string, bool|string>, array<string, int> given.', 20],
259+
['Parameter #1 $robots of function wp_robots_sensitive_page expects array<string, bool|string>, array<string, int> given.', 21],
260+
]
261+
);
262+
}
263+
249264
public function testWpTriggerError(): void
250265
{
251266
$this->analyse(

tests/data/param/wp-robots.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function wp_robots_max_image_preview_large;
8+
use function wp_robots_no_robots;
9+
use function wp_robots_noindex;
10+
use function wp_robots_noindex_embeds;
11+
use function wp_robots_noindex_search;
12+
use function wp_robots_sensitive_page;
13+
14+
// Incorrect usages with array<string, int>
15+
$incorrect = Faker::strArray(Faker::int());
16+
$robots = wp_robots_max_image_preview_large($incorrect);
17+
$robots = wp_robots_no_robots($incorrect);
18+
$robots = wp_robots_noindex($incorrect);
19+
$robots = wp_robots_noindex_embeds($incorrect);
20+
$robots = wp_robots_noindex_search($incorrect);
21+
$robots = wp_robots_sensitive_page($incorrect);
22+
23+
// Correct usages with array<string, string>
24+
$correct = Faker::strArray(Faker::string());
25+
$robots = wp_robots_max_image_preview_large($correct);
26+
$robots = wp_robots_no_robots($correct);
27+
$robots = wp_robots_noindex($correct);
28+
$robots = wp_robots_noindex_embeds($correct);
29+
$robots = wp_robots_noindex_search($correct);
30+
$robots = wp_robots_sensitive_page($correct);

tests/data/return/wp-robots.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function wp_robots_max_image_preview_large;
8+
use function wp_robots_no_robots;
9+
use function wp_robots_noindex;
10+
use function wp_robots_noindex_embeds;
11+
use function wp_robots_noindex_search;
12+
use function wp_robots_sensitive_page;
13+
use function PHPStan\Testing\assertType;
14+
15+
assertType('array<string, bool|string>', wp_robots_max_image_preview_large(Faker::array()));
16+
assertType('array<string, bool|string>', wp_robots_no_robots(Faker::array()));
17+
assertType('array<string, bool|string>', wp_robots_noindex(Faker::array()));
18+
assertType('array<string, bool|string>', wp_robots_noindex_embeds(Faker::array()));
19+
assertType('array<string, bool|string>', wp_robots_noindex_search(Faker::array()));
20+
assertType('array<string, bool|string>', wp_robots_sensitive_page(Faker::array()));

wordpress-stubs.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136666,6 +136666,8 @@ function wp_robots()
136666136666
*
136667136667
* @param array $robots Associative array of robots directives.
136668136668
* @return array Filtered robots directives.
136669+
* @phpstan-param array<string, bool|string> $robots
136670+
* @phpstan-return array<string, bool|string>
136669136671
*/
136670136672
function wp_robots_noindex(array $robots)
136671136673
{
@@ -136683,6 +136685,8 @@ function wp_robots_noindex(array $robots)
136683136685
*
136684136686
* @param array $robots Associative array of robots directives.
136685136687
* @return array Filtered robots directives.
136688+
* @phpstan-param array<string, bool|string> $robots
136689+
* @phpstan-return array<string, bool|string>
136686136690
*/
136687136691
function wp_robots_noindex_embeds(array $robots)
136688136692
{
@@ -136704,6 +136708,8 @@ function wp_robots_noindex_embeds(array $robots)
136704136708
*
136705136709
* @param array $robots Associative array of robots directives.
136706136710
* @return array Filtered robots directives.
136711+
* @phpstan-param array<string, bool|string> $robots
136712+
* @phpstan-return array<string, bool|string>
136707136713
*/
136708136714
function wp_robots_noindex_search(array $robots)
136709136715
{
@@ -136721,6 +136727,8 @@ function wp_robots_noindex_search(array $robots)
136721136727
*
136722136728
* @param array $robots Associative array of robots directives.
136723136729
* @return array Filtered robots directives.
136730+
* @phpstan-param array<string, bool|string> $robots
136731+
* @phpstan-return array<string, bool|string>
136724136732
*/
136725136733
function wp_robots_no_robots(array $robots)
136726136734
{
@@ -136739,6 +136747,8 @@ function wp_robots_no_robots(array $robots)
136739136747
*
136740136748
* @param array $robots Associative array of robots directives.
136741136749
* @return array Filtered robots directives.
136750+
* @phpstan-param array<string, bool|string> $robots
136751+
* @phpstan-return array<string, bool|string>
136742136752
*/
136743136753
function wp_robots_sensitive_page(array $robots)
136744136754
{
@@ -136757,6 +136767,8 @@ function wp_robots_sensitive_page(array $robots)
136757136767
*
136758136768
* @param array $robots Associative array of robots directives.
136759136769
* @return array Filtered robots directives.
136770+
* @phpstan-param array<string, bool|string> $robots
136771+
* @phpstan-return array<string, bool|string>
136760136772
*/
136761136773
function wp_robots_max_image_preview_large(array $robots)
136762136774
{

0 commit comments

Comments
 (0)