Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP 8.4] Fixes for implicit nullability deprecation #865

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,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/Cookie/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ public function testParseInvalidReferenceTime() {
Cookie::parse('test', 'test', '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);
}

/**
* Tests receiving an exception when the parse_from_headers() method received an invalid input type as `$reference_time`.
*
Expand Down