TBindNames   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 22
c 1
b 0
f 0
dl 0
loc 42
ccs 22
cts 22
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeOf() 0 10 4
A bindFromNamedToQuestions() 0 18 6
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database;
4
5
6
use kalanis\kw_mapper\MapperException;
7
8
9
/**
10
 * trait TBindNames
11
 * @package kalanis\kw_mapper\Storage\Database
12
 */
13
trait TBindNames
14
{
15
    /**
16
     * @param string $query
17
     * @param array<string, mixed> $params
18
     * @throws MapperException
19
     * @return array<string|string[]>
20
     */
21 5
    public function bindFromNamedToQuestions(string $query, array $params): array
22
    {
23 5
        $binds = [];
24 5
        $types = [];
25 5
        if (empty($params)) {
26 1
            return [$query, $binds, $types];
27
        }
28 4
        while (false !== ($pos = strpos($query, ':'))) {
29 3
            $nextSpace = strpos($query, ' ', $pos);
30 3
            $key = ($nextSpace) ? substr($query, $pos, $nextSpace - $pos) : substr($query, $pos);
31 3
            if (!isset($params[$key])) {
32 1
                throw new MapperException(sprintf('Unknown bind for key *%s*', $key));
33
            }
34 2
            $binds[] = $params[$key];
35 2
            $types[] = $this->getTypeOf($params[$key]);
36 2
            $query = substr($query, 0, $pos) . '?' . ( $nextSpace ? substr($query, $nextSpace) : '' );
37
        }
38 3
        return [$query, $binds, $types];
39
    }
40
41
    /**
42
     * @param mixed $var
43
     * @return string
44
     */
45 2
    protected function getTypeOf($var): string
46
    {
47 2
        if (is_bool($var)) {
48 1
            return 'i';
49 2
        } elseif (is_int($var)) {
50 1
            return 'i';
51 2
        } elseif (is_float($var)) {
52 1
            return 'd';
53
        } else {
54 1
            return 's';
55
        }
56
    }
57
}
58