Passed
Pull Request — master (#86)
by
unknown
02:39
created

StrictQuoteLine::quoteValue()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 56
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.0601

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 56
ccs 25
cts 28
cp 0.8929
rs 8.5546
c 0
b 0
f 0
cc 7
nc 1
nop 1
crap 7.0601

How to fix   Long Method   

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