ValueFormatter::formatValue()   C
last analyzed

Complexity

Conditions 12
Paths 18

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 15
c 1
b 0
f 0
nc 18
nop 2
dl 0
loc 31
ccs 16
cts 16
cp 1
crap 12
rs 6.9666

How to fix   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
2
3
declare(strict_types=1);
4
5
namespace ClickHouseDB\Quote;
6
7
use ClickHouseDB\Exception\UnsupportedValueType;
8
use ClickHouseDB\Query\Expression\Expression;
9
use ClickHouseDB\Type\Type;
10
use DateTimeInterface;
11
use function addslashes;
0 ignored issues
show
introduced by
Expected 1 lines between different types of use statement, found 0.
Loading history...
12
use function is_bool;
13
use function is_callable;
14
use function is_float;
15
use function is_int;
16
use function is_object;
17
use function is_string;
18
use function sprintf;
19
20
class ValueFormatter
21
{
22
    /**
23
     * @param mixed $value
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
24
     * @param bool $addQuotes
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Method \ClickHouseDB\Quote\ValueFormatter::formatValue() has useless @param annotation for parameter $addQuotes.
Loading history...
25
     * @return mixed
26
     */
27 36
    public static function formatValue($value, bool $addQuotes = true)
28
    {
29 36
        if ($value instanceof DateTimeInterface) {
30 2
            $value = $value->format('Y-m-d H:i:s');
31
        }
32
33 36
        if (is_float($value) || is_int($value) || is_bool($value) || $value === null) {
34 21
            return $value;
35
        }
36
37 31
        if ($value instanceof Type) {
38 1
            return $value->getValue();
39
        }
40
41 31
        if ($value instanceof Expression) {
42 1
            return $value->getValue();
43
        }
44
45 30
        if (is_object($value) && is_callable([$value, '__toString'])) {
46 1
            $value = (string) $value;
47
        }
48
49 30
        if (is_string($value)) {
50 29
            if ($addQuotes) {
51 19
                return self::formatStringParameter(self::escapeString($value));
52
            }
53
54 10
            return $value;
55
        }
56
57 1
        throw UnsupportedValueType::new($value);
58
    }
59
60
    /**
61
     * Escape an string
62
     *
63
     * @param string $value
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
64
     * @return string
65
     */
66 19
    private static function escapeString($value)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Quote\ValueFormatter::escapeString() does not have native type hint for its parameter $value but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Quote\ValueFormatter::escapeString() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
67
    {
68 19
        return addslashes($value);
69
    }
70
71
    /**
72
     * @return string
73
     */
74 19
    private static function formatStringParameter($value)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Quote\ValueFormatter::formatStringParameter() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
introduced by
Method \ClickHouseDB\Quote\ValueFormatter::formatStringParameter() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
75
    {
76 19
        return sprintf("'%s'", $value);
77
    }
78
}
79