Associative   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 17.86 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 20
loc 112
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 20 20 4
A getQueryPartial() 0 11 3
A replaceQueryPartials() 0 16 2
A injectParameterPartials() 0 13 2
A arraySpliceAssoc() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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