Values   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 96
Duplicated Lines 11.46 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%
Metric Value
dl 11
loc 96
wmc 15
lcom 1
cbo 4
ccs 51
cts 51
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A values() 0 6 1
B toString() 0 26 4
A buildValuesSetString() 0 15 3
A formatValue() 0 9 2
A escapeValue() 11 11 2
A wrapWithParentheses() 0 4 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
namespace Muffin\Queries\Snippets;
4
5
use Muffin\Snippet;
6
use Muffin\Type;
7
use Muffin\Types;
8
use Muffin\Traits\EscaperAware;
9
use Muffin\Types\String;
10
use Muffin\Traits\TypeGuesser;
11
12
class Values implements Snippet
13
{
14
    use
15
        EscaperAware,
16
        TypeGuesser;
17
18
    private
19
        $values;
20
21 13
    public function __construct($values = null)
22
    {
23 13
        $this->values = array();
24
25 13
        if(! empty($values))
26 13
        {
27 7
            $this->values($values);
28 7
        }
29 13
    }
30
31 12
    public function values(array $values)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
32
    {
33 12
        $this->values[] = $values;
34
35 12
        return $this;
36
    }
37
38 13
    public function toString()
39
    {
40 13
        if(empty($this->values))
41 13
        {
42 1
            throw new \RuntimeException('No values to insert');
43
        }
44
45 12
        $columnsNameList = array_filter(array_keys(reset($this->values)));
46
47 12
        $values = array();
48 12
        foreach($this->values as $valuesSet)
49
        {
50 12
            if($columnsNameList !== array_keys($valuesSet))
51 12
            {
52 1
                throw new \RuntimeException('Cannot insert different schema on the same table.');
53
            }
54
55 12
            $values[] = $this->buildValuesSetString($valuesSet);
56 12
        }
57
58 11
        return sprintf(
59 11
            '%s VALUES %s',
60 11
            $this->wrapWithParentheses(implode(', ', $columnsNameList)),
61 11
            implode(', ', $values)
62 11
        );
63
    }
64
65 12
    private function buildValuesSetString(array $values)
66
    {
67 12
        $valuesSet = array();
68 12
        foreach($values as $columnName => $value)
69
        {
70 12
            if(! empty($columnName))
71 12
            {
72 12
                $type = $this->guessType($columnName, $value);
73
74 12
                $valuesSet[] = $this->formatValue($type, $value);
75 12
            }
76 12
        }
77
78 12
        return $this->wrapWithParentheses(implode(', ', $valuesSet));
79
    }
80
81 12
    private function formatValue(Type $type, $value)
82
    {
83 12
        if(is_null($value))
84 12
        {
85 1
            return 'NULL';
86
        }
87
88 12
        return $this->escapeValue($type, $value);
89
    }
90
91 12 View Code Duplication
    private function escapeValue(Type $type, $value)
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...
92
    {
93 12
        $value = $type->format($value);
94
95 12
        if($type->isEscapeRequired())
96 12
        {
97 11
            $value = $this->escaper->escape($value);
98 11
        }
99
100 12
        return $value;
101
    }
102
103 12
    private function wrapWithParentheses($value)
104
    {
105 12
        return sprintf('(%s)', $value);
106
    }
107
}
108