QueryFormatter::formatQuery()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 4
eloc 6
c 2
b 1
f 1
nc 5
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
namespace mindplay\sql\framework;
4
5
/**
6
 * This is a pseudo-namespace for a simple query-formatter *strictly* for diagnostic
7
 * purposes - it implements a simple emulation of PDO placeholder interpolation, so
8
 * that queries can be rendered in error-messages with actual parameter values.
9
 */
10
abstract class QueryFormatter
11
{
12
    /**
13
     * @param string              $sql
14
     * @param array<string,mixed> $params
15
     *
16
     * @return string SQL with emulated prepare (for diagnostic purposes only)
17
     */
18 1
    public static function formatQuery(string $sql, array $params): string
19
    {
20 1
        $quoted_params = [];
21
22 1
        foreach ($params as $name => $value) {
23 1
            $quoted_params[":{$name}"] = $value === null
24 1
                ? "NULL"
25 1
                : (is_numeric($value) ? $value : "'{$value}'");
26
        }
27
28 1
        return strtr($sql, $quoted_params);
29
    }
30
}
31