Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

SqlBind::parseSQL()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 30
cts 30
cp 1
rs 8.7752
c 0
b 0
f 0
cc 5
eloc 31
nc 8
nop 3
crap 5

How to fix   Long Method   

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
namespace ByJG\AnyDataset\Store\Helpers;
4
5
use ByJG\Util\Uri;
6
7
/**
8
 * Class to create and manipulate Several Data Types
9
 *
10
 */
11
class SqlBind
12
{
13
14
    /**
15
     * Each provider have your own model for pass parameter.
16
     * This method define how each provider name define the parameters
17
     *
18
     * @param Uri $connData
19
     * @return string
20
     */
21 24
    public static function getParamModel(Uri $connData)
22
    {
23 24
        if ($connData->getQueryPart("parammodel") != "") {
24
            return $connData->getQueryPart("parammodel");
25
        }
26
27 24
        return ":_";
28
    }
29
30
    /**
31
     * Transform generic parameters [[PARAM]] in a parameter recognized by the provider
32
     * name based on current DbParameter array.
33
     *
34
     * @param Uri $connData
35
     * @param string $sql
36
     * @param array $params
37
     * @return array An array with the adjusted SQL and PARAMs
38
     */
39 24
    public static function parseSQL(Uri $connData, $sql, $params = null)
40
    {
41 24
        $paramSubstName = SqlBind::getParamModel($connData);
42
43 24
        $sqlAlter = preg_replace("~'.*?'~", "", $sql);
44 24
        preg_match_all(
45 24
            "/(?<deliStart>\\[\\[|:)(?<param>[\\w\\d]+)(?<deliEnd>\\]\\]|[^\\d\\w]|$)/",
46 24
            $sqlAlter,
47 24
            $matches
48
        );
49
50 24
        $usedParams = [];
51
52 24
        if (is_null($params)) {
53 15
            $params = [];
54
        }
55
56 24
        foreach ($matches['param'] as $paramName) {
57 16
            if (!array_key_exists($paramName, $params)) {
58
                // Remove NON DEFINED parameters
59 2
                $sql = preg_replace(
60
                    [
61 2
                        "/\\[\\[$paramName\\]\\]/",
62 2
                        "/:$paramName([^\\d\\w]|$)/"
63
                    ],
64
                    [
65 2
                        "null",
66
                        "null$2"
67
                    ],
68 2
                    $sql
69
                );
70 2
                continue;
71
            }
72
73 15
            $usedParams[$paramName] = isset($params[$paramName]) ? $params[$paramName] : null;
74 15
            $dbArg = str_replace("_", SqlBind::keyAdj($paramName), $paramSubstName);
75
76 15
            $count = 0;
77 15
            $sql = preg_replace(
78
                [
79 15
                    "/\\[\\[$paramName\\]\\]/",
80 15
                    "/:$paramName([^\\w\\d]|$)/",
81
                ],
82
                [
83 15
                    $dbArg . '',
84 15
                    $dbArg . '$1',
85
                ],
86 15
                $sql,
87 15
                -1,
88 15
                $count
89
            );
90
        }
91
92 24
        return [$sql, $usedParams];
93
    }
94
95 15
    public static function keyAdj($key)
96
    {
97 15
        return str_replace(".", "_", $key);
98
    }
99
}
100