Make WordPress Core


Ignore:
Timestamp:
10/14/2026 11:44:46 PM (5 weeks ago)
Author:
westonruter
Message:

Code Quality: Correct nullable WP_Http_Cookie property types.

A cookie created without a name, value, domain, expiration or host-only flag, or from incomplete data, leaves those properties null, and an unparsable expiration date is stored as false, but their documented types said otherwise. PHPStan therefore treated isset() checks on them as always true, so correcting the types retires the booleanOr.alwaysFalse baseline entirely along with several isset.property entries.

The corrected types surfaced real errors in callers, which are fixed rather than baselined:

  • In WP_Http::normalize_cookies(), a WP_Http_Cookie value was passed to WpOrg\Requests\Cookie as is, which throws an InvalidArgument exception for anything but a string, failing the request. A cookie without a value or with a non-scalar value is now skipped, as WP_Http_Cookie::getHeaderValue() already does, and other scalar values are cast to strings.
  • wp_remote_retrieve_cookie_value() now returns an empty string instead of null for a cookie without a value.

The handling of empty or missing cookie names is unchanged, as it is being addressed separately in #66305.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14283.
Follow-up to r38164, r45135, r57501, r63023.

Props huzaifaalmesbah, westonruter.
See #65817, #66305.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/http/http.php

    r62593 r64350  
    697697                $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar['1'] );
    698698        }
     699
     700        /**
     701         * Tests that {@see WP_Http::normalize_cookies()} skips a cookie without a value, as {@see WP_Http_Cookie::getHeaderValue()} does.
     702         *
     703         * @covers WP_Http::normalize_cookies
     704         */
     705        public function test_normalize_cookies_skips_cookie_without_value(): void {
     706                $cookie = new WP_Http_Cookie( array( 'name' => 'flavor' ) );
     707                $this->assertNull( $cookie->value, 'Expected the cookie value to be null.' );
     708                $this->assertSame( '', $cookie->getHeaderValue(), 'Expected the cookie to be omitted from the header value.' );
     709
     710                $cookie_jar = WP_Http::normalize_cookies( array( $cookie ) );
     711
     712                $this->assertSame( array(), iterator_to_array( $cookie_jar ) );
     713        }
     714
     715        /**
     716         * Tests that {@see WP_Http::normalize_cookies()} skips a cookie with a non-scalar value.
     717         *
     718         * @covers WP_Http::normalize_cookies
     719         */
     720        public function test_normalize_cookies_skips_cookie_with_non_scalar_value(): void {
     721                $cookie = new WP_Http_Cookie(
     722                        array(
     723                                'name'  => 'prefs',
     724                                'value' => array( 'a' => 1 ),
     725                        )
     726                );
     727
     728                $cookie_jar = WP_Http::normalize_cookies( array( $cookie ) );
     729
     730                $this->assertSame( array(), iterator_to_array( $cookie_jar ) );
     731        }
     732
     733        /**
     734         * Tests that {@see WP_Http::normalize_cookies()} sends a cookie with a scalar value as a string.
     735         *
     736         * @covers WP_Http::normalize_cookies
     737         */
     738        public function test_normalize_cookies_casts_scalar_cookie_value_to_string(): void {
     739                $cookie = new WP_Http_Cookie(
     740                        array(
     741                                'name'  => 'count',
     742                                'value' => 5,
     743                        )
     744                );
     745
     746                $cookie_jar = WP_Http::normalize_cookies( array( $cookie ) );
     747
     748                $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar['count'] );
     749                $this->assertSame( '5', $cookie_jar['count']->value );
     750        }
     751
     752        /**
     753         * Tests that {@see WP_Http::normalize_cookies()} sends a cookie with an empty value.
     754         *
     755         * @covers WP_Http::normalize_cookies
     756         */
     757        public function test_normalize_cookies_sends_cookie_with_empty_value(): void {
     758                $cookie = new WP_Http_Cookie(
     759                        array(
     760                                'name'  => 'flavor',
     761                                'value' => '',
     762                        )
     763                );
     764                $this->assertSame( 'flavor=', $cookie->getHeaderValue(), 'Expected the cookie to be in the header value.' );
     765
     766                $cookie_jar = WP_Http::normalize_cookies( array( $cookie ) );
     767
     768                $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar['flavor'] );
     769                $this->assertSame( 'flavor=', $cookie_jar['flavor']->format_for_header() );
     770        }
    699771}
Note: See TracChangeset for help on using the changeset viewer.