Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Tests/Sqlite/QuoteDeprecationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Database\Tests\Sqlite;

use Joomla\Database\Sqlite\SqliteDriver;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

/**
* Tests the deprecation of unsupported input to Joomla\Database\DatabaseDriver::quote(), using SQLite without a connection
*/
class QuoteDeprecationTest extends TestCase
{
#[TestDox('Quoting a value that is neither scalar nor an array is deprecated')]
public function testQuoteNonScalarIsDeprecated()
{
// A driver whose escape() accepts anything that casts to a string, so only quote() is tested
$db = new class (['driver' => '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]);
}
}
30 changes: 27 additions & 3 deletions src/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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.
*
Expand Down
13 changes: 7 additions & 6 deletions src/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Sqlsrv/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . '\'';
}
Expand Down
Loading