1 | <?php |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
2 | namespace ClickHouseDB\Quote; |
||
0 ignored issues
–
show
|
|||
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
|
|||
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...
|
|||
18 | 'CSV'=>[ |
||
0 ignored issues
–
show
|
|||
19 | 'EnclosureArray'=>'"', |
||
0 ignored issues
–
show
|
|||
20 | 'EncodeEnclosure'=>'"', |
||
0 ignored issues
–
show
|
|||
21 | 'Enclosure'=>'"', |
||
0 ignored issues
–
show
|
|||
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 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 ( 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: 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...
|
|||
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 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 ( 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: 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...
|
|||
24 | 'TabEncode'=>false, |
||
0 ignored issues
–
show
|
|||
25 | ], |
||
26 | 'Insert'=>[ |
||
0 ignored issues
–
show
|
|||
27 | 'EnclosureArray'=>'', |
||
0 ignored issues
–
show
|
|||
28 | 'EncodeEnclosure'=>'\\', |
||
0 ignored issues
–
show
|
|||
29 | 'Enclosure'=>'\'', |
||
0 ignored issues
–
show
|
|||
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 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 ( 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: 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 | '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 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 ( 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: 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 | 'TabEncode'=>false, |
||
0 ignored issues
–
show
|
|||
33 | ], |
||
34 | 'TSV'=>[ |
||
0 ignored issues
–
show
|
|||
35 | 'EnclosureArray'=>'', |
||
0 ignored issues
–
show
|
|||
36 | 'EncodeEnclosure'=>'', |
||
0 ignored issues
–
show
|
|||
37 | 'Enclosure'=>'\\', |
||
0 ignored issues
–
show
|
|||
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 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 ( 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: 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 | 'Delimiter'=>"\t", |
||
0 ignored issues
–
show
|
|||
40 | 'TabEncode'=>true, |
||
0 ignored issues
–
show
|
|||
41 | ], |
||
42 | ]; |
||
43 | private $settings = []; |
||
0 ignored issues
–
show
|
|||
44 | |||
45 | 3 | public function __construct($format) |
|
0 ignored issues
–
show
|
|||
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 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 ( 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: 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
|
|||
57 | * @param bool $skipEncode |
||
0 ignored issues
–
show
|
|||
58 | * @return string |
||
59 | */ |
||
60 | 12 | public function quoteRow($row,bool $skipEncode=false ) |
|
0 ignored issues
–
show
|
|||
61 | { |
||
62 | 12 | return implode($this->settings['Delimiter'], $this->quoteValue($row,$skipEncode)); |
|
0 ignored issues
–
show
|
|||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param $row |
||
0 ignored issues
–
show
|
|||
67 | * @param bool $skipEncode |
||
0 ignored issues
–
show
|
|||
68 | * @return array |
||
0 ignored issues
–
show
|
|||
69 | */ |
||
70 | 12 | public function quoteValue($row,bool $skipEncode=false) |
|
0 ignored issues
–
show
|
|||
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
|
|||
80 | 12 | $delimiter_esc = preg_quote($delimiter, '/'); |
|
0 ignored issues
–
show
|
|||
81 | |||
82 | 12 | $enclosure_esc = preg_quote($enclosure, '/'); |
|
0 ignored issues
–
show
|
|||
83 | |||
84 | 12 | $encode_esc = preg_quote($encodeEnclosure, '/'); |
|
0 ignored issues
–
show
|
|||
85 | |||
86 | 12 | $encode = true; |
|
87 | 12 | if ($value instanceof NumericType) { |
|
0 ignored issues
–
show
|
|||
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
|
|||
102 | 5 | return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v; |
|
0 ignored issues
–
show
|
|||
103 | 5 | }, |
|
104 | 5 | $value |
|
105 | ); |
||
106 | 5 | $resultArray = FormatLine::Insert($value,($encodeEnclosure==='\\'?true:false)); |
|
0 ignored issues
–
show
|
|||
107 | 5 | return $encodeArray . '[' . $resultArray . ']' . $encodeArray; |
|
0 ignored issues
–
show
|
|||
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
|
|||
122 | 10 | $value = $this->encodeString($value, $enclosure_esc, $encode_esc); |
|
0 ignored issues
–
show
|
|||
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
|
|||
141 | { |
||
142 | 10 | return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value); |
|
143 | } |
||
144 | } |
||
145 |