Skip to content

Commit 4ac163d

Browse files
committed
fix: $db->escape() does not accept Stringable
1 parent 393181b commit 4ac163d

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

system/Database/BaseConnection.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
1818
use CodeIgniter\Events\Events;
1919
use stdClass;
20+
use Stringable;
2021
use Throwable;
2122

2223
/**
@@ -1309,12 +1310,15 @@ public function escape($str)
13091310
return array_map($this->escape(...), $str);
13101311
}
13111312

1312-
/** @psalm-suppress NoValue I don't know why ERROR. */
1313-
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
1313+
if ($str instanceof Stringable) {
13141314
if ($str instanceof RawSql) {
13151315
return $str->__toString();
13161316
}
13171317

1318+
$str = (string) $str;
1319+
}
1320+
1321+
if (is_string($str)) {
13181322
return "'" . $this->escapeString($str) . "'";
13191323
}
13201324

system/Database/Postgre/Connection.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PgSql\Connection as PgSqlConnection;
2121
use PgSql\Result as PgSqlResult;
2222
use stdClass;
23+
use Stringable;
2324

2425
/**
2526
* Connection for Postgre
@@ -233,20 +234,22 @@ public function escape($str)
233234
$this->initialize();
234235
}
235236

236-
/** @psalm-suppress NoValue I don't know why ERROR. */
237-
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
237+
if ($str instanceof Stringable) {
238238
if ($str instanceof RawSql) {
239239
return $str->__toString();
240240
}
241241

242+
$str = (string) $str;
243+
}
244+
245+
if (is_string($str)) {
242246
return pg_escape_literal($this->connID, $str);
243247
}
244248

245249
if (is_bool($str)) {
246250
return $str ? 'TRUE' : 'FALSE';
247251
}
248252

249-
/** @psalm-suppress NoValue I don't know why ERROR. */
250253
return parent::escape($str);
251254
}
252255

0 commit comments

Comments
 (0)