Passed
Pull Request — master (#57)
by Šimon
02:22
created

StrictQuoteLine::quoteValue()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 55
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.0671

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 24
cts 27
cp 0.8889
rs 7.8235
c 0
b 0
f 0
cc 7
eloc 26
nc 1
nop 1
crap 7.0671

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
2
namespace ClickHouseDB\Quote;
3
4
use ClickHouseDB\Exception\QueryException;
5
6
class StrictQuoteLine
7
{
8
9
    private $preset=[
10
        'CSV'=>[
11
            'EnclosureArray'=>'"',
12
            'EncodeEnclosure'=>'"',
13
            'Enclosure'=>'"',
14
            'Null'=>"\\N",
15
            'Delimiter'=>",",
16
            'TabEncode'=>false,
17
        ],
18
        'Insert'=>[
19
            'EnclosureArray'=>'',
20
            'EncodeEnclosure'=>'\\',
21
            'Enclosure'=>'\'',
22
            'Null'=>"NULL",
23
            'Delimiter'=>",",
24
            'TabEncode'=>false,
25
        ],
26
        'TSV'=>[
27
            'EnclosureArray'=>'',
28
            'EncodeEnclosure'=>'',
29
            'Enclosure'=>'\\',
30
            'Null'=>" ",
31
            'Delimiter'=>"\t",
32
            'TabEncode'=>true,
33
        ],
34
    ];
35
    private $settings=[];
36
37 2
    public function __construct($format)
38
    {
39 2
        if (empty($this->preset[$format]))
40
        {
41
            throw new QueryException("Unsupport format encode line:".$format);
42
        }
43 2
        $this->settings=$this->preset[$format];
44 2
    }
45 6
    public function quoteRow($row)
46
    {
47 6
        return implode($this->settings['Delimiter'],$this->quoteValue($row));
48
    }
49 6
    public function quoteValue($row)
50
    {
51 6
        $enclosure = $this->settings['Enclosure'];
52 6
        $delimiter = $this->settings['Delimiter'];
53 6
        $encode = $this->settings['EncodeEnclosure'];
54 6
        $encodeArray = $this->settings['EnclosureArray'];
55 6
        $null = $this->settings['Null'];
56 6
        $tabEncode=$this->settings['TabEncode'];
57
58
59
        $quote = function ($value) use ($enclosure,$delimiter,$encode,$encodeArray,$null,$tabEncode) {
60
61
62
63 6
            $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...
64
65 6
            $enclosure_esc = preg_quote($enclosure, '/');
66
67 6
            $encode_esc = preg_quote($encode, '/');
68
69 6
            $type = gettype($value);
70
71 6
            if ($type == 'integer' || $type == 'double') {
72 6
                return strval($value);
73
            }
74
75 6
            if (is_string($value)) {
76 6
                if ($tabEncode)
77
                {
78 1
                    return str_replace(["\t","\n"],['\\t','\\n'],$value);
79
                }
80
81 6
                $value = strval($value);
82 6
                $value = preg_replace('/('.$enclosure_esc.'|'.$encode_esc.')/',$encode_esc.'\1', $value);
83 6
                return $enclosure . $value . $enclosure;
84
            }
85
86 3
            if (is_array($value)) {
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 3
                $result_array = FormatLine::Insert($value);
92
93 3
                return $encodeArray . '[' . $result_array . ']' .$encodeArray;
94
            }
95
96
            if (null === $value) {
97
                return $null;
98
            }
99
100
            return $value;
101 6
        };
102
103 6
        return array_map($quote, $row);
104
    }
105
106
107
}
108