Skip to content

Commit fa8049c

Browse files
Fix warning on null query parameter (php-debugbar#722)
* Check if hljs has language (php-debugbar#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 php-debugbar#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 <[email protected]>
1 parent 35e7209 commit fa8049c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/DebugBar/DataCollector/PDO/TracedStatement.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ public function getSqlWithParams(string $quotationChar = '<>') : string
114114

115115
foreach ($this->parameters as $k => $v) {
116116

117-
$backRefSafeV = strtr($v, $cleanBackRefCharMap);
118-
119-
$v = "$quoteLeft$backRefSafeV$quoteRight";
117+
if (null === $v) {
118+
$v = 'NULL';
119+
} else {
120+
$backRefSafeV = strtr($v, $cleanBackRefCharMap);
121+
$v = "$quoteLeft$backRefSafeV$quoteRight";
122+
}
120123

121124
if (is_numeric($k)) {
122125
$marker = "\?";

tests/DebugBar/Tests/TracedStatementTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,37 @@ public function testReplacementParamsContainingPotentialAdditionalNamedPlacehold
119119
$this->assertEquals($expected, $result);
120120
}
121121

122+
/**
123+
* Check if literal `NULL` query parameters are replaced without triggering a deprecation warning since PHP 8.0.0.
124+
* This can happen when e.g. binding `PDO::PARAM_NULL` to your prepared statement.
125+
*
126+
* @link https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.null-not-nullable-internal
127+
*/
128+
public function testReplacementParamsContainingLiteralNullValueGeneratesCorrectString()
129+
{
130+
$sql = 'UPDATE user SET login_failed_reason = :nullable_reason WHERE id = :id';
131+
132+
$params = [
133+
'id' => 1234,
134+
'nullable_reason' => 'Life happens',
135+
];
136+
137+
$traced = new TracedStatement($sql, $params);
138+
$expected = 'UPDATE user SET login_failed_reason = "Life happens" WHERE id = "1234"';
139+
$result = $traced->getSqlWithParams('"');
140+
$this->assertEquals($expected, $result);
141+
142+
$params = [
143+
'id' => 1234,
144+
'nullable_reason' => null,
145+
];
146+
147+
$traced = new TracedStatement($sql, $params);
148+
$expected = 'UPDATE user SET login_failed_reason = NULL WHERE id = "1234"';
149+
$result = $traced->getSqlWithParams('"');
150+
$this->assertEquals($expected, $result);
151+
}
152+
122153
/**
123154
* Check if query parameters are being replaced in the correct way
124155
* @bugFix Before fix it : select *

0 commit comments

Comments
 (0)