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

src/Quote/StrictQuoteLine.php (105 issues)

1
<?php
0 ignored issues
show
Missing declare(strict_types=1).
Loading history...
2
namespace ClickHouseDB\Quote;
0 ignored issues
show
Expected 1 lines before namespace statement, found 0.
Loading history...
3
4
use ClickHouseDB\Exception\QueryException;
5
use ClickHouseDB\Query\Expression\Expression;
6
use ClickHouseDB\Type\NumericType;
7
use function array_map;
0 ignored issues
show
Expected 1 lines between different types of use statement, found 0.
Loading history...
8
use function is_array;
9
use function is_float;
10
use function is_int;
11
use function is_string;
12
use function preg_replace;
13
use function str_replace;
14
15
class StrictQuoteLine
16
{
17
    private $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...
Property \ClickHouseDB\Quote\StrictQuoteLine::$preset does not have @var annotation for its value.
Loading history...
18
        'CSV'=>[
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
19
            'EnclosureArray'=>'"',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
20
            'EncodeEnclosure'=>'"',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
21
            'Enclosure'=>'"',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
22
            'Null'=>"\\N",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \\N 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...
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
23
            'Delimiter'=>",",
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...
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
24
            'TabEncode'=>false,
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
25
        ],
26
        'Insert'=>[
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
27
            'EnclosureArray'=>'',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
28
            'EncodeEnclosure'=>'\\',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
29
            'Enclosure'=>'\'',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
30
            'Null'=>"NULL",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal NULL 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...
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
31
            'Delimiter'=>",",
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...
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
32
            'TabEncode'=>false,
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
33
        ],
34
        'TSV'=>[
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
35
            'EnclosureArray'=>'',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
36
            'EncodeEnclosure'=>'',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
37
            'Enclosure'=>'\\',
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
38
            'Null'=>" ",
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...
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
39
            'Delimiter'=>"\t",
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
40
            'TabEncode'=>true,
0 ignored issues
show
Expected at least 1 space before "=>"; 0 found
Loading history...
Expected at least 1 space after "=>"; 0 found
Loading history...
41
        ],
42
    ];
43
    private $settings = [];
0 ignored issues
show
Property \ClickHouseDB\Quote\StrictQuoteLine::$settings does not have @var annotation for its value.
Loading history...
44
45 3
    public function __construct($format)
0 ignored issues
show
Method \ClickHouseDB\Quote\StrictQuoteLine::__construct() does not have parameter type hint nor @param annotation for its parameter $format.
Loading history...
46
    {
47 3
        if (empty($this->preset[$format]))
48
        {
49
            throw new QueryException("Unsupport format encode line:" . $format);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Unsupport format encode line: 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...
50
        }
51
52 3
        $this->settings = $this->preset[$format];
53 3
    }
54
55
    /**
56
     * @param $row
0 ignored issues
show
Incorrect annotations group.
Loading history...
Missing parameter name
Loading history...
57
     * @param bool $skipEncode
0 ignored issues
show
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteRow() has useless @param annotation for parameter $skipEncode.
Loading history...
58
     * @return string
59
     */
60 12
    public function quoteRow($row,bool $skipEncode=false )
0 ignored issues
show
Expected 1 space between comma and type hint "bool"; 0 found
Loading history...
Incorrect spacing between argument "$skipEncode" and equals sign; expected 1 but found 0
Loading history...
Incorrect spacing between default value and equals sign for argument "$skipEncode"; expected 1 but found 0
Loading history...
Expected 0 spaces before closing parenthesis; 1 found
Loading history...
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteRow() does not have parameter type hint nor @param annotation for its parameter $row.
Loading history...
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteRow() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
61
    {
62 12
        return implode($this->settings['Delimiter'], $this->quoteValue($row,$skipEncode));
0 ignored issues
show
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
63
    }
64
65
    /**
66
     * @param $row
0 ignored issues
show
Incorrect annotations group.
Loading history...
Missing parameter name
Loading history...
67
     * @param bool $skipEncode
0 ignored issues
show
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteValue() has useless @param annotation for parameter $skipEncode.
Loading history...
68
     * @return array
0 ignored issues
show
@return annotation of method \ClickHouseDB\Quote\StrictQuoteLine::quoteValue() does not specify type hint for items of its traversable return value.
Loading history...
69
     */
70 12
    public function quoteValue($row,bool $skipEncode=false)
0 ignored issues
show
Expected 1 space between comma and type hint "bool"; 0 found
Loading history...
Incorrect spacing between argument "$skipEncode" and equals sign; expected 1 but found 0
Loading history...
Incorrect spacing between default value and equals sign for argument "$skipEncode"; expected 1 but found 0
Loading history...
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteValue() does not have parameter type hint nor @param annotation for its parameter $row.
Loading history...
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteValue() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
71
    {
72 12
        $enclosure = $this->settings['Enclosure'];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 7 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...
73 12
        $delimiter = $this->settings['Delimiter'];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 7 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...
74 12
        $encodeEnclosure = $this->settings['EncodeEnclosure'];
75 12
        $encodeArray = $this->settings['EnclosureArray'];
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...
76 12
        $null = $this->settings['Null'];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 12 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...
77 12
        $tabEncode = $this->settings['TabEncode'];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 7 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...
78
79
        $quote = function($value) use ($enclosure, $delimiter, $encodeEnclosure, $encodeArray, $null, $tabEncode, $skipEncode) {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
80 12
            $delimiter_esc = preg_quote($delimiter, '/');
0 ignored issues
show
The assignment to $delimiter_esc is dead and can be removed.
Loading history...
Function preg_quote() should not be referenced via a fallback global name, but via a use statement.
Loading history...
The variable $delimiter_esc should be in camel caps format.
Loading history...
81
82 12
            $enclosure_esc = preg_quote($enclosure, '/');
0 ignored issues
show
Function preg_quote() should not be referenced via a fallback global name, but via a use statement.
Loading history...
The variable $enclosure_esc should be in camel caps format.
Loading history...
83
84 12
            $encode_esc = preg_quote($encodeEnclosure, '/');
0 ignored issues
show
Function preg_quote() should not be referenced via a fallback global name, but via a use statement.
Loading history...
The variable $encode_esc should be in camel caps format.
Loading history...
85
86 12
            $encode = true;
87 12
            if ($value instanceof NumericType) {
0 ignored issues
show
Expected 1 lines after "if", found 0.
Loading history...
88 1
                $encode = false;
89
            }
90 12
            if ($value instanceof Expression) {
91 1
                $encode = $value->needsEncoding();
92
            }
93
94 12
            if (is_array($value)) {
95
                // Arrays are formatted as a list of values separated by commas in square brackets.
96
                // Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
97
                // single quotation marks with the same screening rules as above.
98
                // as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes.
99
100 5
                $value       = array_map(
101
                    function ($v) use ($enclosure_esc, $encode_esc) {
0 ignored issues
show
The variable $enclosure_esc should be in camel caps format.
Loading history...
The variable $encode_esc should be in camel caps format.
Loading history...
102 5
                        return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v;
0 ignored issues
show
The variable $enclosure_esc should be in camel caps format.
Loading history...
The variable $encode_esc should be in camel caps format.
Loading history...
103 5
                    },
104 5
                    $value
105
                );
106 5
                $resultArray = FormatLine::Insert($value,($encodeEnclosure==='\\'?true:false));
0 ignored issues
show
Expected at least 1 space before "==="; 0 found
Loading history...
Expected at least 1 space after "==="; 0 found
Loading history...
Expected at least 1 space before "?"; 0 found
Loading history...
Expected at least 1 space after "?"; 0 found
Loading history...
Useless ternary operator.
Loading history...
Expected at least 1 space before ":"; 0 found
Loading history...
Expected at least 1 space after ":"; 0 found
Loading history...
107 5
                return $encodeArray . '[' . $resultArray . ']' . $encodeArray;
0 ignored issues
show
Expected 1 lines before "return", found 0.
Loading history...
108
            }
109
110 12
            $value = ValueFormatter::formatValue($value, false);
111
112 12
            if (is_float($value) || is_int($value)) {
113 10
                return (string) $value;
114
            }
115
116 12
            if (is_string($value) && $encode) {
117 10
                if ($tabEncode) {
118 1
                    return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
119
                }
120
121 10
                if (!$skipEncode)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Expected 1 space(s) after NOT operator; 0 found
Loading history...
122 10
                $value = $this->encodeString($value, $enclosure_esc, $encode_esc);
0 ignored issues
show
The variable $enclosure_esc should be in camel caps format.
Loading history...
The variable $encode_esc should be in camel caps format.
Loading history...
123
124 10
                return $enclosure . $value . $enclosure;
125
            }
126
127 3
            if ($value === null) {
128 1
                return $null;
129
            }
130
131 2
            return $value;
132 12
        };
133
134 12
        return array_map($quote, $row);
135
    }
136
137
    /**
138
     * @return string
139
     */
140 10
    public function encodeString(string $value, string $enclosureEsc, string $encodeEsc)
0 ignored issues
show
Method \ClickHouseDB\Quote\StrictQuoteLine::encodeString() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
141
    {
142 10
        return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value);
143
    }
144
}
145