ParamsBind::binding()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 9
nop 2
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * Params binding
4
 * User: moyo
5
 * Date: 15/12/2017
6
 * Time: 3:59 PM
7
 */
8
9
namespace Carno\Database\Chips;
10
11
trait ParamsBind
12
{
13
    /**
14
     * @var string
15
     */
16
    private $pbks = '{![%s]-}';
17
18
    /**
19
     * @param string $sql
20
     * @param array $params
21
     * @return string
22
     */
23
    protected function binding(string $sql, array $params) : string
24
    {
25
        // sort by key length (make correct of ":test,:test2")
26
        uksort($params, static function ($a, $b) {
27
            return strlen($b) - strlen($a);
28
        });
29
30
        // prepare slots
31
        foreach ($params as $name => $data) {
32
            is_scalar($data) && $sql = str_ireplace([":{$name}", "?{$name}"], sprintf($this->pbks, $name), $sql);
33
        }
34
35
        // data replaced
36
        foreach ($params as $name => $data) {
37
            is_scalar($data) && $sql = str_ireplace(sprintf($this->pbks, $name), '"'.$this->escape($data).'"', $sql);
0 ignored issues
show
Bug introduced by
It seems like escape() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            is_scalar($data) && $sql = str_ireplace(sprintf($this->pbks, $name), '"'.$this->/** @scrutinizer ignore-call */ escape($data).'"', $sql);
Loading history...
38
        }
39
40
        return $sql;
41
    }
42
}
43