Skip to content

Commit 933e2cb

Browse files
committed
Docs: Add PHPStan types for optional callback parameters.
The `add_*_page()` functions and the `Custom_Background` and `Custom_Image_Header` constructors default their callback to an empty string, and plugins as well as core pass `null` to them, neither of which a plain `callable` allows. Their callbacks are now documented as `''|(callable(): mixed)|null`, since the page and header hooks invoke them without arguments. The type is written out at each use rather than declared as a global alias, as aliases defined only for core's own analysis are lost to anyone analyzing against core's source or the stubs generated from it. The deprecated third parameter of `unregister_setting()` is narrowed to its default of an empty string, in the same way as other unused deprecated parameters, so that static analysis flags callers still passing a sanitize callback. The callbacks of `register_activation_hook()`, `register_deactivation_hook()`, `register_uninstall_hook()`, and `add_feed()` are documented with the arguments core calls them with. This resolves every entry in the `parameter.defaultValue` PHPStan baseline, so that baseline is removed. Developed in #13890. Follow-up to r63020, r64163, r64756. Props swissspidy, marian1, westonruter. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@64793 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a844bea commit 933e2cb

9 files changed

Lines changed: 174 additions & 238 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ includes:
5151
- tests/phpstan/baselines/offsetAccess.nonOffsetAccessible.neon
5252
- tests/phpstan/baselines/offsetAccess.notFound.neon
5353
- tests/phpstan/baselines/offsetAssign.valueType.neon
54-
- tests/phpstan/baselines/parameter.defaultValue.neon
5554
- tests/phpstan/baselines/parameter.notFound.neon
5655
- tests/phpstan/baselines/parameterByRef.type.neon
5756
- tests/phpstan/baselines/parameterByRef.unusedType.neon

‎src/wp-admin/includes/class-custom-background.php‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ class Custom_Background {
1818
* Callback for administration header.
1919
*
2020
* @since 3.0.0
21-
* @var callable
21+
* @var callable|string|null
22+
*
23+
* @phpstan-var ''|(callable(): mixed)|null
2224
*/
2325
public $admin_header_callback;
2426

2527
/**
2628
* Callback for header div.
2729
*
2830
* @since 3.0.0
29-
* @var callable
31+
* @var callable|string|null
32+
*
33+
* @phpstan-var ''|(callable(): mixed)|null
3034
*/
3135
public $admin_image_div_callback;
3236

@@ -43,10 +47,13 @@ class Custom_Background {
4347
*
4448
* @since 3.0.0
4549
*
46-
* @param callable $admin_header_callback Optional. Administration header callback.
47-
* Default empty string.
48-
* @param callable $admin_image_div_callback Optional. Custom image div output callback.
49-
* Default empty string.
50+
* @param callable|string|null $admin_header_callback Optional. Administration header callback.
51+
* Empty string or null for none. Default empty string.
52+
* @param callable|string|null $admin_image_div_callback Optional. Custom image div output callback.
53+
* Empty string or null for none. Default empty string.
54+
*
55+
* @phpstan-param ''|(callable(): mixed)|null $admin_header_callback
56+
* @phpstan-param ''|(callable(): mixed)|null $admin_image_div_callback
5057
*/
5158
public function __construct( $admin_header_callback = '', $admin_image_div_callback = '' ) {
5259
$this->admin_header_callback = $admin_header_callback;

‎src/wp-admin/includes/class-custom-image-header.php‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ class Custom_Image_Header {
1818
* Callback for administration header.
1919
*
2020
* @since 2.1.0
21-
* @var callable
21+
* @var callable|string|null
22+
*
23+
* @phpstan-var ''|(callable(): mixed)|null
2224
*/
2325
public $admin_header_callback;
2426

2527
/**
2628
* Callback for header div.
2729
*
2830
* @since 3.0.0
29-
* @var callable
31+
* @var callable|string|null
32+
*
33+
* @phpstan-var ''|(callable(): mixed)|null
3034
*/
3135
public $admin_image_div_callback;
3236

@@ -51,9 +55,13 @@ class Custom_Image_Header {
5155
*
5256
* @since 2.1.0
5357
*
54-
* @param callable $admin_header_callback Administration header callback.
55-
* @param callable $admin_image_div_callback Optional. Custom image div output callback.
56-
* Default empty string.
58+
* @param callable|string|null $admin_header_callback Administration header callback.
59+
* Empty string or null for none.
60+
* @param callable|string|null $admin_image_div_callback Optional. Custom image div output callback.
61+
* Empty string or null for none. Default empty string.
62+
*
63+
* @phpstan-param ''|(callable(): mixed)|null $admin_header_callback
64+
* @phpstan-param ''|(callable(): mixed)|null $admin_image_div_callback
5765
*/
5866
public function __construct( $admin_header_callback, $admin_image_div_callback = '' ) {
5967
$this->admin_header_callback = $admin_header_callback;

‎src/wp-admin/includes/deprecated.php‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,13 +1427,15 @@ function _relocate_children( $old_ID, $new_ID ) {
14271427
* @see add_menu_page()
14281428
* @global int $_wp_last_object_menu
14291429
*
1430-
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1431-
* @param string $menu_title The text to be used for the menu.
1432-
* @param string $capability The capability required for this menu to be displayed to the user.
1433-
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1434-
* @param callable $callback Optional. The function to be called to output the content for this page.
1435-
* @param string $icon_url Optional. The URL to the icon to be used for this menu.
1430+
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1431+
* @param string $menu_title The text to be used for the menu.
1432+
* @param string $capability The capability required for this menu to be displayed to the user.
1433+
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1434+
* @param callable|string|null $callback Optional. The function to be called to output the content for this page.
1435+
* @param string $icon_url Optional. The URL to the icon to be used for this menu.
14361436
* @return string The resulting page's hook_suffix.
1437+
*
1438+
* @phpstan-param ''|(callable(): mixed)|null $callback
14371439
*/
14381440
function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '') {
14391441
_deprecated_function( __FUNCTION__, '4.5.0', 'add_menu_page()' );
@@ -1460,13 +1462,15 @@ function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $ca
14601462
* @see add_menu_page()
14611463
* @global int $_wp_last_utility_menu
14621464
*
1463-
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1464-
* @param string $menu_title The text to be used for the menu.
1465-
* @param string $capability The capability required for this menu to be displayed to the user.
1466-
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1467-
* @param callable $callback Optional. The function to be called to output the content for this page.
1468-
* @param string $icon_url Optional. The URL to the icon to be used for this menu.
1465+
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1466+
* @param string $menu_title The text to be used for the menu.
1467+
* @param string $capability The capability required for this menu to be displayed to the user.
1468+
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1469+
* @param callable|string|null $callback Optional. The function to be called to output the content for this page.
1470+
* @param string $icon_url Optional. The URL to the icon to be used for this menu.
14691471
* @return string The resulting page's hook_suffix.
1472+
*
1473+
* @phpstan-param ''|(callable(): mixed)|null $callback
14701474
*/
14711475
function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '') {
14721476
_deprecated_function( __FUNCTION__, '4.5.0', 'add_menu_page()' );

0 commit comments

Comments
 (0)