Insert::parseData()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 2
b 0
f 0
nc 16
nop 0
dl 0
loc 25
ccs 11
cts 11
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Nip\Database\Query;
4
5
/**
6
 * Class Insert
7
 * @package Nip\Database\Query
8
 */
9
class Insert extends AbstractQuery
10
{
11
    protected $_cols;
12
13
    protected $_values;
14
15
    /**
16
     * @return string
17
     */
18
    public function assemble()
19 2
    {
20
        $return = "INSERT INTO " . $this->protect($this->getTable());
21 2
        $return .= $this->parseCols();
22 2
        $return .= $this->parseValues();
23 2
        $return .= $this->parseOnDuplicate();
24 2
25
        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...
26 2
    }
27
28
    /**
29
     * @return string
30
     */
31
    public function parseCols()
32 2
    {
33
        if (isset($this->parts['data'][0]) && is_array($this->parts['data'][0])) {
34 2
            $this->setCols(array_keys($this->parts['data'][0]));
35 2
        }
36
        return $this->_cols ? ' (' . implode(',', array_map([$this, 'protect'], $this->_cols)) . ')' : '';
37 2
    }
38
39
    /**
40
     * @param array|string $cols
41
     * @return $this
42
     */
43
    public function setCols($cols = null)
44 2
    {
45
        $this->_cols = $cols;
46 2
        return $this;
47 2
    }
48
49
    /**
50
     * @return string|false
51
     */
52
    public function parseValues()
53 2
    {
54
        if ($this->_values instanceof AbstractQuery) {
55 2
            return ' ' . (string) $this->_values;
56
        } elseif (is_array($this->parts['data'])) {
0 ignored issues
show
introduced by
The condition is_array($this->parts['data']) is always false.
Loading history...
57 2
            return $this->parseData();
58 2
        }
59
        return false;
60
    }
61
62
    /**
63
     * Parses INSERT data
64
     *
65
     * @return string
66
     */
67
    protected function parseData()
68 2
    {
69
        $values = [];
70 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...
71 2
            foreach ($data as $value) {
72 2
                if (!is_array($value)) {
73 2
                    $value = [$value];
74 2
                }
75
76
                foreach ($value as $insertValue) {
77 2
78 2
                    if ($insertValue === null) {
79
                        $insertValue = 'NULL';
80
                    } else {
81
                        $insertValue = $this->getManager()->getAdapter()->quote($insertValue);
82 2
                    }
83 2
                    $values[$key][] = $insertValue;
84
                }
85
            }
86 2
        }
87
        foreach ($values as &$value) {
88
            $value = "(" . implode(", ", $value) . ")";
89
        }
90
91
        return ' VALUES ' . implode(', ', $values);
92 2
    }
93
94 2
    /**
95 1
     * @return string
96
     */
97 1
    public function parseOnDuplicate()
98 1
    {
99 1
        if ($this->hasPart('onDuplicate')) {
100 1
            $update = $this->getManager()->newUpdate();
101 1
102
            $onDuplicates = $this->getPart('onDuplicate');
103
            $data = [];
104 1
            foreach ($onDuplicates as $onDuplicate) {
105
                foreach ($onDuplicate as $key => $value) {
106 1
                    $data[$key] = $value;
107
                }
108 1
            }
109
            $update->data($data);
110
111
            return " ON DUPLICATE KEY UPDATE {$update->parseUpdate()}";
112
        }
113
        return '';
114
    }
115
116
    public function setValues($values)
117
    {
118 1
        $this->_values = $values;
119
120 1
        return $this;
121 1
    }
122
123
    public function onDuplicate($value)
124
    {
125
        $this->addPart('onDuplicate', $value);
126
    }
127
}
128