Issues (2354)

src/Query/Degeneration/Conditions.php (73 issues)

1
<?php
0 ignored issues
show
Missing declare(strict_types=1).
Loading history...
2
3
namespace ClickHouseDB\Query\Degeneration;
4
5
use ClickHouseDB\Exception\QueryException;
6
use ClickHouseDB\Query\Degeneration;
7
8
class Conditions implements Degeneration
9
{
10
    /**
0 ignored issues
show
Found multi-line comment for property \ClickHouseDB\Query\Degeneration\Conditions::$bindings with single line content, use one-line comment instead.
Loading history...
11
     * @var array
0 ignored issues
show
@var annotation of property \ClickHouseDB\Query\Degeneration\Conditions::$bindings does not specify type hint for its items.
Loading history...
12
     */
13
    protected $bindings = [];
14
15
    /**
16
     * @param array $bindings
0 ignored issues
show
@param annotation of method \ClickHouseDB\Query\Degeneration\Conditions::bindParams() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
17
     */
18 3
    public function bindParams(array $bindings)
0 ignored issues
show
Method \ClickHouseDB\Query\Degeneration\Conditions::bindParams() does not have void return type hint.
Loading history...
19
    {
20 3
        foreach ($bindings as $column => $value) {
21 3
            $this->bindings[$column] = $value;
22
        }
23 3
    }
24
25
    public function getBind(): array
0 ignored issues
show
Method \ClickHouseDB\Query\Degeneration\Conditions::getBind() does not have @return annotation for its traversable return value.
Loading history...
26 3
    {
27
        return $this->bindings;
28 3
    }
29 3
30 3
    static function __ifsets($matches, $markers)
0 ignored issues
show
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...
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have parameter type hint nor @param annotation for its parameter $matches.
Loading history...
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have parameter type hint nor @param annotation for its parameter $markers.
Loading history...
Method \ClickHouseDB\Query\Degeneration\Conditions::__ifsets() does not have return type hint nor @return annotation for its return value.
Loading history...
31
    {
32 3
        $content_false = '';
0 ignored issues
show
The variable $content_false should be in camel caps format.
Loading history...
33 3
        $condition = '';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

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...
34 3
        $flag_else = '';
0 ignored issues
show
The assignment to $flag_else is dead and can be removed.
Loading history...
The variable $flag_else should be in camel caps format.
Loading history...
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

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...
35 3
//print_r($matches);
36
        if (sizeof($matches) == 4) {
0 ignored issues
show
Expected 1 lines after "if", found 0.
Loading history...
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
The use of function sizeof() is forbidden; use count() instead
Loading history...
Operator == is disallowed, use === instead.
Loading history...
37
            list($condition, $preset, $variable, $content_true) = $matches;
0 ignored issues
show
list(...) is forbidden, use [...] instead.
Loading history...
The variable $content_true should be in camel caps format.
Loading history...
38
        } elseif (sizeof($matches) == 6) {
0 ignored issues
show
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
The use of function sizeof() is forbidden; use count() instead
Loading history...
Operator == is disallowed, use === instead.
Loading history...
39 3
            list($condition, $preset, $variable, $content_true, $flag_else, $content_false) = $matches;
0 ignored issues
show
list(...) is forbidden, use [...] instead.
Loading history...
The variable $content_true should be in camel caps format.
Loading history...
The variable $flag_else should be in camel caps format.
Loading history...
The variable $content_false should be in camel caps format.
Loading history...
40 3
        } else {
41
            throw new QueryException('Error in parse Conditions' . json_encode($matches));
0 ignored issues
show
Function json_encode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
42 3
        }
43 3
        $variable = trim($variable);
0 ignored issues
show
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
44 3
        $preset = strtolower(trim($preset));
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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...
Function strtolower() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
45 3
46
        if ($preset == '') {
0 ignored issues
show
Expected 1 lines after "if", found 0.
Loading history...
Operator == is disallowed, use === instead.
Loading history...
47 2
            return (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable])))
0 ignored issues
show
Useless parentheses.
Loading history...
Function is_numeric() should not be referenced via a fallback global name, but via a use statement.
Loading history...
48 2
                ? $content_true
0 ignored issues
show
The variable $content_true should be in camel caps format.
Loading history...
49
                : $content_false;
0 ignored issues
show
The variable $content_false should be in camel caps format.
Loading history...
50 2
        }
51 2
        if ($preset == 'set') {
0 ignored issues
show
Operator == is disallowed, use === instead.
Loading history...
Expected 1 lines after "if", found 0.
Loading history...
52 2
            return (isset($markers[$variable]) && !empty($markers[$variable])) ? $content_true : $content_false;
0 ignored issues
show
The variable $content_true should be in camel caps format.
Loading history...
Useless parentheses.
Loading history...
The variable $content_false should be in camel caps format.
Loading history...
Expected 1 space after NOT operator; 0 found
Loading history...
53 2
        }
54
        if ($preset == 'bool') {
0 ignored issues
show
Expected 1 lines after "if", found 0.
Loading history...
Operator == is disallowed, use === instead.
Loading history...
55 2
            return (isset($markers[$variable]) && is_bool($markers[$variable]) && $markers[$variable] == true)
0 ignored issues
show
Operator == is disallowed, use === instead.
Loading history...
Useless parentheses.
Loading history...
Function is_bool() should not be referenced via a fallback global name, but via a use statement.
Loading history...
56 2
                ? $content_true
0 ignored issues
show
The variable $content_true should be in camel caps format.
Loading history...
57 2
                : $content_false;
0 ignored issues
show
The variable $content_false should be in camel caps format.
Loading history...
58 2
        }
59
        if ($preset == 'string') {
0 ignored issues
show
Expected 1 lines after "if", found 0.
Loading history...
Operator == is disallowed, use === instead.
Loading history...
60 2
            return (isset($markers[$variable]) && is_string($markers[$variable]) && strlen($markers[$variable]))
0 ignored issues
show
Useless parentheses.
Loading history...
Function is_string() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Function strlen() should not be referenced via a fallback global name, but via a use statement.
Loading history...
61 2
                ? $content_true
0 ignored issues
show
The variable $content_true should be in camel caps format.
Loading history...
62 2
                : $content_false;
0 ignored issues
show
The variable $content_false should be in camel caps format.
Loading history...
63 2
        }
64
        if ($preset == 'int') {
0 ignored issues
show
Operator == is disallowed, use === instead.
Loading history...
65
            return (isset($markers[$variable]) && intval($markers[$variable]) <> 0)
0 ignored issues
show
Useless parentheses.
Loading history...
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Operator <> is disallowed, use !== instead.
Loading history...
66
                ? $content_true
0 ignored issues
show
The variable $content_true should be in camel caps format.
Loading history...
67
                : $content_false;
0 ignored issues
show
The variable $content_false should be in camel caps format.
Loading history...
68
        }
69
70
        return '';
71
    }
72
73 3
    /**
74
     * @param string $sql
0 ignored issues
show
Incorrect annotations group.
Loading history...
75 3
     * @return mixed
76
     */
77
    public function process($sql)
0 ignored issues
show
Method \ClickHouseDB\Query\Degeneration\Conditions::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...
78 3
    {
79 3
        $markers = $this->bindings;
80 3
81
        // ------ if/else conditions & if[set|int]/else conditions -----
82
        $sql = preg_replace_callback('#\{if(.{0,}?)\s+([^\}]+?)\}(.+?)(\{else\}([^\{]+?)?)?\s*\{\/if}#sui', function ($matches) use ($markers) {
0 ignored issues
show
Function preg_replace_callback() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Closure not using "$this" should be declared static.
Loading history...
83 3
            return self::__ifsets($matches, $markers);
84
        }
85
            , $sql);
0 ignored issues
show
Space found before comma in argument list
Loading history...
86
87
        return $sql;
88
89
        /*
90
         * $ifint var ELSE  {ENDIF}
91
         *
92
         */
93
94
        // stackoverflow
95
        // if(whatever) {  } else { adsffdsa } else if() { }
96
        // /^if\s*\((.*?)\)\s*{(.*?)}(\s*(else|else\s+if\s*\((.*?)\))\s*{(.*?)})*/
97
        // if (condition_function(params)) {
98
        //     statements;
99
        //}
100
        // if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*;
101
        // @if\s*\(\s*([^)]*)\s*\)\s*(((?!@if|@endif).)*)\s*(?:@else\s*(((?!@if|@endif).)*))?\s*@endif
102
        // @if \s* \( \s* ([^)]*)\s*\)\s*(((?!@if|@endif).)*)\s*(?:@else\s*(((?!@if|@endif).)*))?\s*@endif
103
        // [^}]
104
105
        //        // 3. process if conditions
106
        //        $sql = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) {
107
        //            list($condition, $variable, $content) = $matches;
108
        //            if (isset($markers[$variable]) && ($markers[$variable] || is_numeric($markers[$variable]))) {
109
        //                return $content;
110
        //            }
111
        //        }, $sql);
112
113
        // 1. process if[set|int]/else conditions
114
        //        $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{else}(.+?)\{/if}#sui', function($matches) use ($markers) {return  self::__ifsets($matches, $markers, true); }, $sql);
115
        //        $sql = preg_replace_callback('#\{if(.{1,}?)\s(.+?)}(.+?)\{/if}#sui', function($matches) use ($markers) { return self::__ifsets($matches, $markers, false); }, $sql);
116
    }
0 ignored issues
show
Expected 0 blank lines after function; 1 found
Loading history...
117
118
}
0 ignored issues
show
There must be exactly 0 empty lines before class closing brace.
Loading history...
119