Skip to content

Commit

Permalink
[PHP 8.4] Fixes for implicit nullability deprecation (#865)
Browse files Browse the repository at this point in the history
Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
  • Loading branch information
jrfnl committed Mar 25, 2024
1 parent b4be8a2 commit 984a4ae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,19 @@ public static function parse($cookie_header, $name = '', $reference_time = null)
* @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins
* @param int|null $time Reference time for expiration calculation
* @return array
*
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $origin argument is not null or an instance of the Iri class.
*/
public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) {
public static function parse_from_headers(Headers $headers, $origin = null, $time = null) {
$cookie_headers = $headers->getValues('Set-Cookie');
if (empty($cookie_headers)) {
return [];
}

if ($origin !== null && !($origin instanceof Iri)) {
throw InvalidArgument::create(2, '$origin', Iri::class . ' or null', gettype($origin));
}

$cookies = [];
foreach ($cookie_headers as $header) {
$parsed = self::parse($header, '', $time);
Expand Down
31 changes: 31 additions & 0 deletions tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,4 +807,35 @@ public function testParseFromHeadersInvalidReferenceTime() {

Cookie::parse_from_headers($headers, $origin, 'now');
}

/**
* Verify parsing of cookies fails with an exception if the $origin parameter is passed anything but `null`
* or an instance of Iri.
*
* @dataProvider dataParseFromHeadersInvalidOrigin
*
* @covers ::parse_from_headers
*
* @param mixed $input Invalid parameter input.
*
* @return void
*/
public function testParseFromHeadersInvalidOrigin($input) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #2 ($origin) must be of type WpOrg\Requests\Iri or null');

$headers = new Headers();
$headers['Set-Cookie'] = 'name=value';

Cookie::parse_from_headers($headers, $input);
}

/**
* Data Provider.
*
* @return array
*/
public static function dataParseFromHeadersInvalidOrigin() {
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_NULL);
}
}

0 comments on commit 984a4ae

Please sign in to comment.