Passed
Pull Request — master (#94)
by Šimon
04:26
created

StrictQuoteLine::quoteValue()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 61
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 1
nop 1
dl 0
loc 61
ccs 33
cts 33
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ClickHouseDB\Type\NumericType;
6
use function array_map;
7
use function is_array;
8
use function is_float;
9
use function is_int;
10
use function is_string;
11
use function preg_replace;
12
use function str_replace;
13
14
class StrictQuoteLine
15
{
16
    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...
17
        'CSV'=>[
18
            'EnclosureArray'=>'"',
19
            'EncodeEnclosure'=>'"',
20
            'Enclosure'=>'"',
21
            '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...
22
            '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...
23
            'TabEncode'=>false,
24
        ],
25
        'Insert'=>[
26
            'EnclosureArray'=>'',
27
            'EncodeEnclosure'=>'\\',
28
            'Enclosure'=>'\'',
29
            '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...
30
            '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...
31
            'TabEncode'=>false,
32
        ],
33
        'TSV'=>[
34
            'EnclosureArray'=>'',
35
            'EncodeEnclosure'=>'',
36
            'Enclosure'=>'\\',
37
            '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...
38
            'Delimiter'=>"\t",
39
            'TabEncode'=>true,
40
        ],
41
    ];
42
    private $settings = [];
0 ignored issues
show
introduced by
Property \ClickHouseDB\Quote\StrictQuoteLine::$settings does not have @var annotation.
Loading history...
43
44 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...
45
    {
46 3
        if (empty($this->preset[$format]))
47
        {
48
            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...
49
        }
50
51 3
        $this->settings = $this->preset[$format];
52 3
    }
53 10
    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...
54
    {
55 10
        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...
56
    }
57 10
    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...
58
    {
59 10
        $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...
60 10
        $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...
61 10
        $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...
62 10
        $encodeArray = $this->settings['EnclosureArray'];
63 10
        $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...
64 10
        $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...
65
66
        $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...
67 10
            $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 10
            $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 10
            $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 10
            $encode = true;
74 10
            if ($value instanceof NumericType) {
75 1
                $encode = false;
76
            }
77
78 10
            if (is_array($value)) {
79
                // Arrays are formatted as a list of values separated by commas in square brackets.
80
                // Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
81
                // single quotation marks with the same screening rules as above.
82
                // as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes.
83 4
                $value = array_map(
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...
84
                    function ($v) use ($enclosure_esc, $encode_esc) {
85 4
                        return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v;
86 4
                    },
87 4
                    $value
88
                );
89 4
                $resultArray = FormatLine::Insert($value);
90
91 4
                return $encodeArray . '[' . $resultArray . ']' . $encodeArray;
92
            }
93
94 10
            $value = ValueFormatter::formatValue($value, false);
95
96 10
            if (is_float($value) || is_int($value)) {
97 9
                return (string) $value;
98
            }
99
100 10
            if (is_string($value) && $encode) {
101 9
                if ($tabEncode) {
102 1
                    return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
103
                }
104
105 9
                $value = $this->encodeString($value, $enclosure_esc, $encode_esc);
106
107 9
                return $enclosure . $value . $enclosure;
108
            }
109
110 2
            if ($value === null) {
111 1
                return $null;
112
            }
113
114 1
            return $value;
115 10
        };
116
117 10
        return array_map($quote, $row);
118
    }
119
120
    /**
121
     * @return string
122
     */
123 9
    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...
124
    {
125 9
        return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value);
126
    }
127
}
128