Make WordPress Core


Ignore:
Timestamp:
07/22/2026 04:52:41 AM (5 weeks ago)
Author:
westonruter
Message:

Code Quality: Improve comment API type coverage.

Add a Data_Array array-shape type describing the keys returned by WP_Comment::to_array(), and narrow the properties it covers: comment_approved and the two datetime fields become non-empty-string, and the values core uses for it and for comment_type are documented. comment_type itself stays a plain string, because comments created before 5.5.0 may store an empty string rather than 'comment', which is why get_comment_type() normalizes that case on read.

Declare the 21 post fields that WP_Comment::__get() proxies to the comment's post as @property-read, typed to match the corresponding WP_Post property. These were previously invisible to static analysis, IDE completion, and the generated documentation. Also correct WP_Comment::$children, previously a bare array, which is null until get_children() populates it, and type the comment arrays keyed by comment ID as array<int, WP_Comment>, since PHP coerces the numeric string comment_ID to an integer key on assignment.

Add conditional return types to WP_Comment::get_children(), get_comment(), get_comments(), and get_approved_comments(), and document every argument ::get_children() actually accepts, several of which were already in use but undocumented. Comment ID arrays are narrowed to non-negative-int[], and WP_Comment_Query::$comments is typed as null until a query is run.

Several latent issues surfaced by the analysis are fixed:

  • WP_Comment::get_children() now returns a count or fields query directly rather than storing it in the children cache. That cache holds WP_Comment objects and is read back by add_child(), get_child(), and the 'flat' format, so writing an integer or a list of IDs into it left the object returning the wrong thing on a subsequent call.
  • get_comment() now hands only numeric values to WP_Comment::get_instance(). Previously anything that was not a WP_Comment or some other object fell through to be cast to an integer ID, even if it wasn't numeric. Now null is returned in such cases.
  • WP_Comment::get_instance() ignores a non-object read from the comment cache rather than passing it to the WP_Comment constructor, where get_object_vars() would raise a TypeError.
  • WP_Comment::__isset() returns false, and WP_Comment::__get() returns null, when the comment's post no longer exists, instead of raising a TypeError and a warning respectively. __get() also returns null when the comment is not attached to a post at all; previously get_post( 0 ) fell back to the global $post, so the getter returned an unrelated post's field even though __isset() reported that same property as unset.

Developed in https://github.com/WordPress/wordpress-develop/pull/12606.
Follow-up to r34583, r62648, r62694, r62717.

Props westonruter, adamsilverstein.
See #64898.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r61403 r62822  
    9494         * List of comments located by the query.
    9595         *
     96         * Null until a query has been run.
     97         *
    9698         * @since 4.0.0
    97          * @var int[]|WP_Comment[]
     99         * @var int[]|WP_Comment[]|null
     100         * @phpstan-var non-negative-int[]|array<int, WP_Comment>|null
    98101         */
    99102        public $comments;
     
    104107         * @since 4.4.0
    105108         * @var int
     109         * @phpstan-var non-negative-int
    106110         */
    107111        public $found_comments = 0;
     
    112116         * @since 4.4.0
    113117         * @var int
     118         * @phpstan-var non-negative-int
    114119         */
    115120        public $max_num_pages = 0;
     
    360365         *
    361366         * @param string|array $query Array or URL query string of parameters.
    362          * @return array|int List of comments, or number of comments when 'count' is passed as a query var.
     367         * @return WP_Comment[]|int[]|int List of comments, or number of comments when 'count' is passed as a query var.
     368         * @phpstan-return array<int, WP_Comment>|non-negative-int[]|non-negative-int
    363369         */
    364370        public function query( $query ) {
     
    375381         *
    376382         * @return int|int[]|WP_Comment[] List of comments or number of found comments if `$count` argument is true.
     383         * @phpstan-return array<int, WP_Comment>|non-negative-int[]|non-negative-int
    377384         */
    378385        public function get_comments() {
     
    542549         *
    543550         * @return int|array A single count of comment IDs if a count query. An array of comment IDs if a full query.
     551         * @phpstan-return non-negative-int|list<non-negative-int>
    544552         */
    545553        protected function get_comment_ids() {
Note: See TracChangeset for help on using the changeset viewer.