Passed
Push — master ( 6456b5...bd1840 )
by Gabriel
10:45 queued 10s
created

Insert::parseData()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 2
b 0
f 0
nc 12
nop 0
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
1
<?php
2
3
4
namespace Nip\Database\Query;
5
6
/**
7
 * Class Insert
8
 * @package Nip\Database\Query
9
 */
10
class Insert extends AbstractQuery
11
{
12
    protected $_cols;
13
14
    protected $_values;
15
16
    /**
17
     * @return string
18
     */
19 2
    public function assemble()
20
    {
21 2
        $return = "INSERT INTO " . $this->protect($this->getTable());
22 2
        $return .= $this->parseCols();
23 2
        $return .= $this->parseValues();
24 2
        $return .= $this->parseOnDuplicate();
25
26 2
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return returns the type string which is incompatible with the return type mandated by Nip\Database\Query\AbstractQuery::assemble() of null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
27
    }
28
29
    /**
30
     * @return string
31
     */
32 2
    public function parseCols()
33
    {
34 2
        if (is_array($this->parts['data'][0])) {
35 2
            $this->setCols(array_keys($this->parts['data'][0]));
36
        }
37 2
        return $this->_cols ? ' (' . implode(',', array_map([$this, 'protect'], $this->_cols)) . ')' : '';
38
    }
39
40
    /**
41
     * @param array|string $cols
42
     * @return $this
43
     */
44 2
    public function setCols($cols = null)
45
    {
46 2
        $this->_cols = $cols;
47 2
        return $this;
48
    }
49
50
    /**
51
     * @return string|false
52
     */
53 2
    public function parseValues()
54
    {
55 2
        if ($this->_values instanceof AbstractQuery) {
56
            return ' ' . (string) $this->_values;
57 2
        } elseif (is_array($this->parts['data'])) {
0 ignored issues
show
introduced by
The condition is_array($this->parts['data']) is always false.
Loading history...
58 2
            return $this->parseData();
59
        }
60
        return false;
61
    }
62
63
    /**
64
     * Parses INSERT data
65
     *
66
     * @return string
67
     */
68 2
    protected function parseData()
69
    {
70 2
        $values = [];
71 2
        foreach ($this->parts['data'] as $key => $data) {
0 ignored issues
show
Bug introduced by
The expression $this->parts['data'] of type null is not traversable.
Loading history...
72 2
            foreach ($data as $value) {
73 2
                if (!is_array($value)) {
74 2
                    $value = [$value];
75
                }
76
77 2
                foreach ($value as $insertValue) {
78 2
                    $values[$key][] = $this->getManager()->getAdapter()->quote($insertValue);
79
                }
80
            }
81
        }
82 2
        foreach ($values as &$value) {
83 2
            $value = "(" . implode(", ", $value) . ")";
84
        }
85
86 2
        return ' VALUES ' . implode(', ', $values);
87
    }
88
89
    /**
90
     * @return string
91
     */
92 2
    public function parseOnDuplicate()
93
    {
94 2
        if ($this->hasPart('onDuplicate')) {
95 1
            $update = $this->getManager()->newUpdate();
96
97 1
            $onDuplicates = $this->getPart('onDuplicate');
98 1
            $data = [];
99 1
            foreach ($onDuplicates as $onDuplicate) {
100 1
                foreach ($onDuplicate as $key => $value) {
101 1
                    $data[$key] = $value;
102
                }
103
            }
104 1
            $update->data($data);
105
106 1
            return " ON DUPLICATE KEY UPDATE {$update->parseUpdate()}";
107
        }
108 1
        return '';
109
    }
110
111
    public function setValues($values)
112
    {
113
        $this->_values = $values;
114
115
        return $this;
116
    }
117
118 1
    public function onDuplicate($value)
119
    {
120 1
        $this->addPart('onDuplicate', $value);
121 1
    }
122
}
123