Completed
Push — master ( a58b00...b032d6 )
by Igor
10s
created

Bindings::bindParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
3
namespace ClickHouseDB\Query\Degeneration;
4
5
use DateTimeInterface;
6
use function array_map;
7
use function implode;
8
use function is_array;
9
use function is_float;
10
use function is_int;
11
use function is_string;
12
use function sprintf;
13
use function str_ireplace;
0 ignored issues
show
introduced by
Type str_ireplace is not used in this file.
Loading history...
14
15
class Bindings implements \ClickHouseDB\Query\Degeneration
0 ignored issues
show
introduced by
Class \ClickHouseDB\Query\Degeneration should not be referenced via a fully qualified name, but via a use statement.
Loading history...
16
{
17
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Query\Degeneration\Bindings::$bindings with single line content, use one-line comment instead.
Loading history...
18
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Query\Degeneration\Bindings::$bindings does not specify type hint for its items.
Loading history...
19
     */
20
    protected $bindings = [];
21
22
23
    /**
24
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Query\Degeneration\Bindings::bindParams() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
25
     */
26 49
    public function bindParams(array $bindings)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::bindParams() does not have void return type hint.
Loading history...
27
    {
28 49
        $this->bindings = [];
29 49
        foreach ($bindings as $column => $value) {
30 19
            $this->bindParam($column, $value);
31
        }
32 49
    }
33
34
    /**
35
     * @param string $column
36
     * @param mixed  $value
37
     */
38 19
    public function bindParam($column, $value)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::bindParam() does not have parameter type hint for its parameter $column but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::bindParam() does not have void return type hint.
Loading history...
39
    {
40 19
        if ($value instanceof DateTimeInterface) {
41
            $value = $value->format('Y-m-d H:i:s');
42
        }
43
44 19
        $this->bindings[$column] = $value;
45 19
    }
46
47
    /**
48
     * Escape an string
49
     *
50
     * @param string $value
51
     * @return string
52
     */
53 13
    private function escapeString($value)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::escapeString() does not have parameter type hint for its parameter $value but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::escapeString() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
54
    {
55
        // return str_replace("'", "''", remove_invisible_characters($str, FALSE));
56 13
        return addslashes($value);
0 ignored issues
show
introduced by
Function addslashes() should not be referenced via a fallback global name, but via a use statement.
Loading history...
57
    }
58
59
    /**
60
     * Escape an array
61
     *
62
     * @param array $values
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Query\Degeneration\Bindings::escapeArray() does not specify type hint for items of its traversable parameter $values.
Loading history...
63
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Query\Degeneration\Bindings::escapeArray() does not specify type hint for items of its traversable return value.
Loading history...
64
     */
65 6
    private function escapeArray($values)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::escapeArray() does not have parameter type hint for its parameter $values but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::escapeArray() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
66
    {
67 6
        $escapedValues = [];
68 6
        foreach ($values as $value) {
69 6
            if (is_numeric($value)) {
0 ignored issues
show
introduced by
Function is_numeric() should not be referenced via a fallback global name, but via a use statement.
Loading history...
70 5
                $escapedValues[] = $value;
71 3
            } elseif (is_string($value)) {
72 3
                $escapedValues[] = $this->escapeString($value);
73
            } elseif (is_array($value)) {
74 6
                $escapedValues[] = $this->escapeArray($value);
75
            }
76
        }
77
78 6
        return $escapedValues;
79
    }
80
81
    /**
82
     * Binds a list of values to the corresponding parameters.
83
     * This is similar to [[bindValue()]] except that it binds multiple values at a time.
84
     *
85
     * @param string $sql
86
     * @param array $binds
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Query\Degeneration\Bindings::compile_binds() does not specify type hint for items of its traversable parameter $binds.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
87
     * @param string $pattern
88
     * @return string
89
     */
90 49
    public function compile_binds($sql, $binds,$pattern)
0 ignored issues
show
Coding Style introduced by
Method name "Bindings::compile_binds" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::compile_binds() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::compile_binds() does not have parameter type hint for its parameter $binds but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::compile_binds() does not have parameter type hint for its parameter $pattern but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::compile_binds() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$pattern"; 0 found
Loading history...
91
    {
92
        return preg_replace_callback($pattern, function($m) use ($binds){
0 ignored issues
show
introduced by
Function preg_replace_callback() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
Expected 1 space before opening brace; found 0
Loading history...
93 19
            if(isset($binds[$m[1]])){ // If it exists in our array
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
94 19
                return $binds[$m[1]]; // Then replace it from our array
95
            }else{
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; 0 found
Loading history...
introduced by
Remove useless else to reduce code nesting.
Loading history...
96 3
                return $m[0]; // Otherwise return the whole match (basically we won't change it)
97
            }
98 49
        }, $sql);
99
    }
100
101
102
    /**
103
     * Compile Bindings
104
     *
105
     * @param string $sql
106
     * @return mixed
107
     */
108 49
    public function process($sql)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::process() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
109
    {
110 49
        arsort($this->bindings);
0 ignored issues
show
introduced by
Function arsort() should not be referenced via a fallback global name, but via a use statement.
Loading history...
111
112 49
        $bindFormatted=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
113 49
        $bindRaw=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
114 49
        foreach ($this->bindings as $key => $value) {
115 19
            $valueSet           = null;
116 19
            $formattedParameter = null;
117
118 19
            if ($value === null || $value === false) {
119 1
                $formattedParameter = '';
120
            }
121
122 19
            if (is_array($value)) {
123 6
                $escapedValues = $this->escapeArray($value);
124
125 6
                $escapedValues = array_map(
126
                    function ($escapedValue) {
127 6
                        if (is_string($escapedValue)) {
128 5
                            return $this->formatStringParameter($escapedValue);
129
                        }
130
131 3
                        return $escapedValue;
132 6
                    },
133 6
                    $escapedValues
134
                );
135
136 6
                $formattedParameter = implode(',', $escapedValues);
137 6
                $valueSet           = implode(', ', $escapedValues);
138
            }
139
140 19
            if (is_float($value) || is_int($value)) {
141 7
                $formattedParameter = $value;
142 7
                $valueSet           = $value;
143
            }
144
145 19
            if (is_string($value)) {
146 12
                $valueSet           = $value;
147 12
                $formattedParameter = $this->formatStringParameter($this->escapeString($value));
148
            }
149
150 19
            if ($formattedParameter !== null) {
151 19
                $bindFormatted[$key]=$formattedParameter;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
152
            }
153
154 19
            if ($valueSet !== null) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
155 19
                $bindRaw[$key]=$valueSet;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
156
            }
157
        }
158
159 49
        for ($loop=0;$loop<2;$loop++)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after first semicolon of FOR loop; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after second semicolon of FOR loop; 0 found
Loading history...
160
        {
161
            // dipping in binds
162
            // example ['A' => '{B}' , 'B'=>':C','C'=>123]
163 49
            $sql=$this->compile_binds($sql,$bindRaw,'#{([\w+]+)}#');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
164
        }
165 49
        $sql=$this->compile_binds($sql,$bindFormatted,'#:([\w+]+)#');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
166
167 49
        return $sql;
168
    }
169
170
    /**
171
     * @return string
172
     */
173 15
    private function formatStringParameter($value)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::formatStringParameter() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::formatStringParameter() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
174
    {
175 15
        return sprintf("'%s'", $value);
176
    }
177
}
178