Passed
Push — master ( cc4828...00e84f )
by Igor
10:05
created

src/Query/Query.php (31 issues)

1
<?php
2
3
namespace ClickHouseDB\Query;
4
5
use ClickHouseDB\Exception\QueryException;
6
use function sizeof;
7
8
class Query
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $sql;
14
15
    /**
16
     * @var string|null
17
     */
18
    protected $format = null;
19
20
    /**
21
     * @var array
22
     */
23
    private $degenerations = [];
24
25
    private $supportFormats=[
0 ignored issues
show
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
Expected at least 1 space before "="; 0 found
Loading history...
Expected at least 1 space after "="; 0 found
Loading history...
26
        "FORMAT\\s+TSVRaw",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TSVRaw does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
27
        "FORMAT\\s+TSVWithNamesAndTypes",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TSVWithNamesAndTypes does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
28
        "FORMAT\\s+TSVWithNames",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TSVWithNames does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
29
        "FORMAT\\s+TSV",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TSV does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
30
        "FORMAT\\s+Vertical",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+Vertical does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
31
        "FORMAT\\s+JSONCompact",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+JSONCompact does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
32
        "FORMAT\\s+JSONEachRow",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+JSONEachRow does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
33
        "FORMAT\\s+TSKV",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TSKV does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
34
        "FORMAT\\s+TabSeparatedWithNames",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TabSeparatedWithNames does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
35
        "FORMAT\\s+TabSeparatedWithNamesAndTypes",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TabSeparatedWithNamesAndTypes does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
36
        "FORMAT\\s+TabSeparatedRaw",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TabSeparatedRaw does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
37
        "FORMAT\\s+BlockTabSeparated",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+BlockTabSeparated does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
38
        "FORMAT\\s+CSVWithNames",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+CSVWithNames does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
39
        "FORMAT\\s+CSV",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+CSV does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
40
        "FORMAT\\s+JSON",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+JSON does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
41
        "FORMAT\\s+TabSeparated"
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal FORMAT\\s+TabSeparated does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
42
    ];
43
44
    /**
45
     * Query constructor.
46
     * @param string $sql
47
     * @param array $degenerations
0 ignored issues
show
Expected 2 spaces after parameter type; 1 found
Loading history...
48
     */
49 44
    public function __construct($sql, $degenerations = [])
50
    {
51 44
        if (!trim($sql))
0 ignored issues
show
Expected 1 space(s) after NOT operator; 0 found
Loading history...
52
        {
53 1
            throw new QueryException('Empty Query');
54
        }
55 43
        $this->sql = $sql;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 11 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...
56 43
        $this->degenerations = $degenerations;
57 43
    }
58
59
    /**
60
     * @param string|null $format
61
     */
62 37
    public function setFormat($format)
63
    {
64 37
        $this->format = $format;
65 37
    }
0 ignored issues
show
Expected 1 blank line after function; 2 found
Loading history...
66
67
68 37
    private function applyFormatQuery()
69
    {
70
        // FORMAT\s(\w)*$
71 37
        if (null === $this->format) {
72
            return false;
73
        }
74 37
        $supportFormats = implode("|",$this->supportFormats);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal | does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
75
76 37
        $this->sql = trim($this->sql);
77 37
        if (substr($this->sql, -1) == ';') {
78
            $this->sql = substr($this->sql, 0, -1);
79
        }
80
81 37
        $matches = [];
82 37
        if (preg_match_all('%(' . $supportFormats . ')%ius', $this->sql, $matches)) {
0 ignored issues
show
Blank line found at start of control structure
Loading history...
83
84
            // skip add "format json"
85 2
            if (isset($matches[0]))
86
            {
0 ignored issues
show
Blank line found at start of control structure
Loading history...
87
88 2
                $this->format = trim(str_ireplace('format', '', $matches[0][0]));
89
0 ignored issues
show
Blank line found at end of control structure
Loading history...
90
            }
91
        } else {
92 35
            $this->sql = $this->sql . ' FORMAT ' . $this->format;
93
        }
94
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 6 empty lines
Loading history...
95
96
97
98
99
100 37
    }
0 ignored issues
show
Function closing brace must go on the next line following the body; found 6 blank lines before brace
Loading history...
101
102
    /**
103
     * @return null|string
104
     */
105 43
    public function getFormat()
106
    {
0 ignored issues
show
Expected 0 blank lines after opening function brace; 1 found
Loading history...
107
108 43
        return $this->format;
109
    }
110
111 43
    public function toSql()
112
    {
113 43
        if ($this->format !== null) {
114 37
            $this->applyFormatQuery();
115
        }
116
117 43
        if (sizeof($this->degenerations))
0 ignored issues
show
The use of function sizeof() is forbidden; use count() instead
Loading history...
118
        {
119 43
            foreach ($this->degenerations as $degeneration)
120
            {
121 43
                if ($degeneration instanceof Degeneration) {
122 43
                    $this->sql = $degeneration->process($this->sql);
123
                }
124
            }
125
        }
126
127 43
        return $this->sql;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function __toString()
134
    {
135
        return $this->toSql();
136
    }
137
}
138