Bindings   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 97
ccs 36
cts 36
cp 1
rs 10
c 1
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bindParams() 0 5 2
B process() 0 39 6
A bindParam() 0 3 1
A getBind() 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\Query\Degeneration;
8
use ClickHouseDB\Quote\ValueFormatter;
9
use function array_map;
0 ignored issues
show
introduced by
Expected 1 lines between different types of use statement, found 0.
Loading history...
10
use function implode;
11
use function is_array;
12
13
class Bindings implements Degeneration
14
{
15
    /**
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...
16
     * @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...
17
     */
18
    protected $bindings = [];
19
20
    /**
21
     * @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...
22
     */
23 56
    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...
24
    {
25 56
        $this->bindings = [];
26 56
        foreach ($bindings as $column => $value) {
27 24
            $this->bindParam($column, $value);
28
        }
29 56
    }
30
31
    public function getBind(): array
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::getBind() does not have @return annotation for its traversable return value.
Loading history...
32
    {
33
        return $this->bindings;
34
    }
35 24
36
    /**
37 24
     * @param string $column
38 24
     * @param mixed  $value
39
     */
40
    public function bindParam($column, $value)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::bindParam() does not have native 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...
41
    {
42
        $this->bindings[$column] = $value;
43
    }
44
45
    /**
46
     * Binds a list of values to the corresponding parameters.
47
     * This is similar to [[bindValue()]] except that it binds multiple values at a time.
48
     *
49 54
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
50
     * @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...
51 54
     * @param string $pattern
52 24
     * @return string
53 23
     */
54
    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 native 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 native 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 native 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 native 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...
55
    {
56 6
        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...
introduced by
Closure not using "$this" should be declared static.
Loading history...
Coding Style introduced by
Expected 1 space before opening brace; found 0
Loading history...
57 54
            if(isset($binds[$m[1]])){ // If it exists in our array
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
58
                return $binds[$m[1]]; // Then replace it from our array
59
            }
60
61
            return $m[0]; // Otherwise return the whole match (basically we won't change it)
62
        }, $sql);
63
    }
64
65
    /**
66 55
     * Compile Bindings
67
     *
68 55
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
69 55
     * @return mixed
70 55
     */
71 24
    public function process($sql)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Bindings::process() does not have native type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
72 6
    {
73
        $bindFormatted=[];
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
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...
74 6
        $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...
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
75 6
        foreach ($this->bindings as $key => $value) {
76 6
            if (is_array($value)) {
77 6
                $valueSet = implode(', ', $value);
78 6
79
                $values = array_map(
80
                    function ($value) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
81 6
                        return ValueFormatter::formatValue($value);
82
                    },
83 20
                    $value
84 20
                );
85
86
                $formattedParameter = implode(',', $values);
87 23
            } else {
88 23
                $valueSet           = $value;
89
                $formattedParameter = ValueFormatter::formatValue($value);
90
            }
91 23
92 23
            if ($formattedParameter !== null) {
93
                $bindFormatted[$key]=$formattedParameter;
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
94
            }
95
96 54
            if ($valueSet !== null) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
97
                $bindRaw[$key]=$valueSet;
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
98
            }
99
        }
100 54
101
        for ($loop=0;$loop<2;$loop++)
0 ignored issues
show
introduced by
Expected 1 lines after "for", found 0.
Loading history...
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after first semicolon of FOR loop; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space before "<"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "<"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after second semicolon of FOR loop; 0 found
Loading history...
102 54
        {
103
            // dipping in binds
104 54
            // example ['A' => '{B}' , 'B'=>':C','C'=>123]
105
            $sql=$this->compile_binds($sql,$bindRaw,'#{([\w+]+)}#');
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
106
        }
107
        $sql=$this->compile_binds($sql,$bindFormatted,'#:([\w+]+)#');
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
108
109
        return $sql;
110
    }
111
}
112