Skip to content

Commit 33178a4

Browse files
authored
Narrow parameter type for register_post_type (#380)
1 parent 17066eb commit 33178a4

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
'previous_posts' => ['($display is true ? void : string)'],
127127
'rawurlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
128128
'register_nav_menus' => [null, 'locations' => 'array<string, string>'],
129+
'register_post_type' => [null, 'post_type' => 'lowercase-string&non-empty-string'],
129130
'render_block_core_categories' => ['non-falsy-string'],
130131
'rest_authorization_required_code' => ['401|403'],
131132
'rest_sanitize_boolean' => ["(T is bool ? T : (T is ''|'false'|'FALSE'|'0'|0 ? false : true))", '@phpstan-template T' => 'of bool|string|int', 'value' => 'T'],

tests/ParameterTypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ public function testRegisterNavMenus(): void
117117
);
118118
}
119119

120+
public function testRegisterPostType(): void
121+
{
122+
$this->analyse(
123+
__DIR__ . '/data/param/register-post-type.php',
124+
[
125+
["Parameter #1 \$post_type of function register_post_type expects lowercase-string&non-empty-string, '' given.", 13],
126+
["Parameter #1 \$post_type of function register_post_type expects lowercase-string&non-empty-string, 'PostType' given.", 14],
127+
// Maybes
128+
['Parameter #1 $post_type of function register_post_type expects lowercase-string&non-empty-string, non-empty-string given.', 17],
129+
['Parameter #1 $post_type of function register_post_type expects lowercase-string&non-empty-string, non-falsy-string given.', 18],
130+
['Parameter #1 $post_type of function register_post_type expects lowercase-string&non-empty-string, lowercase-string given.', 19],
131+
['Parameter #1 $post_type of function register_post_type expects lowercase-string&non-empty-string, string given.', 20],
132+
]
133+
);
134+
}
135+
120136
public function testWpdbGetRow(): void
121137
{
122138
$this->analyse(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function register_post_type;
8+
9+
$empty = '';
10+
$containsUppercases = 'PostType';
11+
12+
// Incorrect $post_type
13+
register_post_type($empty, Faker::array());
14+
register_post_type($containsUppercases, Faker::array());
15+
16+
// Maybe incorrect $post_type
17+
register_post_type(Faker::nonEmptyString(), Faker::array());
18+
register_post_type(Faker::nonFalsyString(), Faker::array());
19+
register_post_type(Faker::lowercaseString(), Faker::array());
20+
register_post_type(Faker::string(), Faker::array());
21+
22+
// Correct $post_type
23+
register_post_type('post_type', Faker::array());
24+
register_post_type(Faker::intersection(Faker::lowercaseString(), Faker::nonEmptyString()), Faker::array());

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131627,6 +131627,7 @@ function get_post_types($args = array(), $output = 'names', $operator = 'and')
131627131627
* _builtin?: bool,
131628131628
* _edit_link?: string,
131629131629
* } $args
131630+
* @phpstan-param lowercase-string&non-empty-string $post_type
131630131631
*/
131631131632
function register_post_type($post_type, $args = array())
131632131633
{

0 commit comments

Comments
 (0)