Completed
Pull Request — master (#949)
by Grégoire
02:40
created

InlineParameterFormatter::formatParameter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Types\Type;
9
use function array_map;
10
use function implode;
11
use function is_array;
12
use function is_bool;
13
use function is_int;
14
use function is_string;
15
use function sprintf;
16
17
/**
18
 * The InlineParameterFormatter class is responsible for formatting SQL query parameters to a string
19
 * for display output.
20
 *
21
 * @internal
22
 */
23
final class InlineParameterFormatter implements ParameterFormatter
24
{
25
    /** @var Connection */
26
    private $connection;
27
28 1
    public function __construct(Connection $connection)
29
    {
30 1
        $this->connection = $connection;
31 1
    }
32
33
    /**
34
     * @param mixed[] $params
35
     * @param mixed[] $types
36
     */
37 1
    public function formatParameters(array $params, array $types) : string
38
    {
39 1
        if ($params === []) {
40
            return '';
41
        }
42
43 1
        $formattedParameters = [];
44
45 1
        foreach ($params as $key => $value) {
46 1
            $type = $types[$key] ?? 'string';
47
48 1
            $formattedParameter = '[' . $this->formatParameter($value, $type) . ']';
49
50 1
            $formattedParameters[] = is_string($key)
51 1
                ? sprintf(':%s => %s', $key, $formattedParameter)
52 1
                : $formattedParameter;
53
        }
54
55 1
        return sprintf('with parameters (%s)', implode(', ', $formattedParameters));
56
    }
57
58
    /**
59
     * @param string|int $value
60
     * @param string|int $type
61
     *
62
     * @return string|int
63
     */
64 1
    private function formatParameter($value, $type)
65
    {
66 1
        if (is_string($type) && Type::hasType($type)) {
67 1
            return Type::getType($type)->convertToDatabaseValue(
68 1
                $value,
69 1
                $this->connection->getDatabasePlatform()
70
            );
71
        }
72
73 1
        return $this->parameterToString($value);
74
    }
75
76
    /**
77
     * @param int[]|bool[]|string[]|array|int|string|bool $value
78
     */
79 1
    private function parameterToString($value) : string
80
    {
81 1
        if (is_array($value)) {
82
            return implode(', ', array_map(function ($value) : string {
83 1
                return $this->parameterToString($value);
84 1
            }, $value));
85
        }
86
87 1
        if (is_int($value) || is_string($value)) {
88 1
            return (string) $value;
89
        }
90
91 1
        if (is_bool($value)) {
0 ignored issues
show
introduced by
The condition is_bool($value) is always true.
Loading history...
92 1
            return $value === true ? 'true' : 'false';
93
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 91 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
94
    }
95
}
96