Completed
Pull Request — master (#87)
by Šimon
10:03 queued 01:01
created

StrictQuoteLine   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 12
eloc 58
dl 0
loc 112
ccs 36
cts 40
cp 0.9
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B quoteValue() 0 61 8
A quoteRow() 0 3 1
A encodeString() 0 3 1
A __construct() 0 7 2
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
namespace ClickHouseDB\Quote;
3
4
use ClickHouseDB\Exception\QueryException;
5
use function array_map;
6
use function is_string;
7
use function preg_replace;
8
use function str_replace;
9
10
class StrictQuoteLine
11
{
0 ignored issues
show
introduced by
There must be exactly 0 empty lines after class opening brace.
Loading history...
12
13
    private $preset = [
0 ignored issues
show
introduced by
Property \ClickHouseDB\Quote\StrictQuoteLine::$preset does not have @var annotation.
Loading history...
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...
14
        'CSV'=>[
15
            'EnclosureArray'=>'"',
16
            'EncodeEnclosure'=>'"',
17
            'Enclosure'=>'"',
18
            '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...
19
            '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...
20
            'TabEncode'=>false,
21
        ],
22
        'Insert'=>[
23
            'EnclosureArray'=>'',
24
            'EncodeEnclosure'=>'\\',
25
            'Enclosure'=>'\'',
26
            '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...
27
            '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...
28
            'TabEncode'=>false,
29
        ],
30
        'TSV'=>[
31
            'EnclosureArray'=>'',
32
            'EncodeEnclosure'=>'',
33
            'Enclosure'=>'\\',
34
            '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...
35
            'Delimiter'=>"\t",
36
            'TabEncode'=>true,
37
        ],
38
    ];
39
    private $settings = [];
0 ignored issues
show
introduced by
Property \ClickHouseDB\Quote\StrictQuoteLine::$settings does not have @var annotation.
Loading history...
40
41 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...
42
    {
43 3
        if (empty($this->preset[$format]))
44
        {
45
            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...
46
        }
47 3
        $this->settings = $this->preset[$format];
48 3
    }
49 7
    public function quoteRow($row)
0 ignored issues
show
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 return type hint nor @return annotation for its return value.
Loading history...
50
    {
51 7
        return implode($this->settings['Delimiter'], $this->quoteValue($row));
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
52
    }
53 7
    public function quoteValue($row)
0 ignored issues
show
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 return type hint nor @return annotation for its return value.
Loading history...
54
    {
55 7
        $enclosure = $this->settings['Enclosure'];
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...
56 7
        $delimiter = $this->settings['Delimiter'];
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...
57 7
        $encode = $this->settings['EncodeEnclosure'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
58 7
        $encodeArray = $this->settings['EnclosureArray'];
59 7
        $null = $this->settings['Null'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
60 7
        $tabEncode = $this->settings['TabEncode'];
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...
61
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
62
63
        $quote = function($value) use ($enclosure, $delimiter, $encode, $encodeArray, $null, $tabEncode) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 blank lines after opening function brace; 3 found
Loading history...
64
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
65
66
67 7
            $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...
68
69 7
            $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...
70
71 7
            $encode_esc = preg_quote($encode, '/');
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...
72
73 7
            $type = gettype($value);
0 ignored issues
show
introduced by
Function gettype() should not be referenced via a fallback global name, but via a use statement.
Loading history...
74
75 7
            if ($type == 'integer' || $type == 'double') {
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
76 7
                return strval($value);
0 ignored issues
show
introduced by
Function strval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
77
            }
78
79 7
            if (is_string($value)) {
80 7
                if ($tabEncode)
81
                {
82 1
                    return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
83
                }
84
85 7
                $value = strval($value);
0 ignored issues
show
introduced by
Function strval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
86 7
                $value = $this->encodeString($value, $enclosure_esc, $encode_esc);
87 7
                return $enclosure . $value . $enclosure;
88
            }
89
90 4
            if (is_array($value)) {
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
91
                // Arrays are formatted as a list of values separated by commas in square brackets.
92
                // Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
93
                // single quotation marks with the same screening rules as above.
94
                // as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes.
95 4
                $value = array_map(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
96
                    function ($v) use ($enclosure_esc, $encode_esc) {
97 4
                        return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v;
98 4
                    },
99 4
                    $value
100
                );
101 4
                $result_array = FormatLine::Insert($value);
102
103 4
                return $encodeArray . '[' . $result_array . ']' . $encodeArray;
104
            }
105
106
            if (null === $value) {
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
107
                return $null;
108
            }
109
110
            return $value;
111 7
        };
112
113 7
        return array_map($quote, $row);
114
    }
115
116
    /**
117
     * @return string
118
     */
119 7
    public function encodeString(string $value, string $enclosureEsc, string $encodeEsc)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Quote\StrictQuoteLine::encodeString() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
120
    {
121 7
        return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value);
122
    }
123
}
124