Passed
Pull Request — master (#94)
by Šimon
03:46
created

Bindings::formatParameter()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9

Importance

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