Skip to content

Commit 8ed494f

Browse files
authored
Widen callable type to allow empty string default value (#413)
* Widen type to allow default value in Custom_Background::__construct * Widen type to allow default value in Custom_Image_Header::__construct
1 parent 0e71023 commit 8ed494f

5 files changed

Lines changed: 73 additions & 0 deletions

File tree

functionMap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@
268268
'wp_widgets_access_body_class' => ['non-falsy-string'],
269269
'zeroise' => ['($threshold is 0 ? lowercase-string&non-empty-string&numeric-string : ($number is int<0, max> ? lowercase-string&non-empty-string&numeric-string : lowercase-string&non-empty-string))', 'threshold' => 'int<0, max>'],
270270
// Classes, methods, and properties
271+
'Custom_Background::$admin_header_callback' => [null, '@phpstan-var' => "''|callable(): void"],
272+
'Custom_Background::$admin_image_div_callback' => [null, '@phpstan-var' => "''|callable(): void"],
273+
'Custom_Background::__construct' => [null, 'admin_header_callback' => "''|callable(): void", 'admin_image_div_callback' => "''|callable(): void"],
274+
'Custom_Image_Header::$admin_header_callback' => [null, '@phpstan-var' => "''|callable(): void"],
275+
'Custom_Image_Header::$admin_image_div_callback' => [null, '@phpstan-var' => "''|callable(): void"],
276+
'Custom_Image_Header::__construct' => [null, 'admin_image_div_callback' => "''|callable(): void"],
271277
'Custom_Image_Header::set_header_image' => [null, 'choice' => 'string|array{attachment_id: int<1, max>, url: string, width: int<0, max>, height: int<0, max>}'],
272278
'Custom_Image_Header::show_header_selector' => [null, 'type' => "'default'|'uploaded'"],
273279
'WP_Block_List' => [null, '@phpstan-implements' => 'ArrayAccess<int, \WP_Block>'],

tests/ParameterTypeTest.php

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

158+
public function testCustomBackground(): void
159+
{
160+
$this->analyse(
161+
__DIR__ . '/data/param/custom-background.php',
162+
[
163+
["Parameter #2 \$admin_image_div_callback of class Custom_Background constructor expects ''|(callable(): void), Closure(int): int given.", 20],
164+
]
165+
);
166+
}
167+
168+
public function testCustomImageHeader(): void
169+
{
170+
$this->analyse(
171+
__DIR__ . '/data/param/custom-image-header.php',
172+
[
173+
["Parameter #2 \$admin_image_div_callback of class Custom_Image_Header constructor expects ''|(callable(): void), Closure(int): int given.", 20],
174+
]
175+
);
176+
}
177+
158178
public function testDoAction(): void
159179
{
160180
$this->analyse(
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 Custom_Background;
8+
9+
$compatibleCallback = static function (): void {
10+
// Do nothing
11+
};
12+
$incompatibleCallback = static function (int $arg): int {
13+
return $arg;
14+
};
15+
16+
// Correct
17+
new Custom_Background('', ''); // Empty string as callback is allowed
18+
19+
// Incorrect
20+
new Custom_Background($compatibleCallback, $incompatibleCallback);
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 Custom_Image_Header;
8+
9+
$compatibleCallback = static function (): void {
10+
// Do nothing
11+
};
12+
$incompatibleCallback = static function (int $arg): int {
13+
return $arg;
14+
};
15+
16+
// Correct
17+
new Custom_Image_Header($compatibleCallback, ''); // Empty string as callback is allowed
18+
19+
// Incorrect
20+
new Custom_Image_Header($compatibleCallback, $incompatibleCallback);

wordpress-stubs.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,13 +1026,15 @@ class Custom_Background
10261026
*
10271027
* @since 3.0.0
10281028
* @var callable
1029+
* @phpstan-var ''|callable(): void
10291030
*/
10301031
public $admin_header_callback;
10311032
/**
10321033
* Callback for header div.
10331034
*
10341035
* @since 3.0.0
10351036
* @var callable
1037+
* @phpstan-var ''|callable(): void
10361038
*/
10371039
public $admin_image_div_callback;
10381040
/**
@@ -1044,6 +1046,8 @@ class Custom_Background
10441046
* Default empty string.
10451047
* @param callable $admin_image_div_callback Optional. Custom image div output callback.
10461048
* Default empty string.
1049+
* @phpstan-param ''|callable(): void $admin_header_callback
1050+
* @phpstan-param ''|callable(): void $admin_image_div_callback
10471051
*/
10481052
public function __construct($admin_header_callback = '', $admin_image_div_callback = '')
10491053
{
@@ -1145,13 +1149,15 @@ class Custom_Image_Header
11451149
*
11461150
* @since 2.1.0
11471151
* @var callable
1152+
* @phpstan-var ''|callable(): void
11481153
*/
11491154
public $admin_header_callback;
11501155
/**
11511156
* Callback for header div.
11521157
*
11531158
* @since 3.0.0
11541159
* @var callable
1160+
* @phpstan-var ''|callable(): void
11551161
*/
11561162
public $admin_image_div_callback;
11571163
/**
@@ -1169,6 +1175,7 @@ class Custom_Image_Header
11691175
* @param callable $admin_header_callback Administration header callback.
11701176
* @param callable $admin_image_div_callback Optional. Custom image div output callback.
11711177
* Default empty string.
1178+
* @phpstan-param ''|callable(): void $admin_image_div_callback
11721179
*/
11731180
public function __construct($admin_header_callback, $admin_image_div_callback = '')
11741181
{

0 commit comments

Comments
 (0)