Completed
Branch develop (c2aa4c)
by Anton
05:17
created

QueryInterpolator::prepareParameter()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 4.8197
cc 10
eloc 18
nc 10
nop 1

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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Entities;
10
11
use Spiral\Database\Injections\ParameterInterface;
12
13
/**
14
 * Simple helper class used to interpolate query with given values. To be used for profiling and
15
 * debug purposes only, unsafe SQL are generated!
16
 */
17
class QueryInterpolator
18
{
19
    /**
20
     * Helper method used to interpolate SQL query with set of parameters, must be used only for
21
     * development purposes and never for real query.
22
     *
23
     * @param string $query
24
     * @param array  $parameters Parameters to be binded into query.
25
     * @return mixed
26
     */
27
    public static function interpolate($query, array $parameters = [])
28
    {
29
        if (empty($parameters)) {
30
            return $query;
31
        }
32
33
        //Flattening first
34
        $parameters = self::flattenParameters($parameters);
35
36
        //Let's prepare values so they looks better
37
        foreach ($parameters as &$parameter) {
38
            $parameter = self::prepareParameter($parameter);
39
            unset($parameter);
40
        }
41
42
        reset($parameters);
43
        if (!is_int(key($parameters))) {
44
            //Associative array
45
            return \Spiral\interpolate($query, $parameters, '', '');
46
        }
47
48
        foreach ($parameters as $parameter) {
49
            $query = preg_replace('/\?/', $parameter, $query, 1);
50
        }
51
52
        return $query;
53
    }
54
55
    /**
56
     * Flatten all parameters into simple array.
57
     *
58
     * @param array $parameters
59
     * @return array
60
     */
61
    protected static function flattenParameters(array $parameters)
62
    {
63
        $flatten = [];
64
        foreach ($parameters as $parameter) {
65
            if ($parameter instanceof ParameterInterface) {
66
                $flatten = array_merge($flatten, $parameter->flatten());
67
                continue;
68
            }
69
70
            if (is_array($parameter)) {
71
                $flatten = array_merge($flatten, $parameter);
72
            }
73
74
            $flatten[] = $parameter;
75
        }
76
77
        return $flatten;
78
    }
79
80
    /**
81
     * Normalize parameter value to be interpolated.
82
     *
83
     * @param mixed $parameter
84
     * @return string
85
     */
86
    protected static function prepareParameter($parameter)
87
    {
88
        if ($parameter instanceof ParameterInterface) {
89
            return self::prepareParameter($parameter->getValue());
90
        }
91
92
        switch (gettype($parameter)) {
93
            case "boolean":
94
                return $parameter ? 'true' : 'false';
95
            case "integer":
96
                return $parameter + 0;
97
            case "NULL":
98
                return 'NULL';
99
            case "double":
100
                return sprintf('%F', $parameter);
101
            case "string":
102
                return "'" . addcslashes($parameter, "'") . "'";
103
            case 'object':
104
                if (method_exists($parameter, '__toString')) {
105
                    return "'" . addcslashes((string)$parameter, "'") . "'";
106
                }
107
        }
108
109
        return "[UNRESOLVED]";
110
    }
111
}