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

ValueFormatter::formatValue()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 13
nc 16
nop 2
dl 0
loc 27
ccs 14
cts 14
cp 1
crap 11
rs 7.3166
c 0
b 0
f 0

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\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