Failed Conditions
Push — master ( 2f7299...1dbc04 )
by Igor
03:34
created

StrictQuoteLine::quoteValue()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 65
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 35
nc 1
nop 2
dl 0
loc 65
ccs 36
cts 36
cp 1
crap 13
rs 6.6166
c 1
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...
introduced by
An error occurred during processing; checking has been aborted. The error message was: "if" without curly braces is not supported.
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;
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
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...
18
        'CSV'=>[
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 4 space(s) but found 0
Loading history...
19
            'EnclosureArray'=>'"',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 2 space(s) but found 0
Loading history...
20
            'EncodeEnclosure'=>'"',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 1 space(s) but found 0
Loading history...
21
            'Enclosure'=>'"',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
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
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 12 space(s) but found 0
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
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
Loading history...
24
            'TabEncode'=>false,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
Loading history...
25
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 15 space(s), but found 8.
Loading history...
26
        'Insert'=>[
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 1 space(s) but found 0
Loading history...
27
            'EnclosureArray'=>'',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 2 space(s) but found 0
Loading history...
28
            'EncodeEnclosure'=>'\\',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 1 space(s) but found 0
Loading history...
29
            'Enclosure'=>'\'',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
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
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 12 space(s) but found 0
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
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
Loading history...
32
            'TabEncode'=>false,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
Loading history...
33
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 18 space(s), but found 8.
Loading history...
34
        'TSV'=>[
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 8.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 4 space(s) but found 0
Loading history...
35
            'EnclosureArray'=>'',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 2 space(s) but found 0
Loading history...
36
            'EncodeEnclosure'=>'',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 1 space(s) but found 0
Loading history...
37
            'Enclosure'=>'\\',
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
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
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 12 space(s) but found 0
Loading history...
39
            'Delimiter'=>"\t",
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
Loading history...
40
            'TabEncode'=>true,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
Coding Style introduced by
Array double arrow not aligned correctly; expected 7 space(s) but found 0
Loading history...
41
        ],
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 15 space(s), but found 8.
Loading history...
42
    ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 4.
Loading history...
43
    private $settings = [];
0 ignored issues
show
introduced by
Property \ClickHouseDB\Quote\StrictQuoteLine::$settings does not have @var annotation.
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...
Coding Style Documentation introduced by
Missing doc comment for function __construct()
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
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$row" missing
Loading history...
56
     * @param $row
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Missing parameter name
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...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
58
     * @return string
59
     */
60 12
    public function quoteRow($row,bool $skipEncode=false )
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 for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
Coding Style introduced by
Type hint "row" missing for
Loading history...
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...
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
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$row" missing
Loading history...
66
     * @param $row
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Missing parameter name
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...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
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
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 for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
Coding Style introduced by
Type hint "row" missing for
Loading history...
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...
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
        $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...
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...
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...
85
86 12
            $encode = true;
87 12
            if ($value instanceof NumericType) {
88 1
                $encode = false;
89
            }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
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) {
102 5
                        return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v;
103 5
                    },
104 5
                    $value
105
                );
106 5
                $resultArray = FormatLine::Insert($value,($encodeEnclosure==='\\'?true:false));
107 5
                return $encodeArray . '[' . $resultArray . ']' . $encodeArray;
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
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...
122 10
                $value = $this->encodeString($value, $enclosure_esc, $encode_esc);
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)
141
    {
142 10
        return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value);
143
    }
144
}
145