Passed
Pull Request — master (#84)
by Šimon
02:19
created

Bindings   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
eloc 42
dl 0
loc 136
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A escapeString() 0 3 1
A bindParams() 0 5 2
B formatParameter() 0 19 9
B process() 0 39 6
A formatStringParameter() 0 3 1
A bindParam() 0 3 1
A compile_binds() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ClickHouseDB\Query\Degeneration;
6
7
use ClickHouseDB\Exception\UnsupportedParameterType;
8
use ClickHouseDB\Query\Degeneration;
9
use DateTimeInterface;
10
use function array_map;
11
use function implode;
12
use function is_array;
13
use function is_bool;
14
use function is_callable;
15
use function is_float;
16
use function is_int;
17
use function is_string;
18
use function sprintf;
19
20
class Bindings implements Degeneration
21
{
22
    /**
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...
23
     * @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...
24
     */
25
    protected $bindings = [];
26
27
    /**
28
     * @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...
29
     */
30 57
    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...
31
    {
32 57
        $this->bindings = [];
33 57
        foreach ($bindings as $column => $value) {
34 22
            $this->bindParam($column, $value);
35
        }
36 57
    }
37
38
    /**
39
     * @param string $column
40
     * @param mixed  $value
41
     */
42 22
    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...
43
    {
44 22
        $this->bindings[$column] = $value;
45 22
    }
46
47
    /**
48
     * Escape an string
49
     *
50
     * @param string $value
51
     * @return string
52
     */
53 17
    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 17
        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...
56
    }
57
58
    /**
59
     * Binds a list of values to the corresponding parameters.
60
     * This is similar to [[bindValue()]] except that it binds multiple values at a time.
61
     *
62
     * @param string $sql
63
     * @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...
64
     * @param string $pattern
65
     * @return string
66
     */
67 57
    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...
68
    {
69
        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...
70 21
            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...
71 21
                return $binds[$m[1]]; // Then replace it from our array
72
            }
73
74 3
            return $m[0]; // Otherwise return the whole match (basically we won't change it)
75 57
        }, $sql);
76
    }
77
78
    /**
79
     * Compile Bindings
80
     *
81
     * @param string $sql
82
     * @return mixed
83
     */
84 57
    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...
85
    {
86 57
        $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...
87 57
        $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...
88 57
        foreach ($this->bindings as $key => $value) {
89 22
            if (is_array($value)) {
90 6
                $valueSet = implode(', ', $value);
91
92 6
                $values = array_map(
93
                    function ($value) {
94 6
                        return $this->formatParameter($value);
95 6
                    },
96 6
                    $value
97
                );
98
99 6
                $formattedParameter = implode(',', $values);
100
            } else {
101 18
                $valueSet = $value;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

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...
102 18
                $formattedParameter = $this->formatParameter($value);
103
            }
104
105 21
            if ($formattedParameter !== null) {
106 21
                $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...
107
            }
108
109 21
            if ($valueSet !== null) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
110 21
                $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...
111
            }
112
        }
113
114 57
        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...
115
        {
116
            // dipping in binds
117
            // example ['A' => '{B}' , 'B'=>':C','C'=>123]
118 57
            $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...
119
        }
120 57
        $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...
121
122 57
        return $sql;
123
    }
124
125
    /**
126
     * @param mixed $value
127
     * @return mixed
128
     */
129 22
    private function formatParameter($value)
130
    {
131 22
        if ($value instanceof DateTimeInterface) {
132 1
            $value = $value->format('Y-m-d H:i:s');
133
        }
134
135 22
        if (is_float($value) || is_int($value) || is_bool($value) || $value === null) {
136 9
            return $value;
137
        }
138
139 18
        if (is_object($value) && is_callable([$value, '__toString'])) {
0 ignored issues
show
introduced by
Function is_object() should not be referenced via a fallback global name, but via a use statement.
Loading history...
140 1
            $value = (string) $value;
141
        }
142
143 18
        if (is_string($value)) {
144 17
            return $this->formatStringParameter($this->escapeString($value));
145
        }
146
147 1
        throw UnsupportedParameterType::new($value);
148
    }
149
150
    /**
151
     * @return string
152
     */
153 17
    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...
154
    {
155 17
        return sprintf("'%s'", $value);
156
    }
157
}
158