Completed
Push — master ( a58b00...b032d6 )
by Igor
10s
created

Conditions::__ifsets()   C

Complexity

Conditions 16
Paths 42

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 37
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 5.5666
cc 16
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
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
3
namespace ClickHouseDB\Query\Degeneration;
4
5
use ClickHouseDB\Query\Degeneration;
6
7
class Conditions implements Degeneration
8
{
9
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Query\Degeneration\Conditions::$bindings with single line content, use one-line comment instead.
Loading history...
10
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Query\Degeneration\Conditions::$bindings does not specify type hint for its items.
Loading history...
11
     */
12
    protected $bindings = [];
13
14
    /**
15
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Query\Degeneration\Conditions::bindParams() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
16
     */
17 1
    public function bindParams(array $bindings)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Conditions::bindParams() does not have void return type hint.
Loading history...
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...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __ifsets.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have parameter type hint nor @param annotation for its parameter $matches.
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have parameter type hint nor @param annotation for its parameter $markers.
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have parameter type hint nor @param annotation for its parameter $else.
Loading history...
introduced by
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have return type hint nor @return annotation for its return value.
Loading history...
26
    {
27
        $content_false = '';
28
29
        if ($else)
30
        {
31
            list($condition, $preset, $variable, $content_true, $content_false) = $matches;
32
        } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
33
        {
34
            list($condition, $preset, $variable, $content_true) = $matches;
35
        }
36
        $preset = strtolower($preset);
0 ignored issues
show
introduced by
Function strtolower() should not be referenced via a fallback global name, but via a use statement.
Loading history...
37
38
        if ($preset == 'set')
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
39
        {
40
            return (isset($markers[$variable]) && !empty($markers[$variable])) ? $content_true : $content_false;
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
41
        }
42
        if ($preset == 'bool')
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
43
        {
44
            return (isset($markers[$variable]) && is_bool($markers[$variable]) && $markers[$variable] == true)
0 ignored issues
show
introduced by
Function is_bool() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
45
                ? $content_true
46
                : $content_false;
47
        }
48
        if ($preset == 'string')
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
49
        {
50
            return (isset($markers[$variable]) && is_string($markers[$variable]) && strlen($markers[$variable]))
0 ignored issues
show
introduced by
Function is_string() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function strlen() should not be referenced via a fallback global name, but via a use statement.
Loading history...
51
                ? $content_true
52
                : $content_false;
53
        }
54
        if ($preset == 'int')
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
55
        {
56
            return (isset($markers[$variable]) && intval($markers[$variable]) <> 0)
0 ignored issues
show
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Operator <> is disallowed, use !== instead.
Loading history...
57
                ? $content_true
58
                : $content_false;
59
        }
60
61
        return '';
62
    }
63
64
    /**
65
     * @param string $sql
66
     * @return mixed
67
     */
68 1
    public function process($sql)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Query\Degeneration\Conditions::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...
69
    {
70 1
        $markers = $this->bindings;
71
72
        // 2. process if/else conditions
73
        $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) {
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...
74 1
            list($condition, $variable, $content_true, $content_false) = $matches;
75
76 1
            return (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable])))
0 ignored issues
show
introduced by
Function is_numeric() should not be referenced via a fallback global name, but via a use statement.
Loading history...
77
                ? $content_true
78 1
                : $content_false;
79 1
        }, $sql);
80
81
        // 3. process if conditions
82
        $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) {
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...
83 1
            list($condition, $variable, $content) = $matches;
84
85 1
            if (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) {
0 ignored issues
show
introduced by
Function is_numeric() should not be referenced via a fallback global name, but via a use statement.
Loading history...
86 1
                return $content;
87
            }
88 1
        }, $sql);
89
90
        // 1. process if[set|int]/else conditions
91
        $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) {return  self::__ifsets($matches, $markers, true); }, $sql);
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
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Language constructs must be followed by a single space; expected 1 space but found "··"
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
92
        $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { return self::__ifsets($matches, $markers, false); }, $sql);
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
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
93
94 1
        return $sql;
95
    }
96
97
}
0 ignored issues
show
introduced by
There must be exactly 0 empty lines before class closing brace.
Loading history...
98