Skip to content

refactor: more accurate array PHPDocs of Cookie #9569

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

Merged
merged 1 commit into from
May 19, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions system/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ class Cookie implements ArrayAccess, CloneableCookieInterface
* Default attributes for a Cookie object. The keys here are the
* lowercase attribute names. Do not camelCase!
*
* @var array<string, bool|int|string>
* @var array{
* prefix: string,
* expires: int,
* path: string,
* domain: string,
* secure: bool,
* httponly: bool,
* samesite: string,
* raw: bool,
* }
*/
private static array $defaults = [
'prefix' => '',
Expand Down Expand Up @@ -127,9 +136,27 @@ class Cookie implements ArrayAccess, CloneableCookieInterface
*
* This method is called from Response::__construct().
*
* @param array<string, bool|int|string>|CookieConfig $config
* @param array{
* prefix?: string,
* expires?: int,
* path?: string,
* domain?: string,
* secure?: bool,
* httponly?: bool,
* samesite?: string,
* raw?: bool,
* }|CookieConfig $config
*
* @return array<string, mixed> The old defaults array. Useful for resetting.
* @return array{
* prefix: string,
* expires: int,
* path: string,
* domain: string,
* secure: bool,
* httponly: bool,
* samesite: string,
* raw: bool,
* } The old defaults array. Useful for resetting.
*/
public static function setDefaults($config = [])
{
Expand Down Expand Up @@ -198,9 +225,9 @@ public static function fromHeaderString(string $cookie, bool $raw = false)
/**
* Construct a new Cookie instance.
*
* @param string $name The cookie's name
* @param string $value The cookie's value
* @param array<string, bool|int|string> $options The cookie's options
* @param string $name The cookie's name
* @param string $value The cookie's value
* @param array{prefix?: string, max-age?: int|numeric-string, expires?: DateTimeInterface|int|string, path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: string, raw?: bool} $options The cookie's options
*
* @throws CookieException
*/
Expand Down
22 changes: 20 additions & 2 deletions system/Cookie/CookieInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ public function isRaw(): bool;
* Gets the options that are passable to the `setcookie` variant
* available on PHP 7.3+
*
* @return array<string, bool|int|string>
* @return array{
* expires: int,
* path: string,
* domain: string,
* secure: bool,
* httponly: bool,
* samesite: string,
* }
*/
public function getOptions(): array;

Expand All @@ -164,7 +171,18 @@ public function __toString();
/**
* Returns the array representation of the Cookie object.
*
* @return array<string, bool|int|string>
* @return array{
* name: string,
* value: string,
* prefix: string,
* raw: bool,
* expires: int,
* path: string,
* domain: string,
* secure: bool,
* httponly: bool,
* samesite: string,
* }
*/
public function toArray(): array;
}
2 changes: 1 addition & 1 deletion tests/system/Cookie/CookieStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testCookieStoreInitViaHeaders(): void
public function testInvalidCookieStored(): void
{
$this->expectException(CookieException::class);
new CookieStore([new DateTimeImmutable('now')]);
new CookieStore([new DateTimeImmutable('now')]); // @phpstan-ignore argument.type
}

public function testPutRemoveCookiesInStore(): void
Expand Down
12 changes: 1 addition & 11 deletions utils/phpstan-baseline/argument.type.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 138 errors
# total 136 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -82,16 +82,6 @@ parameters:
count: 2
path: ../../tests/system/Config/FactoriesTest.php

-
message: '#^Parameter \#1 \$cookies of class CodeIgniter\\Cookie\\CookieStore constructor expects array\<CodeIgniter\\Cookie\\Cookie\>, array\<int, DateTimeImmutable\> given\.$#'
count: 1
path: ../../tests/system/Cookie/CookieStoreTest.php

-
message: '#^Parameter \#3 \$options of class CodeIgniter\\Cookie\\Cookie constructor expects array\<string, bool\|int\|string\>, array\<string, DateTimeImmutable\> given\.$#'
count: 1
path: ../../tests/system/Cookie/CookieTest.php

-
message: '#^Parameter \#1 \$from of method CodeIgniter\\Database\\BaseBuilder\:\:from\(\) expects array\|string, null given\.$#'
count: 1
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 3273 errors
# total 3271 errors
includes:
- argument.type.neon
- assign.propertyType.neon
Expand Down
Loading