Associative::replaceQueryPartials()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Foo\Pdo\Statement\Preprocessor\ArrayParameter;
6
7
use Foo\Pdo\Statement\Preprocessor\ArrayParameter;
8
9
class Associative
10
{
11
    /**
12
     * @param string $query
13
     * @param array  $parameters
14
     * @param array  $whereInParameters
15
     *
16
     * @return bool
17
     */
18 3 View Code Duplication
    public function process(string &$query, array &$parameters, array $whereInParameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20 3
        $partials = [];
21 3
        foreach ($whereInParameters as $key => $values) {
22 2
            if (is_numeric($key)) {
23 1
                continue;
24
            }
25
26 1
            $partials[$key] = $this->getQueryPartial($parameters[$key], $key);
27
        }
28
29 3
        if (!$partials) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partials of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
30 2
            return false;
31
        }
32
33 1
        $query      = $this->replaceQueryPartials($query, $partials);
34 1
        $parameters = $this->injectParameterPartials($parameters, $partials);
35
36 1
        return true;
37
    }
38
39
    /**
40
     * @param array  $parameterValues
41
     * @param string $key
42
     *
43
     * @return array
44
     */
45 1
    private function getQueryPartial(array $parameterValues, string $key)
46
    {
47 1
        $paramType = $parameterValues[1] === ArrayParameter::PARAM_INT_ARRAY ? \PDO::PARAM_INT : \PDO::PARAM_STR;
48
49 1
        $queryPartial = [];
50 1
        foreach ($parameterValues[0] as $index => $value) {
51 1
            $queryPartial[$key . '__expanded' . $index] = [$value, $paramType];
52
        }
53
54 1
        return $queryPartial;
55
    }
56
57
    /**
58
     * @param string $query
59
     * @param array  $partials
60
     *
61
     * @return string
62
     */
63 1
    private function replaceQueryPartials(string $query, array $partials)
64
    {
65 1
        foreach ($partials as $key => $inQueryParts) {
66 1
            $inQueryParts = array_map(
67 1
                function ($value) {
68 1
                    return ":{$value}";
69 1
                },
70
                array_keys($inQueryParts)
71
            );
72 1
            $inQuery      = implode(', ', $inQueryParts);
73
74 1
            $query = str_replace(":$key", $inQuery, $query);
75
        }
76
77 1
        return $query;
78
    }
79
80
    /**
81
     * @param array $parameters
82
     * @param array $partials
83
     *
84
     * @return array
85
     */
86 1
    private function injectParameterPartials(array $parameters, array $partials)
87
    {
88 1
        foreach ($partials as $origKey => $replacement) {
89 1
            $parameters = $this->arraySpliceAssoc(
90
                $parameters,
91
                $origKey,
92 1
                1,
93
                $replacement
94
            );
95
        }
96
97 1
        return $parameters;
98
    }
99
100
    /**
101
     * @param array  $input
102
     * @param string $key
103
     * @param int    $length
104
     * @param array  $replacement
105
     *
106
     * @return array
107
     */
108 1
    private function arraySpliceAssoc(array &$input, string $key, int $length, array $replacement)
109
    {
110 1
        $keyIndices = array_flip(array_keys($input));
111 1
        $offset     = $keyIndices[$key];
112
113 1
        $beginning = array_slice($input, 0, $offset, true);
114 1
        $end       = array_slice($input, $offset + $length, null, true);
115
116 1
        $input = $beginning + $replacement + $end;
117
118 1
        return $input;
119
    }
120
}
121