Completed
Push — master ( 21bf70...cc5276 )
by Kamil
05:50
created

Query::resolveValueForSql()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 30
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 25
nc 7
nop 1
crap 72
1
<?php
2
3
namespace Dazzle\MySQL;
4
5
class Query
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $sql;
11
12
    /**
13
     * @var string
14
     */
15
    protected $sqlPrepared;
16
17
    /**
18
     * @var mixed[]
19
     */
20
    protected $params = [];
21
22
    /**
23
     * @var string[]
24
     */
25
    protected $escapeChars = [
26
        "\x00" => "\\0",
27
        "\r"   => "\\r",
28
        "\n"   => "\\n",
29
        "\t"   => "\\t",
30
        "'"    => "\'",
31
        '"'    => '\"',
32
        "\\"   => "\\\\",
33
    ];
34
35
    /**
36
     * @param string $sql
37
     */
38
    public function __construct($sql)
39
    {
40
        $this->sql = $sql;
41
        $this->sqlPrepared = '';
42
    }
43
44
    /**
45
     * Binding params for the query, mutiple arguments support.
46
     *
47
     * @param  mixed              $param
0 ignored issues
show
Bug introduced by
There is no parameter named $param. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     * @return Query
49
     */
50
    public function bindParams()
51
    {
52
        $this->sqlPrepared = null;
53
        $this->params   = func_get_args();
54
55
        return $this;
56
    }
57
58
    public function bindParamsFromArray(array $params)
59
    {
60
        $this->sqlPrepared = null;
61
        $this->params   = $params;
62
63
        return $this;
64
    }
65
66
    public function escape($str)
67
    {
68
        return strtr($str, $this->escapeChars);
69
    }
70
71
    /**
72
     * @param  mixed  $value
73
     * @return string
74
     */
75
    protected function resolveValueForSql($value)
76
    {
77
        $type = gettype($value);
78
79
        switch ($type)
80
        {
81
            case 'boolean':
82
                $value = (int) $value;
83
                break;
84
            case 'double':
85
            case 'integer':
86
                break;
87
            case 'string':
88
                $value = "'" . $this->escape($value) . "'";
89
                break;
90
            case 'array':
91
                $nvalue = [];
92
                foreach ($value as $v) {
93
                    $nvalue[] = $this->resolveValueForSql($v);
94
                }
95
                $value = implode(',', $nvalue);
96
                break;
97
            case 'null':
98
                $value = 'null';
99
                break;
100
            default:
101
                throw new \InvalidArgumentException(sprintf('Not supportted value type of %s.', $type));
102
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
103
        }
104
105
        return $value;
106
    }
107
108
    protected function buildSql()
109
    {
110
        $sql = $this->sql;
111
        $offset = strpos($sql, '?');
112
113
        foreach ($this->params as $param)
114
        {
115
            $replacement = $this->resolveValueForSql($param);
116
            $sql = substr_replace($sql, $replacement, $offset, 1);
117
            $offset = strpos($sql, '?', $offset + strlen($replacement));
118
        }
119
120
        if ($offset !== false)
121
        {
122
            throw new \LogicException('Params not enough to build sql');
123
        }
124
125
        return $sql;
126
    }
127
128
    /**
129
     * Get the constructed and escaped sql string.
130
     *
131
     * @return string
132
     */
133
    public function getSql()
134
    {
135
        if ($this->sqlPrepared === null)
136
        {
137
            $this->sqlPrepared = $this->buildSql();
138
        }
139
140
        return $this->sqlPrepared;
141
    }
142
}
143