From 32aaf0c545ae023acc8a05df23c8cc8a7f88ec6b Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 3 Nov 2023 13:41:22 +0900 Subject: [PATCH 1/9] refactor: fix parameter types --- system/CLI/CLI.php | 8 ++++---- system/Commands/Database/MigrateStatus.php | 2 +- system/Session/Handlers/RedisHandler.php | 2 +- system/Test/CIUnitTestCase.php | 4 ++-- system/Validation/Validation.php | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index 27ea440703cc..86017c2bcb48 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -213,9 +213,9 @@ public static function input(?string $prefix = null): string * // Do not provide options but requires a valid email * $email = CLI::prompt('What is your email?', null, 'required|valid_email'); * - * @param string $field Output "field" question - * @param array|string $options String to a default value, array to a list of options (the first option will be the default value) - * @param array|string|null $validation Validation rules + * @param string $field Output "field" question + * @param list|string $options String to a default value, array to a list of options (the first option will be the default value) + * @param array|string|null $validation Validation rules * * @return string The user input */ @@ -239,7 +239,7 @@ public static function prompt(string $field, $options = null, $validation = null if (is_array($options) && $options) { $opts = $options; - $extraOutputDefault = static::color($opts[0], 'green'); + $extraOutputDefault = static::color((string) $opts[0], 'green'); unset($opts[0]); diff --git a/system/Commands/Database/MigrateStatus.php b/system/Commands/Database/MigrateStatus.php index af3cbacf772c..cc7fe7d1ef02 100644 --- a/system/Commands/Database/MigrateStatus.php +++ b/system/Commands/Database/MigrateStatus.php @@ -115,7 +115,7 @@ public function run(array $params) ksort($migrations); foreach ($migrations as $uid => $migration) { - $migrations[$uid]->name = mb_substr($migration->name, mb_strpos($migration->name, $uid . '_')); + $migrations[$uid]->name = mb_substr($migration->name, (int) mb_strpos($migration->name, $uid . '_')); $date = '---'; $group = '---'; diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index 1fb87597ab8a..c52693dec2cc 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -124,7 +124,7 @@ public function open($path, $name): bool $redis = new Redis(); - if (! $redis->connect($this->savePath['protocol'] . '://' . $this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : $this->savePath['port']), $this->savePath['timeout'])) { + if (! $redis->connect($this->savePath['protocol'] . '://' . $this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : (int) $this->savePath['port']), $this->savePath['timeout'])) { $this->logger->error('Session: Unable to connect to Redis with the configured settings.'); } elseif (isset($this->savePath['password']) && ! $redis->auth($this->savePath['password'])) { $this->logger->error('Session: Unable to authenticate to Redis instance.'); diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index 4e889b5ea1d0..f004d43beba4 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -439,13 +439,13 @@ public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false) * where the result is close but not exactly equal to the * expected time, for reasons beyond our control. * - * @param mixed $actual + * @param float|int $actual * * @throws Exception */ public function assertCloseEnough(int $expected, $actual, string $message = '', int $tolerance = 1) { - $difference = abs($expected - (int) floor($actual)); + $difference = abs($expected - (int) floor((float) $actual)); $this->assertLessThanOrEqual($tolerance, $difference, $message); } diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index d14986164e63..747ac66d9bc4 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -806,7 +806,7 @@ protected function fillPlaceholders(array $rules, array $data): array } // Replace the placeholder in the rule - $ruleSet = str_replace('{' . $field . '}', $data[$field], $ruleSet); + $ruleSet = str_replace('{' . $field . '}', (string) $data[$field], $ruleSet); } } } From 87701b393abd985d3f1e1ceb5d9bc286210ffd15 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 3 Nov 2023 13:41:34 +0900 Subject: [PATCH 2/9] refactor: fix return type --- system/Database/MigrationRunner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 241a9e6da5fa..ef598b980628 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -752,7 +752,7 @@ public function getBatchEnd(int $batch): string ->get() ->getResultObject(); - return count($migration) ? $migration[0]->version : 0; + return count($migration) ? $migration[0]->version : '0'; } /** From f39d0e13bdafee678c256cb8cbc909c1028aaa09 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 3 Nov 2023 13:46:21 +0900 Subject: [PATCH 3/9] style: break long line --- system/Session/Handlers/RedisHandler.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index c52693dec2cc..994bb1a882b7 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -124,7 +124,13 @@ public function open($path, $name): bool $redis = new Redis(); - if (! $redis->connect($this->savePath['protocol'] . '://' . $this->savePath['host'], ($this->savePath['host'][0] === '/' ? 0 : (int) $this->savePath['port']), $this->savePath['timeout'])) { + if ( + ! $redis->connect( + $this->savePath['protocol'] . '://' . $this->savePath['host'], + ($this->savePath['host'][0] === '/' ? 0 : (int) $this->savePath['port']), + $this->savePath['timeout'] + ) + ) { $this->logger->error('Session: Unable to connect to Redis with the configured settings.'); } elseif (isset($this->savePath['password']) && ! $redis->auth($this->savePath['password'])) { $this->logger->error('Session: Unable to authenticate to Redis instance.'); From 98eeb95dc00b4f27a0174cb7df48f112d8001a3a Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 3 Nov 2023 13:47:36 +0900 Subject: [PATCH 4/9] refactor: move () positions --- system/Session/Handlers/RedisHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index 994bb1a882b7..690860c6175e 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -127,7 +127,7 @@ public function open($path, $name): bool if ( ! $redis->connect( $this->savePath['protocol'] . '://' . $this->savePath['host'], - ($this->savePath['host'][0] === '/' ? 0 : (int) $this->savePath['port']), + ($this->savePath['host'][0] === '/') ? 0 : (int) $this->savePath['port'], $this->savePath['timeout'] ) ) { From b03638055ccd13d3abea07de8726f1c6ab82fe21 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 3 Nov 2023 13:56:12 +0900 Subject: [PATCH 5/9] refactor: replace non boolean if condition --- system/CLI/CLI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index 86017c2bcb48..c6bcb3557590 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -237,7 +237,7 @@ public static function prompt(string $field, $options = null, $validation = null $default = $options; } - if (is_array($options) && $options) { + if (is_array($options) && $options !== []) { $opts = $options; $extraOutputDefault = static::color((string) $opts[0], 'green'); From 05feaaf0f3697f8a404f359244ddd417736cf019 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 3 Nov 2023 13:57:48 +0900 Subject: [PATCH 6/9] chore: update phpstan-baseline.php --- phpstan-baseline.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 11487806e48d..29ff1c50cc09 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -96,11 +96,6 @@ 'count' => 6, 'path' => __DIR__ . '/system/CLI/CLI.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Only booleans are allowed in &&, array given on the right side\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/CLI/CLI.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in &&, array\\ given on the right side\\.$#', 'count' => 1, From 64a2f5b0a8b44f06e3cf23ec6ed922e429a4f5ee Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 6 Nov 2023 09:40:24 +0900 Subject: [PATCH 7/9] refactor: replace count() Co-authored-by: MGatner --- system/Database/MigrationRunner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index ef598b980628..1df8aaf2e80f 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -752,7 +752,7 @@ public function getBatchEnd(int $batch): string ->get() ->getResultObject(); - return count($migration) ? $migration[0]->version : '0'; + return $migration === [] ? '0' : $migration[0]->version; } /** From d51e6a800dc12bcf4a063616dc8e26e3d698b0bd Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 6 Nov 2023 09:47:59 +0900 Subject: [PATCH 8/9] fix: remove unneeded (float) --- system/Test/CIUnitTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index f004d43beba4..266a22b0161d 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -445,7 +445,7 @@ public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false) */ public function assertCloseEnough(int $expected, $actual, string $message = '', int $tolerance = 1) { - $difference = abs($expected - (int) floor((float) $actual)); + $difference = abs($expected - (int) floor($actual)); $this->assertLessThanOrEqual($tolerance, $difference, $message); } From 966b717935060dcf0733100a38534792ea618d81 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 6 Nov 2023 09:58:29 +0900 Subject: [PATCH 9/9] chore: update phpstan-baseline.php --- phpstan-baseline.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 29ff1c50cc09..8c5d7a06625e 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -1153,7 +1153,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, max\\> given\\.$#', - 'count' => 4, + 'count' => 3, 'path' => __DIR__ . '/system/Database/MigrationRunner.php', ]; $ignoreErrors[] = [