From 67f36b5c094f2e56a91bbf35ce8333a6f664b417 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 09:07:58 +0100 Subject: [PATCH] Document the real types of escape() and quote() and deprecate other input escape() is documented as returning a string but returns integers unchanged, and quote() accepts arrays as well as scalars. The docblocks now state the actual union types. Passing anything else to quote(), such as an object without __toString() or a resource, triggers a deprecation, preparing the native types planned for 5.0. Co-Authored-By: Claude Opus 5.5 --- Tests/Sqlite/QuoteDeprecationTest.php | 58 +++++++++++++++++++++++++++ src/DatabaseDriver.php | 30 ++++++++++++-- src/DatabaseInterface.php | 13 +++--- src/Sqlsrv/SqlsrvDriver.php | 2 + 4 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 Tests/Sqlite/QuoteDeprecationTest.php diff --git a/Tests/Sqlite/QuoteDeprecationTest.php b/Tests/Sqlite/QuoteDeprecationTest.php new file mode 100644 index 00000000..c343a79f --- /dev/null +++ b/Tests/Sqlite/QuoteDeprecationTest.php @@ -0,0 +1,58 @@ + 'sqlite', 'database' => ':memory:']) extends SqliteDriver { + public function escape($text, $extra = false) + { + return (string) $text; + } + }; + + $resource = fopen('php://memory', 'r'); + + $deprecations = []; + + set_error_handler( + function (int $errno, string $errstr) use (&$deprecations) { + $deprecations[] = $errstr; + + return true; + }, + E_USER_DEPRECATED + ); + + try { + $db->quote('text'); + $db->quote(1); + $db->quote(['a', 2]); + $this->assertSame([], $deprecations, 'Scalars and arrays of scalars are fine'); + + $db->quote($resource); + } finally { + restore_error_handler(); + fclose($resource); + } + + $this->assertCount(1, $deprecations); + $this->assertStringContainsString('Passing a value of type resource', $deprecations[0]); + } +} diff --git a/src/DatabaseDriver.php b/src/DatabaseDriver.php index 81885d02..8f2b431b 100644 --- a/src/DatabaseDriver.php +++ b/src/DatabaseDriver.php @@ -1445,10 +1445,11 @@ public function q($text, $escape = true) /** * Quotes and optionally escapes a string to database requirements for use in database queries. * - * @param array|string $text A string or an array of strings to quote. - * @param boolean $escape True (default) to escape the string, false to leave it unchanged. + * @param mixed $text A scalar or an array of scalars to quote. Other values are deprecated and will not be + * accepted in 5.0. + * @param boolean $escape True (default) to escape the string, false to leave it unchanged. * - * @return array|string The quoted input string. + * @return array|string The quoted string, or an array of quoted strings for an array. * * @since 1.0 */ @@ -1462,9 +1463,32 @@ public function quote($text, $escape = true) return $text; } + $this->checkQuoteInput($text); + return '\'' . ($escape ? $this->escape($text) : $text) . '\''; } + /** + * Trigger a deprecation for values that quote() will not accept in 5.0. + * + * @param mixed $text The value passed to quote(). + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + protected function checkQuoteInput($text): void + { + if ($text !== null && !\is_scalar($text) && !$text instanceof \Stringable) { + trigger_deprecation( + 'joomla/database', + '__DEPLOY_VERSION__', + 'Passing a value of type %s to quote() is deprecated and will throw a TypeError in 5.0; pass a scalar or an array of scalars.', + get_debug_type($text) + ); + } + } + /** * Quotes a binary string to database requirements for use in database queries. * diff --git a/src/DatabaseInterface.php b/src/DatabaseInterface.php index 5f3e41c9..71890ccd 100644 --- a/src/DatabaseInterface.php +++ b/src/DatabaseInterface.php @@ -93,10 +93,10 @@ public function dropTable($table, $ifExists = true); /** * Escapes a string for usage in an SQL statement. * - * @param string $text The string to be escaped. - * @param boolean $extra Optional parameter to provide extra escaping. + * @param string|int|float $text The string to be escaped. Integers and floats are returned as numbers. + * @param boolean $extra Optional parameter to provide extra escaping. * - * @return string The escaped string. + * @return string|int The escaped string; an integer input is returned unchanged. * * @since 2.0.0 */ @@ -467,10 +467,11 @@ public function lockTable($tableName); /** * Quotes and optionally escapes a string to database requirements for use in database queries. * - * @param array|string $text A string or an array of strings to quote. - * @param boolean $escape True (default) to escape the string, false to leave it unchanged. + * @param array|string|int|float|null $text A scalar or an array of scalars to quote. Other values are + * deprecated and will not be accepted in 5.0. + * @param boolean $escape True (default) to escape the string, false to leave it unchanged. * - * @return array|string The quoted input string. + * @return array|string The quoted string, or an array of quoted strings for an array. * * @since 2.0.0 */ diff --git a/src/Sqlsrv/SqlsrvDriver.php b/src/Sqlsrv/SqlsrvDriver.php index e2a430c7..ddab5b3a 100644 --- a/src/Sqlsrv/SqlsrvDriver.php +++ b/src/Sqlsrv/SqlsrvDriver.php @@ -259,6 +259,8 @@ public function quote($text, $escape = true) return parent::quote($text, $escape); } + $this->checkQuoteInput($text); + // To support unicode on MSSQL we have to add prefix N return 'N\'' . ($escape ? $this->escape($text) : $text) . '\''; }