From ef2ba3e4f0f20803f43ec2e0e792e7ccfc8fbb8a Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 06:58:46 +0900 Subject: [PATCH 1/6] refactor: fix TypeError in ShowTableInfo::setYesOrNo() 1) CodeIgniter\Commands\Database\ShowTableInfoTest::testDbTableMetadata TypeError: CodeIgniter\Commands\Database\ShowTableInfo::setYesOrNo(): Argument #1 ($fieldValue) must be of type bool, int given, called in /home/runner/work/CodeIgniter4/CodeIgniter4/system/Commands/Database/ShowTableInfo.php on line 286 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Commands/Database/ShowTableInfo.php:297 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Commands/Database/ShowTableInfo.php:286 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Commands/Database/ShowTableInfo.php:144 /home/runner/work/CodeIgniter4/CodeIgniter4/system/CLI/Commands.php:67 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Common.php:198 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Commands/Database/ShowTableInfoTest.php:98 --- system/Commands/Database/ShowTableInfo.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/system/Commands/Database/ShowTableInfo.php b/system/Commands/Database/ShowTableInfo.php index c5b67407b17b..736c7ab994dd 100644 --- a/system/Commands/Database/ShowTableInfo.php +++ b/system/Commands/Database/ShowTableInfo.php @@ -292,9 +292,12 @@ private function showFieldMetaData(string $tableName): void CLI::table($this->tbody, $thead); } - private function setYesOrNo(bool $fieldValue): string + /** + * @param bool|int|string|null $fieldValue + */ + private function setYesOrNo($fieldValue): string { - if ($fieldValue) { + if ((bool) $fieldValue) { return CLI::color('Yes', 'green'); } From 0ee570ce950dd632beb81b6dc568a6b0fa5e4e84 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 11:07:33 +0900 Subject: [PATCH 2/6] refactor: fix TypeError in BaseConnection 1) CodeIgniter\Models\InsertModelTest::testInsertPermitInsertNoData TypeError: strcspn(): Argument #1 ($string) must be of type string, int given /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseConnection.php:1042 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseConnection.php:1029 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Model.php:308 /home/runner/work/CodeIgniter4/CodeIgniter4/system/BaseModel.php:782 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Model.php:707 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Models/InsertModelTest.php:255 --- system/Database/BaseConnection.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index 6d92965f0b94..eac85ef56d50 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -1006,10 +1006,10 @@ public function getConnectDuration(int $decimals = 6): string * insert the table prefix (if it exists) in the proper position, and escape only * the correct identifiers. * - * @param array|string $item - * @param bool $prefixSingle Prefix a table name with no segments? - * @param bool $protectIdentifiers Protect table or column names? - * @param bool $fieldExists Supplied $item contains a column name? + * @param array|int|string $item + * @param bool $prefixSingle Prefix a table name with no segments? + * @param bool $protectIdentifiers Protect table or column names? + * @param bool $fieldExists Supplied $item contains a column name? * * @return array|string * @phpstan-return ($item is array ? array : string) @@ -1030,6 +1030,9 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro return $escapedArray; } + // If you pass `['column1', 'column2']`, `$item` will be int because the array keys are int. + $item = (string) $item; + // This is basically a bug fix for queries that use MAX, MIN, etc. // If a parenthesis is found we know that we do not need to // escape the data or add a prefix. There's probably a more graceful From b7deefa85f075b7e16e62be04c04b3087890025d Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 11:11:13 +0900 Subject: [PATCH 3/6] refactor: fix TypeError in BaseUtils 1) CodeIgniter\Database\Live\DbUtilsTest::testUtilsCSVFromResult TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, int given /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseUtils.php:217 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/DbUtilsTest.php:186 2) CodeIgniter\Database\Live\DbUtilsTest::testUtilsXMLFromResult TypeError: xml_convert(): Argument #1 ($str) must be of type string, int given, called in /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseUtils.php on line 249 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Helpers/xml_helper.php:20 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseUtils.php:249 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/DbUtilsTest.php:199 --- system/Database/BaseUtils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Database/BaseUtils.php b/system/Database/BaseUtils.php index d26bde154e02..cfe25886c302 100644 --- a/system/Database/BaseUtils.php +++ b/system/Database/BaseUtils.php @@ -212,7 +212,7 @@ public function getCSVFromResult(ResultInterface $query, string $delim = ',', st $line = []; foreach ($row as $item) { - $line[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $item ?? '') . $enclosure; + $line[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, (string) $item) . $enclosure; } $out .= implode($delim, $line) . $newline; @@ -244,7 +244,7 @@ public function getXMLFromResult(ResultInterface $query, array $params = []): st $xml .= $tab . '<' . $element . '>' . $newline; foreach ($row as $key => $val) { - $val = (! empty($val)) ? xml_convert($val) : ''; + $val = (! empty($val)) ? xml_convert((string) $val) : ''; $xml .= $tab . $tab . '<' . $key . '>' . $val . '' . $newline; } From 6f70ecadb57318d59947a076ddc83092186fd2bc Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 11:36:56 +0900 Subject: [PATCH 4/6] refactor: fix TypeError in Postgre/Builder.php There were 2 errors: 1) CodeIgniter\Database\Live\UpsertTest::testUpsertWithMatchingDataOnUniqueIndexandPrimaryKey TypeError: strtoupper(): Argument #1 ($string) must be of type string, int given /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/Postgre/Builder.php:364 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseBuilder.php:1790 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseBuilder.php:1927 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/UpsertTest.php:410 2) CodeIgniter\Database\Live\UpsertTest::testUpsertBatchOnPrimaryKey TypeError: strtoupper(): Argument #1 ($string) must be of type string, int given /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/Postgre/Builder.php:364 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseBuilder.php:1790 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseBuilder.php:1961 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/UpsertTest.php:457 -- There was 1 failure: 1) CodeIgniter\Database\Live\UpsertTest::testUpsertCauseConstraintError Failed asserting that exception of type "TypeError" matches expected exception "CodeIgniter\Database\Exceptions\DatabaseException". Message was: "trim(): Argument #1 ($string) must be of type string, CodeIgniter\Database\RawSql given" at /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/Postgre/Builder.php:360 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseBuilder.php:1790 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/BaseBuilder.php:1927 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/UpsertTest.php:303 --- system/Database/Postgre/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index e9763089a90f..d4be734b5bb0 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -355,11 +355,11 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri // autoincrement identity field must use DEFAULT and not NULL // this could be removed in favour of leaving to developer but does make things easier and function like other DBMS foreach ($constraints as $constraint) { - $key = array_search(trim($constraint, '"'), $fieldNames, true); + $key = array_search(trim((string) $constraint, '"'), $fieldNames, true); if ($key !== false) { foreach ($values as $arrayKey => $value) { - if (strtoupper($value[$key]) === 'NULL') { + if (strtoupper((string) $value[$key]) === 'NULL') { $values[$arrayKey][$key] = 'DEFAULT'; } } From 54d7c6519fdffa900bf6588db9e5e84f5c496ee3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 11:58:32 +0900 Subject: [PATCH 5/6] refactor: fix TypeError in SQLSRV/Connection.php 3) CodeIgniter\Database\Live\FabricatorLiveTest::testCreateAddsToDatabase TypeError: CodeIgniter\Database\SQLSRV\Connection::insertID(): Return value must be of type int, string returned /home/runner/work/CodeIgniter4/CodeIgniter4/system/Database/SQLSRV/Connection.php:195 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Model.php:328 /home/runner/work/CodeIgniter4/CodeIgniter4/system/BaseModel.php:782 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Model.php:707 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Test/Fabricator.php:475 /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/Live/FabricatorLiveTest.php:41 --- system/Database/SQLSRV/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/SQLSRV/Connection.php b/system/Database/SQLSRV/Connection.php index f1a9fb0b433a..9e00329b72ce 100755 --- a/system/Database/SQLSRV/Connection.php +++ b/system/Database/SQLSRV/Connection.php @@ -190,7 +190,7 @@ protected function _escapeString(string $str): string */ public function insertID(): int { - return $this->query('SELECT SCOPE_IDENTITY() AS insert_id')->getRow()->insert_id ?? 0; + return (int) ($this->query('SELECT SCOPE_IDENTITY() AS insert_id')->getRow()->insert_id ?? 0); } /** From 18582ccc78ba7b7057ed98288b49caee93d84cfa Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 12:06:22 +0900 Subject: [PATCH 6/6] style: break long line --- system/Database/BaseUtils.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/system/Database/BaseUtils.php b/system/Database/BaseUtils.php index cfe25886c302..a3d878ad7f3c 100644 --- a/system/Database/BaseUtils.php +++ b/system/Database/BaseUtils.php @@ -212,7 +212,11 @@ public function getCSVFromResult(ResultInterface $query, string $delim = ',', st $line = []; foreach ($row as $item) { - $line[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, (string) $item) . $enclosure; + $line[] = $enclosure . str_replace( + $enclosure, + $enclosure . $enclosure, + (string) $item + ) . $enclosure; } $out .= implode($delim, $line) . $newline;