Numeric   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 18.87 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 20
loc 106
ccs 42
cts 43
cp 0.9767
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 20 20 4
A getInQueryPartial() 0 10 2
B replaceQueryPartials() 0 24 4
B injectParameterPartials() 0 21 5

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 Numeric
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->getInQueryPartial($parameters, $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 $parameters
41
     * @param int   $key
42
     *
43
     * @return array
44
     */
45 1
    private function getInQueryPartial(array $parameters, int $key)
46
    {
47 1
        $values = $parameters[$key][0];
48
49 1
        $paramType = $parameters[$key][1] === ArrayParameter::PARAM_INT_ARRAY ? \PDO::PARAM_INT : \PDO::PARAM_STR;
50
51 1
        $queryParts = array_fill(0, count($values), ['?', $paramType]);
52
53 1
        return $queryParts;
54
    }
55
56
    /**
57
     * @param string $query
58
     * @param array  $partials
59
     *
60
     * @return string
61
     */
62 1
    private function replaceQueryPartials(string $query, array $partials)
63
    {
64 1
        $queryParts = explode('?', $query);
65 1
        $lastIndex  = count($queryParts) - 1;
66 1
        $query      = '';
67 1
        foreach ($queryParts as $index => $queryPart) {
68 1
            if ($index == $lastIndex) {
69 1
                $query .= $queryPart;
70 1
            } elseif (array_key_exists($index, $partials)) {
71 1
                $partialIndex = array_map(
72 1
                    function (array $valueArray) {
73 1
                        return $valueArray[0];
74 1
                    },
75 1
                    $partials[$index]
76
                );
77
78 1
                $query .= $queryPart . implode(', ', $partialIndex);
79
            } else {
80 1
                $query .= $queryPart . '?';
81
            }
82
        }
83
84 1
        return $query;
85
    }
86
87
    /**
88
     * @param array $parameters
89
     * @param array $partials
90
     *
91
     * @return array
92
     */
93 1
    private function injectParameterPartials(array $parameters, array $partials)
94
    {
95 1
        $newParameters = [];
96 1
        foreach ($parameters as $parameterKey => $parameter) {
97 1
            if (!array_key_exists($parameterKey, $partials)) {
98 1
                if (is_numeric($parameterKey)) {
99 1
                    $newParameters[] = $parameter;
100
                } else {
101
                    $newParameters[$parameterKey] = $parameter;
102
                }
103
104 1
                continue;
105
            }
106
107 1
            foreach ($partials[$parameterKey] as $partialKey => $partialValues) {
108 1
                $newParameters[] = [$parameter[0][$partialKey], $partialValues[1]];
109
            }
110
        }
111
112 1
        return $newParameters;
113
    }
114
}
115