Skip to content

Commit 9dd0446

Browse files
committed
Code Quality: Document properties accessible via magic methods.
Code outside of `wpdb`, `WP_List_Table`, and `WP_Object_Cache` reads several of their protected properties, which the classes' magic `__get()` methods allow for backward compatibility. Since PHPStan cannot see this, each such read was reported as a `property.protected` error. Class-level `@property` tags now document these properties, and the `property.protected` PHPStan baseline is removed. The tags are writable to match the classes' `__set()` methods, except for `wpdb::$check_current_query`, which `__set()` blocks. Beyond the properties that core itself reads, the `wpdb` tags also cover properties that plugins are known to read, while the database password is not documented, to avoid encouraging access to it. The documented types of `wpdb::$col_info` and `WP_Object_Cache::$global_groups` are also corrected: the former can be `null`, and the latter maps group names to `true` rather than listing them. Developed in WordPress#14265. Follow-up to r21472, r31146, r63020. Props mukesh27, westonruter. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@64868 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 94f5ebb commit 9dd0446

7 files changed

Lines changed: 24 additions & 70 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ includes:
5858
- tests/phpstan/baselines/property.nonObject.neon
5959
- tests/phpstan/baselines/property.notFound.neon
6060
- tests/phpstan/baselines/property.private.neon
61-
- tests/phpstan/baselines/property.protected.neon
6261
- tests/phpstan/baselines/return.type.neon
6362
- tests/phpstan/baselines/return.unusedType.neon
6463
- tests/phpstan/baselines/varTag.noVariable.neon

‎src/wp-admin/includes/class-wp-list-table.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* Base class for displaying a list of items in an ajaxified HTML table.
1212
*
1313
* @since 3.1.0
14+
*
15+
* @property array<string, mixed> $_pagination_args Various information needed for displaying the pagination.
16+
* @property WP_Screen $screen The current screen.
1417
*/
1518
#[AllowDynamicProperties]
1619
class WP_List_Table {

‎src/wp-includes/class-wp-object-cache.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* exists, then this file will not be included.
2121
*
2222
* @since 2.0.0
23+
*
24+
* @property array<string, true> $global_groups Global cache groups, keyed by group name.
2325
*/
2426
#[AllowDynamicProperties]
2527
class WP_Object_Cache {
@@ -49,10 +51,10 @@ class WP_Object_Cache {
4951
public $cache_misses = 0;
5052

5153
/**
52-
* List of global cache groups.
54+
* Global cache groups, keyed by group name.
5355
*
5456
* @since 3.0.0
55-
* @var string[]
57+
* @var array<string, true>
5658
*/
5759
protected $global_groups = array();
5860

‎src/wp-includes/class-wpdb.php‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050
* @link https://developer.wordpress.org/reference/classes/wpdb/
5151
*
5252
* @since 0.71
53+
*
54+
* @property mysqli_result|bool|null $result Result of the last query.
55+
* @property-read bool $check_current_query Whether the current query needs to be confidence checked.
56+
* @property array<int, stdClass|false>|null $col_info Saved info on the table column.
57+
* @property int $reconnect_retries Number of times to retry reconnecting before dying.
58+
* @property string $dbuser Database username.
59+
* @property string $dbname Database name.
60+
* @property string $dbhost Database host.
61+
* @property mysqli|false|null $dbh Database handle.
5362
*/
5463
#[AllowDynamicProperties]
5564
class wpdb {
@@ -193,9 +202,15 @@ class wpdb {
193202
/**
194203
* Saved info on the table column.
195204
*
205+
* Each value is populated by mysqli_fetch_field() in {@see wpdb::load_col_info()}, which is
206+
* typed as returning false when there are no more fields. A false value cannot actually occur,
207+
* however, since the loop only runs for the number of fields returned by mysqli_num_fields().
208+
* Populating the array with mysqli_fetch_fields() instead would allow this type to be narrowed
209+
* to a list of stdClass objects.
210+
*
196211
* @since 0.71
197212
*
198-
* @var array
213+
* @var array<int, stdClass|false>|null
199214
*/
200215
protected $col_info;
201216

‎tests/phpstan/baselines/assign.propertyType.neon‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ parameters:
8888
identifier: assign.propertyType
8989
count: 1
9090
path: ../../../src/wp-includes/class-wp-user.php
91-
-
92-
message: '#^Property wpdb\:\:\$col_info \(array\) does not accept null\.$#'
93-
identifier: assign.propertyType
94-
count: 1
95-
path: ../../../src/wp-includes/class-wpdb.php
9691
-
9792
message: '#^Property wpdb\:\:\$last_query \(string\) does not accept null\.$#'
9893
identifier: assign.propertyType

‎tests/phpstan/baselines/isset.property.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ parameters:
154154
count: 1
155155
path: ../../../src/wp-includes/ms-blogs.php
156156
-
157-
message: '#^Property WP_Object_Cache\:\:\$global_groups \(array\<string\>\) in isset\(\) is not nullable\.$#'
157+
message: '#^Property WP_Object_Cache\:\:\$global_groups \(array\<string, true\>\) in isset\(\) is not nullable\.$#'
158158
identifier: isset.property
159159
count: 1
160160
path: ../../../src/wp-includes/ms-blogs.php

‎tests/phpstan/baselines/property.protected.neon‎

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)