From 974371a1e98ac5da28f65d188e0c31707d2d7cbd Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 29 Aug 2026 19:46:13 +0800 Subject: [PATCH] refactor: fix remaining `argument.type` and `method.childParameterType` errors --- system/Cookie/Cookie.php | 2 +- system/I18n/TimeTrait.php | 8 +++-- system/Log/Handlers/ChromeLoggerHandler.php | 4 +-- tests/system/CodeIgniterTest.php | 23 ++++++------- .../system/RESTful/ResourceControllerTest.php | 2 +- utils/phpstan-baseline/argument.type.neon | 33 ------------------- utils/phpstan-baseline/loader.neon | 4 +-- .../method.childParameterType.neon | 28 ---------------- 8 files changed, 21 insertions(+), 83 deletions(-) delete mode 100644 utils/phpstan-baseline/argument.type.neon delete mode 100644 utils/phpstan-baseline/method.childParameterType.neon diff --git a/system/Cookie/Cookie.php b/system/Cookie/Cookie.php index 61de7d6ce672..b807e0885df4 100644 --- a/system/Cookie/Cookie.php +++ b/system/Cookie/Cookie.php @@ -619,7 +619,7 @@ public function offsetGet($offset): bool|int|string /** * Offset to set. * - * @param string $offset + * @param string|null $offset * @param bool|int|string $value * * @throws LogicException diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index 588162b6f093..0660724f5019 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -1239,10 +1239,14 @@ public function __isset($name): bool /** * This is called when we unserialize the Time object. * - * @param array{date: string, timezone: string, timezone_type: int} $data + * @param array $data */ public function __unserialize(array $data): void { - parent::__construct($data['date'], new DateTimeZone($data['timezone'])); + $date = $data['date'] ?? null; + $timezone = $data['timezone'] ?? null; + assert(is_string($date) && is_string($timezone)); + + parent::__construct($date, new DateTimeZone($timezone)); } } diff --git a/system/Log/Handlers/ChromeLoggerHandler.php b/system/Log/Handlers/ChromeLoggerHandler.php index 8d763399b513..ce8e97a12750 100644 --- a/system/Log/Handlers/ChromeLoggerHandler.php +++ b/system/Log/Handlers/ChromeLoggerHandler.php @@ -99,8 +99,8 @@ public function __construct(array $config = []) * will stop. Any handlers that have not run, yet, will not * be run. * - * @param string $level - * @param string $message + * @param string $level + * @param object|string $message */ public function handle($level, $message): bool { diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index bf6321d9d1fd..c5b3863ee203 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -119,7 +119,7 @@ public function testRunClosureRoute(): void // Inject mock router. $routes = service('routes'); - $routes->add('pages/(:segment)', static function ($segment): void { + $routes->add('pages/(:segment)', static function (string $segment = ''): void { echo 'You want to see "' . esc($segment) . '" page.'; }); $router = service('router', $routes, service('incomingrequest')); @@ -231,7 +231,7 @@ public function testControllersCanReturnString(): void $routes = service('routes'); $routes->add( 'pages/(:segment)', - static fn ($segment): string => 'You want to see "' . esc($segment) . '" page.', + static fn (string $segment = ''): string => 'You want to see "' . esc($segment) . '" page.', ); $router = service('router', $routes, service('incomingrequest')); Services::injectMock('router', $router); @@ -253,12 +253,10 @@ public function testControllersCanReturnResponseObject(): void // Inject mock router. $routes = service('routes'); - $routes->add('pages/(:segment)', static function ($segment) { - $response = service('response'); - $string = "You want to see 'about' page."; - - return $response->setBody($string); - }); + $routes->add( + 'pages/(:segment)', + static fn () => service('response')->setBody("You want to see 'about' page."), + ); $router = service('router', $routes, service('incomingrequest')); Services::injectMock('router', $router); @@ -282,11 +280,10 @@ public function testControllersCanReturnDownloadResponseObject(): void // Inject mock router. $routes = service('routes'); - $routes->add('pages/(:segment)', static function ($segment) { - $response = service('response'); - - return $response->download('some.txt', 'some text', true); - }); + $routes->add( + 'pages/(:segment)', + static fn () => service('response')->download('some.txt', 'some text', true), + ); $router = service('router', $routes, service('incomingrequest')); Services::injectMock('router', $router); diff --git a/tests/system/RESTful/ResourceControllerTest.php b/tests/system/RESTful/ResourceControllerTest.php index 70081b5ee3f9..d660cb7c3f9c 100644 --- a/tests/system/RESTful/ResourceControllerTest.php +++ b/tests/system/RESTful/ResourceControllerTest.php @@ -310,7 +310,7 @@ public function testFormat(): void $resource = new MockResourceController(); $this->assertSame('json', $resource->getFormat()); - $resource->setFormat('Nonsense'); + $resource->setFormat('Nonsense'); // @phpstan-ignore argument.type (Testing that an invalid format is silently ignored) $this->assertSame('json', $resource->getFormat()); $resource->setFormat('xml'); diff --git a/utils/phpstan-baseline/argument.type.neon b/utils/phpstan-baseline/argument.type.neon deleted file mode 100644 index b9f5906a8ac3..000000000000 --- a/utils/phpstan-baseline/argument.type.neon +++ /dev/null @@ -1,33 +0,0 @@ -# total 6 errors - -parameters: - ignoreErrors: - - - message: '#^Parameter \#2 \$to of method CodeIgniter\\Router\\RouteCollection\:\:add\(\) expects array\\|\(Closure\(mixed \.\.\.\)\: \(CodeIgniter\\HTTP\\ResponseInterface\|string\|void\)\)\|string, Closure\(mixed\)\: CodeIgniter\\HTTP\\ResponseInterface given\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Parameter \#2 \$to of method CodeIgniter\\Router\\RouteCollection\:\:add\(\) expects array\\|\(Closure\(mixed \.\.\.\)\: \(CodeIgniter\\HTTP\\ResponseInterface\|string\|void\)\)\|string, Closure\(mixed\)\: \(CodeIgniter\\HTTP\\DownloadResponse\|null\) given\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Parameter \#2 \$to of method CodeIgniter\\Router\\RouteCollection\:\:add\(\) expects array\\|\(Closure\(mixed \.\.\.\)\: \(CodeIgniter\\HTTP\\ResponseInterface\|string\|void\)\)\|string, Closure\(mixed\)\: non\-falsy\-string given\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Parameter \#2 \$to of method CodeIgniter\\Router\\RouteCollection\:\:add\(\) expects array\\|\(Closure\(mixed \.\.\.\)\: \(CodeIgniter\\HTTP\\ResponseInterface\|string\|void\)\)\|string, Closure\(mixed\)\: void given\.$#' - count: 1 - path: ../../tests/system/CodeIgniterTest.php - - - - message: '#^Parameter \#2 \$message of method CodeIgniter\\Log\\Handlers\\ChromeLoggerHandler\:\:handle\(\) expects string, stdClass given\.$#' - count: 1 - path: ../../tests/system/Log/Handlers/ChromeLoggerHandlerTest.php - - - - message: '#^Parameter \#1 \$format of method CodeIgniter\\RESTful\\ResourceController\:\:setFormat\(\) expects ''json''\|''xml'', ''Nonsense'' given\.$#' - count: 1 - path: ../../tests/system/RESTful/ResourceControllerTest.php diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 290d2f74e5a8..78ffcf540c8a 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,12 +1,10 @@ -# total 24 errors +# total 13 errors includes: - - argument.type.neon - arguments.count.neon - assign.propertyType.neon - deadCode.unreachable.neon - function.resultUnused.neon - - method.childParameterType.neon - method.childReturnType.neon - missingType.iterableValue.neon - property.defaultValue.neon diff --git a/utils/phpstan-baseline/method.childParameterType.neon b/utils/phpstan-baseline/method.childParameterType.neon deleted file mode 100644 index d237e221ebf5..000000000000 --- a/utils/phpstan-baseline/method.childParameterType.neon +++ /dev/null @@ -1,28 +0,0 @@ -# total 5 errors - -parameters: - ignoreErrors: - - - message: '#^Parameter \#1 \$offset \(string\) of method CodeIgniter\\Cookie\\Cookie\:\:offsetSet\(\) should be contravariant with parameter \$offset \(string\|null\) of method ArrayAccess\\:\:offsetSet\(\)$#' - count: 1 - path: ../../system/Cookie/Cookie.php - - - - message: '#^Parameter \#1 \$data \(array\{date\: string, timezone\: string, timezone_type\: int\}\) of method CodeIgniter\\I18n\\Time\:\:__unserialize\(\) should be contravariant with parameter \$data \(array\) of method DateTimeImmutable\:\:__unserialize\(\)$#' - count: 1 - path: ../../system/I18n/Time.php - - - - message: '#^Parameter \#1 \$data \(array\{date\: string, timezone\: string, timezone_type\: int\}\) of method CodeIgniter\\I18n\\Time\:\:__unserialize\(\) should be contravariant with parameter \$data \(array\) of method DateTimeInterface\:\:__unserialize\(\)$#' - count: 1 - path: ../../system/I18n/Time.php - - - - message: '#^Parameter \#1 \$data \(array\{date\: string, timezone\: string, timezone_type\: int\}\) of method CodeIgniter\\I18n\\TimeLegacy\:\:__unserialize\(\) should be contravariant with parameter \$data \(array\) of method DateTimeInterface\:\:__unserialize\(\)$#' - count: 1 - path: ../../system/I18n/TimeLegacy.php - - - - message: '#^Parameter \#1 \$data \(array\{date\: string, timezone\: string, timezone_type\: int\}\) of method CodeIgniter\\I18n\\TimeLegacy\:\:__unserialize\(\) should be contravariant with parameter \$data \(array\) of method DateTime\:\:__unserialize\(\)$#' - count: 1 - path: ../../system/I18n/TimeLegacy.php