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

ValueFormatter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 52
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

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