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

Bindings::escapeArray()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
rs 9.6111
cc 5
nc 5
nop 1
crap 5.025

1 Method

Rating   Name   Duplication   Size   Complexity  
A Bindings::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_object;
18
use function is_string;
19
use function sprintf;
20
21
class Bindings implements Degeneration
22
{
23
    /**
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...
24
     * @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...
25
     */
26
    protected $bindings = [];
27
28
    /**
29
     * @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...
30
     */
31 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...
32
    {
33 57
        $this->bindings = [];
34 57
        foreach ($bindings as $column => $value) {
35 22
            $this->bindParam($column, $value);
36
        }
37 57
    }
38
39
    /**
40
     * @param string $column
41
     * @param mixed  $value
42
     */
43 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...
44
    {
45 22
        $this->bindings[$column] = $value;
46 22
    }
47
48
    /**
49
     * Escape an string
50
     *
51
     * @param string $value
52
     * @return string
53
     */
54 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...
55
    {
56 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...
57
    }
58
59
    /**
60
     * Binds a list of values to the corresponding parameters.
61
     * This is similar to [[bindValue()]] except that it binds multiple values at a time.
62
     *
63
     * @param string $sql
64
     * @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...
65
     * @param string $pattern
66
     * @return string
67
     */
68 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...
69
    {
70
        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...
71 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...
72 21
                return $binds[$m[1]]; // Then replace it from our array
73
            }
74
75 3
            return $m[0]; // Otherwise return the whole match (basically we won't change it)
76 57
        }, $sql);
77
    }
78
79
    /**
80
     * Compile Bindings
81
     *
82
     * @param string $sql
83
     * @return mixed
84
     */
85 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...
86
    {
87 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...
88 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...
89 57
        foreach ($this->bindings as $key => $value) {
90 22
            if (is_array($value)) {
91 6
                $valueSet = implode(', ', $value);
92
93 6
                $values = array_map(
94
                    function ($value) {
95 6
                        return $this->formatParameter($value);
96 6
                    },
97 6
                    $value
98
                );
99
100 6
                $formattedParameter = implode(',', $values);
101
            } else {
102 18
                $valueSet           = $value;
103 18
                $formattedParameter = $this->formatParameter($value);
104
            }
105
106 21
            if ($formattedParameter !== null) {
107 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...
108
            }
109
110 21
            if ($valueSet !== null) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
111 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...
112
            }
113
        }
114
115 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...
116
        {
117
            // dipping in binds
118
            // example ['A' => '{B}' , 'B'=>':C','C'=>123]
119 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...
120
        }
121 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...
122
123 57
        return $sql;
124
    }
125
126
    /**
127
     * @param mixed $value
128
     * @return mixed
129
     */
130 22
    private function formatParameter($value)
131
    {
132 22
        if ($value instanceof DateTimeInterface) {
133 1
            $value = $value->format('Y-m-d H:i:s');
134
        }
135
136 22
        if (is_float($value) || is_int($value) || is_bool($value) || $value === null) {
137 9
            return $value;
138
        }
139
140 18
        if (is_object($value) && is_callable([$value, '__toString'])) {
141 1
            $value = (string) $value;
142
        }
143
144 18
        if (is_string($value)) {
145 17
            return $this->formatStringParameter($this->escapeString($value));
146
        }
147
148 1
        throw UnsupportedParameterType::new($value);
149
    }
150
151
    /**
152
     * @return string
153
     */
154 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...
155
    {
156 17
        return sprintf("'%s'", $value);
157
    }
158
}
159