QueryFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 7
c 2
b 1
f 1
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A formatQuery() 0 11 4
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