StrictQuoteLine   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 97.87%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 64
c 2
b 1
f 0
dl 0
loc 130
ccs 46
cts 47
cp 0.9787
rs 10
wmc 19

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A quoteRow() 0 3 1
C quoteValue() 0 67 15
A encodeString() 0 3 1
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types=1).
Loading history...
2
namespace ClickHouseDB\Quote;
0 ignored issues
show
introduced by
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
introduced by
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
Coding Style introduced by
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...
introduced by
Property \ClickHouseDB\Quote\StrictQuoteLine::$preset does not have @var annotation for its value.
Loading history...
18
        'CSV'=>[
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
19
            'EnclosureArray'=>'"',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
20
            'EncodeEnclosure'=>'"',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
21
            'Enclosure'=>'"',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
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...
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
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...
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
24
            'TabEncode'=>false,
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
25
        ],
26
        'Insert'=>[
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
27
            'EnclosureArray'=>'',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
28
            'EncodeEnclosure'=>'\\',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
29
            'Enclosure'=>'\'',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
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...
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
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...
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
32
            'TabEncode'=>false,
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
33
        ],
34
        'TSV'=>[
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
35
            'EnclosureArray'=>'',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
36
            'EncodeEnclosure'=>'',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
37
            'Enclosure'=>'\\',
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
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...
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
39
            'Delimiter'=>"\t",
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
40
            'TabEncode'=>true,
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "=>"; 0 found
Loading history...
41
        ],
42
    ];
43
    private $settings = [];
0 ignored issues
show
introduced by
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
introduced by
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
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Missing parameter type
Loading history...
57
     * @param bool $skipEncode
0 ignored issues
show
introduced by
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
Coding Style introduced by
Expected 1 space between comma and type hint "bool"; 0 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$skipEncode" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$skipEncode"; expected 1 but found 0
Loading history...
Coding Style introduced by
Expected 0 spaces before closing parenthesis; 1 found
Loading history...
introduced by
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteRow() does not have parameter type hint nor @param annotation for its parameter $row.
Loading history...
introduced by
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
introduced by
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
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Missing parameter type
Loading history...
67
     * @param bool $skipEncode
0 ignored issues
show
introduced by
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteValue() has useless @param annotation for parameter $skipEncode.
Loading history...
68
     * @return array
0 ignored issues
show
introduced by
@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
Coding Style introduced by
Expected 1 space between comma and type hint "bool"; 0 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$skipEncode" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$skipEncode"; expected 1 but found 0
Loading history...
introduced by
Method \ClickHouseDB\Quote\StrictQuoteLine::quoteValue() does not have parameter type hint nor @param annotation for its parameter $row.
Loading history...
introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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 12
        $quote = function($value) use ($enclosure, $delimiter, $encodeEnclosure, $encodeArray, $null, $tabEncode, $skipEncode) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
80 12
            $delimiter_esc = preg_quote($delimiter, '/');
0 ignored issues
show
Unused Code introduced by
The assignment to $delimiter_esc is dead and can be removed.
Loading history...
introduced by
Function preg_quote() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The variable $delimiter_esc should be in camel caps format.
Loading history...
81
82 12
            $enclosure_esc = preg_quote($enclosure, '/');
0 ignored issues
show
introduced by
Function preg_quote() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The variable $enclosure_esc should be in camel caps format.
Loading history...
83
84 12
            $encode_esc = preg_quote($encodeEnclosure, '/');
0 ignored issues
show
introduced by
Function preg_quote() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
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
introduced by
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 5
                    function ($v) use ($enclosure_esc, $encode_esc) {
0 ignored issues
show
Coding Style introduced by
The variable $enclosure_esc should be in camel caps format.
Loading history...
Coding Style introduced by
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
Coding Style introduced by
The variable $enclosure_esc should be in camel caps format.
Loading history...
Coding Style introduced by
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
Coding Style introduced by
Expected at least 1 space before "==="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "==="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space before "?"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "?"; 0 found
Loading history...
introduced by
Useless ternary operator.
Loading history...
Coding Style introduced by
Expected at least 1 space before ":"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after ":"; 0 found
Loading history...
107 5
                return $encodeArray . '[' . $resultArray . ']' . $encodeArray;
0 ignored issues
show
introduced by
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
            $value = is_bool($value) ? ($value ? 'true' : 'false') : $value;
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...
117 10
118 1
            if (is_string($value) && $encode) {
119
                if ($tabEncode) {
120
                    return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
121 10
                }
122 10
123
                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...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
124 10
                $value = $this->encodeString($value, $enclosure_esc, $encode_esc);
0 ignored issues
show
Coding Style introduced by
The variable $enclosure_esc should be in camel caps format.
Loading history...
Coding Style introduced by
The variable $encode_esc should be in camel caps format.
Loading history...
125
126
                return $enclosure . $value . $enclosure;
127 3
            }
128 1
129
            if ($value === null) {
130
                return $null;
131 2
            }
132 12
133
            return $value;
134 12
        };
135
136
        return array_map($quote, $row);
137
    }
138
139
    /**
140 10
     * @return string
141
     */
142 10
    public function encodeString(string $value, string $enclosureEsc, string $encodeEsc)
0 ignored issues
show
introduced by
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...
143
    {
144
        return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value);
145
    }
146
}
147