Passed
Pull Request — master (#57)
by Šimon
02:16
created

Conditions::__ifsets()   C

Complexity

Conditions 16
Paths 42

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
rs 5.0151
ccs 0
cts 21
cp 0
cc 16
eloc 21
nc 42
nop 3
crap 272

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ClickHouseDB\Query\Degeneration;
4
5
use ClickHouseDB\Query\Degeneration;
6
7
class Conditions implements Degeneration
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $bindings = [];
13
14
    /**
15
     * @param array $bindings
16
     */
17 1
    public function bindParams(array $bindings)
18
    {
19 1
        foreach ($bindings as $column => $value) {
20 1
            $this->bindings[$column]=$value;
21
        }
22 1
    }
23
24
25
    static function __ifsets($matches,$markers,$else=false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        $content_false='';
28
29
        if ($else)
30
        {
31
            list($condition, $preset ,$variable, $content_true, $content_false) = $matches;
32
        }
33
        else
34
        {
35
            list($condition, $preset ,$variable, $content_true) = $matches;
36
        }
37
        $preset=strtolower($preset);
38
39
        if ($preset=='set')
40
        {
41
            return (isset($markers[$variable]) && !empty($markers[$variable]))   ? $content_true: $content_false;
42
        }
43
        if ($preset=='bool')
44
        {
45
            return (isset($markers[$variable]) && is_bool($markers[$variable]) && $markers[$variable]==true)
46
                ? $content_true
47
                : $content_false;
48
        }
49
        if ($preset=='string')
50
        {
51
            return (isset($markers[$variable]) && is_string($markers[$variable]) && strlen($markers[$variable]))
52
                ? $content_true
53
                : $content_false;
54
        }
55
        if ($preset=='int')
56
        {
57
            return (isset($markers[$variable]) && intval($markers[$variable])<>0 )
58
                ? $content_true
59
                : $content_false;
60
        }
61
62
        return '';
63
    }
64
65
    /**
66
     * @param $sql
67
     * @return mixed
68
     */
69 1
    public function process($sql)
70
    {
71 1
        $markers=$this->bindings;
72
73
        // 2. process if/else conditions
74
        $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function ($matches) use ($markers) {
75 1
            list($condition, $variable, $content_true, $content_false) = $matches;
76
77 1
            return (isset($markers[$variable]) && ( $markers[$variable] || is_numeric($markers[$variable]) ) )
78
                ? $content_true
79 1
                : $content_false;
80 1
        }, $sql);
81
82
        // 3. process if conditions
83
        $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#sui', function ($matches) use ($markers) {
84 1
            list($condition, $variable, $content) = $matches;
85
86 1
            if (isset($markers[$variable]) && ( $markers[$variable] || is_numeric($markers[$variable]) )) {
87 1
                return $content;
88
            }
89 1
        }, $sql);
90
91
        // 1. process if[set|int]/else conditions
92
        $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui',  function ($matches) use ($markers) {return  self::__ifsets($matches,$markers,true);  }, $sql);
93
        $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{/if}#sui',  function ($matches) use ($markers) { return self::__ifsets($matches,$markers,false);  }, $sql);
94
95 1
        return $sql;
96
    }
97
98
}
99