Skip to content

Commit

Permalink
Merge pull request #8270 from kenjis/fix-TypeError-in-strict-mode
Browse files Browse the repository at this point in the history
refactor: fix TypeError in strict mode
  • Loading branch information
kenjis authored Nov 30, 2023
2 parents 9208a60 + 18582cc commit 8584f1b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
7 changes: 5 additions & 2 deletions system/Commands/Database/ShowTableInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
11 changes: 7 additions & 4 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions system/Database/BaseUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ 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;
Expand Down Expand Up @@ -244,7 +248,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 . '</' . $key . '>' . $newline;
}
Expand Down
4 changes: 2 additions & 2 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 8584f1b

Please sign in to comment.