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) . '\''; }