Make WordPress Core

Changeset 62635


Ignore:
Timestamp:
07/05/2026 12:27:24 AM (2 months ago)
Author:
westonruter
Message:

Docs: Modernize and improve specificity of types in WP_Error class.

This brings the WP_Error class to full PHPStan rule level 10 compliance.

Also make use of null coalescing operator where appropriate, and simplify has_errors() method.

Developed in https://github.com/WordPress/wordpress-develop/pull/12405.
Follow-up to r42761, r49115, r49116.

See #64898, #64897.

File:
1 edited

Legend:

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

    r55398 r62635  
    2222         *
    2323         * @since 2.1.0
    24          * @var array
     24         * @var array<int|string, string[]>
    2525         */
    2626        public $errors = array();
     
    3030         *
    3131         * @since 2.1.0
    32          * @var array
     32         * @var array<int|string, mixed>
    3333         */
    3434        public $error_data = array();
     
    3838         *
    3939         * @since 5.6.0
    40          * @var array[]
     40         * @var array<int|string, mixed[]>
    4141         */
    4242        protected $additional_data = array();
     
    7272         * @since 2.1.0
    7373         *
    74          * @return array List of error codes, if available.
     74         * @return list<int|string> List of error codes, if available.
    7575         */
    7676        public function get_error_codes() {
     
    112112                if ( empty( $code ) ) {
    113113                        $all_messages = array();
    114                         foreach ( (array) $this->errors as $code => $messages ) {
     114                        foreach ( (array) $this->errors as $messages ) {
    115115                                $all_messages = array_merge( $all_messages, $messages );
    116116                        }
     
    119119                }
    120120
    121                 if ( isset( $this->errors[ $code ] ) ) {
    122                         return $this->errors[ $code ];
    123                 } else {
    124                         return array();
    125                 }
     121                return $this->errors[ $code ] ?? array();
    126122        }
    127123
     
    162158                }
    163159
    164                 if ( isset( $this->error_data[ $code ] ) ) {
    165                         return $this->error_data[ $code ];
    166                 }
     160                return $this->error_data[ $code ] ?? null;
    167161        }
    168162
     
    175169         */
    176170        public function has_errors() {
    177                 if ( ! empty( $this->errors ) ) {
    178                         return true;
    179                 }
    180                 return false;
     171                return (bool) $this->errors;
    181172        }
    182173
     
    189180         * @param string     $message Error message.
    190181         * @param mixed      $data    Optional. Error data. Default empty string.
     182         * @return void
    191183         */
    192184        public function add( $code, $message, $data = '' ) {
     
    218210         * @param mixed      $data Error data.
    219211         * @param string|int $code Error code.
     212         * @return void
    220213         */
    221214        public function add_data( $data, $code = '' ) {
     
    266259         *
    267260         * @param string|int $code Error code.
     261         * @return void
    268262         */
    269263        public function remove( $code ) {
     
    279273         *
    280274         * @param WP_Error $error Error object to merge.
     275         * @return void
    281276         */
    282277        public function merge_from( WP_Error $error ) {
     
    290285         *
    291286         * @param WP_Error $error Error object to export into.
     287         * @return void
    292288         */
    293289        public function export_to( WP_Error $error ) {
     
    302298         * @param WP_Error $from The WP_Error to copy from.
    303299         * @param WP_Error $to   The WP_Error to copy to.
     300         * @return void
    304301         */
    305302        protected static function copy_errors( WP_Error $from, WP_Error $to ) {
Note: See TracChangeset for help on using the changeset viewer.