Skip to content

Commit fb0ea4b

Browse files
authored
refactor: Update phpDoc typing (#9661)
* refactor: Update phpDoc typing * fix: Apply string type for `samesite` Cookie
1 parent d45342a commit fb0ea4b

File tree

6 files changed

+69
-71
lines changed

6 files changed

+69
-71
lines changed

system/Common.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,19 @@ function config(string $name, bool $getShared = true)
216216
/**
217217
* Simpler way to create a new Cookie instance.
218218
*
219-
* @param string $name Name of the cookie
220-
* @param string $value Value of the cookie
221-
* @param array $options Array of options to be passed to the cookie
219+
* @param string $name Name of the cookie
220+
* @param string $value Value of the cookie
221+
* @param array{
222+
* prefix?: string,
223+
* max-age?: int|numeric-string,
224+
* expires?: DateTimeInterface|int|string,
225+
* path?: string,
226+
* domain?: string,
227+
* secure?: bool,
228+
* httponly?: bool,
229+
* samesite?: string,
230+
* raw?: bool
231+
* } $options Cookie configuration options
222232
*
223233
* @throws CookieException
224234
*/
@@ -352,7 +362,27 @@ function csp_script_nonce(): string
352362
* If $getShared === false then a new connection instance will be provided,
353363
* otherwise it will all calls will return the same instance.
354364
*
355-
* @param array|ConnectionInterface|string|null $db
365+
* @param array{
366+
* DSN?: string,
367+
* hostname?: string,
368+
* username?: string,
369+
* password?: string,
370+
* database?: string,
371+
* DBDriver?: 'MySQLi'|'OCI8'|'Postgre'|'SQLite3'|'SQLSRV',
372+
* DBPrefix?: string,
373+
* pConnect?: bool,
374+
* DBDebug?: bool,
375+
* charset?: string,
376+
* DBCollat?: string,
377+
* swapPre?: string,
378+
* encrypt?: bool,
379+
* compress?: bool,
380+
* strictOn?: bool,
381+
* failover?: array<string, mixed>,
382+
* port?: int,
383+
* dateFormat?: array<string, string>,
384+
* foreignKeys?: bool
385+
* }|ConnectionInterface|string|null $db
356386
*
357387
* @return BaseConnection
358388
*/
@@ -402,13 +432,13 @@ function env(string $key, $default = null)
402432
* If $data is an array, then it loops over it, escaping each
403433
* 'value' of the key/value pairs.
404434
*
405-
* @param array|string $data
406-
* @param 'attr'|'css'|'html'|'js'|'raw'|'url' $context
407-
* @param string|null $encoding Current encoding for escaping.
408-
* If not UTF-8, we convert strings from this encoding
409-
* pre-escaping and back to this encoding post-escaping.
435+
* @param array<int|string, array<int|string, mixed>|string>|string $data
436+
* @param 'attr'|'css'|'html'|'js'|'raw'|'url' $context
437+
* @param string|null $encoding Current encoding for escaping.
438+
* If not UTF-8, we convert strings from this encoding
439+
* pre-escaping and back to this encoding post-escaping.
410440
*
411-
* @return array|string
441+
* @return ($data is string ? string : array<int|string, array<int|string, mixed>|string>)
412442
*
413443
* @throws InvalidArgumentException
414444
*/
@@ -560,7 +590,7 @@ function function_usable(string $functionName): bool
560590
* 2. {namespace}/Helpers
561591
* 3. system/Helpers
562592
*
563-
* @param array|string $filenames
593+
* @param list<string>|string $filenames
564594
*
565595
* @throws FileNotFoundException
566596
*/

system/Helpers/cookie_helper.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,29 @@
2424
*
2525
* Accepts seven parameters, or you can submit an associative
2626
* array in the first parameter containing all the values.
27+
* ['', 'value', 'expire', '', '', '', '', '', 'name']
2728
*
28-
* @param array|Cookie|string $name Cookie name / array containing binds / Cookie object
29-
* @param string $value The value of the cookie
30-
* @param int $expire The number of seconds until expiration
31-
* @param string $domain For site-wide cookie. Usually: .yourdomain.com
32-
* @param string $path The cookie path
33-
* @param string $prefix The cookie prefix ('': the default prefix)
34-
* @param bool|null $secure True makes the cookie secure
35-
* @param bool|null $httpOnly True makes the cookie accessible via http(s) only (no javascript)
36-
* @param string|null $sameSite The cookie SameSite value
29+
* @param array{
30+
* name?: string,
31+
* value?: string,
32+
* prefix?: string,
33+
* max-age?: int|numeric-string,
34+
* expire?: DateTimeInterface|int|string,
35+
* path?: string,
36+
* domain?: string,
37+
* secure?: bool,
38+
* httponly?: bool,
39+
* samesite?: string,
40+
* raw?: bool
41+
* }|Cookie|string $name Cookie name / array containing binds / Cookie object
42+
* @param string $value The value of the cookie
43+
* @param int $expire The number of seconds until expiration
44+
* @param string $domain For site-wide cookie. Usually: .yourdomain.com
45+
* @param string $path The cookie path
46+
* @param string $prefix The cookie prefix ('': the default prefix)
47+
* @param bool|null $secure True makes the cookie secure
48+
* @param bool|null $httpOnly True makes the cookie accessible via http(s) only (no javascript)
49+
* @param string|null $sameSite The cookie SameSite value
3750
*
3851
* @see \CodeIgniter\HTTP\Response::setCookie()
3952
*/
@@ -62,7 +75,7 @@ function set_cookie(
6275
* '': the prefix in Config\Cookie
6376
* null: no prefix
6477
*
65-
* @return array|string|null
78+
* @return array<string, mixed>|string|null
6679
*
6780
* @see \CodeIgniter\HTTP\IncomingRequest::getCookie()
6881
*/

tests/system/CommonFunctionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ public function testEscapeWithDifferentEncodings(): void
243243
public function testEscapeBadContext(): void
244244
{
245245
$this->expectException('InvalidArgumentException');
246-
esc(['width' => '800', 'height' => '600'], 'bogus');
246+
esc(['width' => '800', 'height' => '600'], 'bogus'); // @phpstan-ignore argument.type
247247
}
248248

249249
public function testEscapeBadContextZero(): void
250250
{
251251
$this->expectException('InvalidArgumentException');
252-
esc('<script>', '0');
252+
esc('<script>', '0'); // @phpstan-ignore argument.type
253253
}
254254

255255
public function testEscapeArray(): void

utils/phpstan-baseline/argument.type.neon

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 90 errors
1+
# total 88 errors
22

33
parameters:
44
ignoreErrors:
@@ -52,16 +52,6 @@ parameters:
5252
count: 4
5353
path: ../../tests/system/Commands/RoutesTest.php
5454

55-
-
56-
message: '#^Parameter \#2 \$context of function esc expects ''attr''\|''css''\|''html''\|''js''\|''raw''\|''url'', ''0'' given\.$#'
57-
count: 1
58-
path: ../../tests/system/CommonFunctionsTest.php
59-
60-
-
61-
message: '#^Parameter \#2 \$context of function esc expects ''attr''\|''css''\|''html''\|''js''\|''raw''\|''url'', ''bogus'' given\.$#'
62-
count: 1
63-
path: ../../tests/system/CommonFunctionsTest.php
64-
6555
-
6656
message: '#^Parameter \#2 \$file of class CodeIgniter\\Config\\DotEnv constructor expects string, int given\.$#'
6757
count: 1

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2853 errors
1+
# total 2844 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/missingType.iterableValue.neon

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1423 errors
1+
# total 1416 errors
22

33
parameters:
44
ignoreErrors:
@@ -342,31 +342,6 @@ parameters:
342342
count: 1
343343
path: ../../system/Common.php
344344

345-
-
346-
message: '#^Function cookie\(\) has parameter \$options with no value type specified in iterable type array\.$#'
347-
count: 1
348-
path: ../../system/Common.php
349-
350-
-
351-
message: '#^Function db_connect\(\) has parameter \$db with no value type specified in iterable type array\.$#'
352-
count: 1
353-
path: ../../system/Common.php
354-
355-
-
356-
message: '#^Function esc\(\) has parameter \$data with no value type specified in iterable type array\.$#'
357-
count: 1
358-
path: ../../system/Common.php
359-
360-
-
361-
message: '#^Function esc\(\) return type has no value type specified in iterable type array\.$#'
362-
count: 1
363-
path: ../../system/Common.php
364-
365-
-
366-
message: '#^Function helper\(\) has parameter \$filenames with no value type specified in iterable type array\.$#'
367-
count: 1
368-
path: ../../system/Common.php
369-
370345
-
371346
message: '#^Function log_message\(\) has parameter \$context with no value type specified in iterable type array\.$#'
372347
count: 1
@@ -3732,16 +3707,6 @@ parameters:
37323707
count: 1
37333708
path: ../../system/Helpers/array_helper.php
37343709

3735-
-
3736-
message: '#^Function get_cookie\(\) return type has no value type specified in iterable type array\.$#'
3737-
count: 1
3738-
path: ../../system/Helpers/cookie_helper.php
3739-
3740-
-
3741-
message: '#^Function set_cookie\(\) has parameter \$name with no value type specified in iterable type array\.$#'
3742-
count: 1
3743-
path: ../../system/Helpers/cookie_helper.php
3744-
37453710
-
37463711
message: '#^Function directory_map\(\) return type has no value type specified in iterable type array\.$#'
37473712
count: 1

0 commit comments

Comments
 (0)