Passed
Pull Request — master (#94)
by Šimon
04:26
created

Bindings::formatStringParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ClickHouseDB\Query\Degeneration;
6
7
use ClickHouseDB\Exception\UnsupportedValueType;
0 ignored issues
show
introduced by
Type ClickHouseDB\Exception\UnsupportedValueType is not used in this file.
Loading history...
8
use ClickHouseDB\Query\Degeneration;
9
use ClickHouseDB\Quote\ValueFormatter;
10
use ClickHouseDB\Type\UInt64;
0 ignored issues
show
introduced by
Type ClickHouseDB\Type\UInt64 is not used in this file.
Loading history...
11
use DateTimeInterface;
0 ignored issues
show
introduced by
Type DateTimeInterface is not used in this file.
Loading history...
12
use function array_map;
13
use function implode;
14
use function is_array;
15
use function is_bool;
0 ignored issues
show
introduced by
Type is_bool is not used in this file.
Loading history...
16
use function is_callable;
0 ignored issues
show
introduced by
Type is_callable is not used in this file.
Loading history...
17
use function is_float;
0 ignored issues
show
introduced by
Type is_float is not used in this file.
Loading history...
18
use function is_int;
0 ignored issues
show
introduced by
Type is_int is not used in this file.
Loading history...
19
use function is_object;
0 ignored issues
show
introduced by
Type is_object is not used in this file.
Loading history...
20
use function is_string;
0 ignored issues
show
introduced by
Type is_string is not used in this file.
Loading history...
21
use function sprintf;
0 ignored issues
show
introduced by
Type sprintf is not used in this file.
Loading history...
22
23
class Bindings implements Degeneration
24
{
25
    /**
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...
26
     * @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...
27
     */
28
    protected $bindings = [];
29
30
    /**
31
     * @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...
32
     */
33 51
    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...
34
    {
35 51
        $this->bindings = [];
36 51
        foreach ($bindings as $column => $value) {
37 22
            $this->bindParam($column, $value);
38
        }
39 51
    }
40
41
    /**
42
     * @param string $column
43
     * @param mixed  $value
44
     */
45 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...
46
    {
47 22
        $this->bindings[$column] = $value;
48 22
    }
49
50
    /**
51
     * Binds a list of values to the corresponding parameters.
52
     * This is similar to [[bindValue()]] except that it binds multiple values at a time.
53
     *
54
     * @param string $sql
55
     * @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...
56
     * @param string $pattern
57
     * @return string
58
     */
59 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...
60
    {
61
        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...
62 22
            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...
63 21
                return $binds[$m[1]]; // Then replace it from our array
64
            }
65
66 4
            return $m[0]; // Otherwise return the whole match (basically we won't change it)
67 49
        }, $sql);
68
    }
69
70
    /**
71
     * Compile Bindings
72
     *
73
     * @param string $sql
74
     * @return mixed
75
     */
76 50
    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...
77
    {
78 50
        $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...
79 50
        $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...
80 50
        foreach ($this->bindings as $key => $value) {
81 22
            if (is_array($value)) {
82 6
                $valueSet = implode(', ', $value);
83
84 6
                $values = array_map(
85
                    function ($value) {
86 6
                        return ValueFormatter::formatValue($value);
87 6
                    },
88 6
                    $value
89
                );
90
91 6
                $formattedParameter = implode(',', $values);
92
            } else {
93 18
                $valueSet           = $value;
94 18
                $formattedParameter = ValueFormatter::formatValue($value);
95
            }
96
97 21
            if ($formattedParameter !== null) {
98 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...
99
            }
100
101 21
            if ($valueSet !== null) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
102 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...
103
            }
104
        }
105
106 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...
107
        {
108
            // dipping in binds
109
            // example ['A' => '{B}' , 'B'=>':C','C'=>123]
110 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...
111
        }
112 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...
113
114 49
        return $sql;
115
    }
116
}
117