mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-07-23 09:41:48 +02:00
Fix warning on null query parameter (#722)
* Check if hljs has language (#699) * Fix PHP 8 warning in `TracedStatement::getSqlWithParameters()` when null was bound Since PHP 8.0, [built-in functions like `strtr()` will emit a warning when passing `null` to required string parameters](docs): ``` strtr(): Passing null to parameter #1 ($string) of type string is deprecated ``` This can happen when e.g. binding `PDO::PARAM_NULL` to your query. [docs]: https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.null-not-nullable-internal --------- Co-authored-by: Barry vd. Heuvel <barryvdh@gmail.com>
This commit is contained in:
@@ -114,9 +114,12 @@ class TracedStatement
|
||||
|
||||
foreach ($this->parameters as $k => $v) {
|
||||
|
||||
$backRefSafeV = strtr($v, $cleanBackRefCharMap);
|
||||
|
||||
$v = "$quoteLeft$backRefSafeV$quoteRight";
|
||||
if (null === $v) {
|
||||
$v = 'NULL';
|
||||
} else {
|
||||
$backRefSafeV = strtr($v, $cleanBackRefCharMap);
|
||||
$v = "$quoteLeft$backRefSafeV$quoteRight";
|
||||
}
|
||||
|
||||
if (is_numeric($k)) {
|
||||
$marker = "\?";
|
||||
|
@@ -119,6 +119,37 @@ class TracedStatementTest extends DebugBarTestCase
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if literal `NULL` query parameters are replaced without triggering a deprecation warning since PHP 8.0.0.
|
||||
* This can happen when e.g. binding `PDO::PARAM_NULL` to your prepared statement.
|
||||
*
|
||||
* @link https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.null-not-nullable-internal
|
||||
*/
|
||||
public function testReplacementParamsContainingLiteralNullValueGeneratesCorrectString()
|
||||
{
|
||||
$sql = 'UPDATE user SET login_failed_reason = :nullable_reason WHERE id = :id';
|
||||
|
||||
$params = [
|
||||
'id' => 1234,
|
||||
'nullable_reason' => 'Life happens',
|
||||
];
|
||||
|
||||
$traced = new TracedStatement($sql, $params);
|
||||
$expected = 'UPDATE user SET login_failed_reason = "Life happens" WHERE id = "1234"';
|
||||
$result = $traced->getSqlWithParams('"');
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$params = [
|
||||
'id' => 1234,
|
||||
'nullable_reason' => null,
|
||||
];
|
||||
|
||||
$traced = new TracedStatement($sql, $params);
|
||||
$expected = 'UPDATE user SET login_failed_reason = NULL WHERE id = "1234"';
|
||||
$result = $traced->getSqlWithParams('"');
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if query parameters are being replaced in the correct way
|
||||
* @bugFix Before fix it : select *
|
||||
|
Reference in New Issue
Block a user