Update::parseUpdate()   B
last analyzed

Complexity

Conditions 10
Paths 27

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 11.3027

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 3
b 0
f 0
nc 27
nop 0
dl 0
loc 30
ccs 13
cts 17
cp 0.7647
crap 11.3027
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Nip\Database\Query;
4
5
/**
6
 * Class Update
7
 * @package Nip\Database\Query
8
 */
9
class Update extends AbstractQuery
10
{
11
    /**
12
     * @return string
13
     */
14
    public function assemble()
15
    {
16
        $query = 'UPDATE ' . $this->protect($this->getTable()) . ' SET ' . $this->parseUpdate();
0 ignored issues
show
Bug introduced by
Are you sure $this->parseUpdate() of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
        $query = 'UPDATE ' . $this->protect($this->getTable()) . ' SET ' . /** @scrutinizer ignore-type */ $this->parseUpdate();
Loading history...
17
18
        $query .= $this->assembleWhere();
19
        $query .= $this->assembleLimit();
20
21
        return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query 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...
22
    }
23
24
    /**
25
     * @return bool|string
26
     */
27
    public function parseUpdate()
28 1
    {
29
        if (!$this->parts['data']) {
30 1
            return false;
31
        }
32
        $fields = [];
33 1
        foreach ($this->parts['data'] as $data) {
0 ignored issues
show
Bug introduced by
The expression $this->parts['data'] of type void is not traversable.
Loading history...
34 1
            foreach ($data as $key => $values) {
35 1
                if (!is_array($values)) {
36 1
                    $values = [$values];
37
                }
38
                $value = $values[0];
39 1
                $quote = isset($values[1]) ? $values[1] : null;
40 1
41
                if ($value === null) {
42 1
                    $value = 'NULL';
43 1
                } elseif (!is_numeric($value)) {
44
                    if (is_null($quote)) {
45
                        $quote = true;
46 1
                    }
47
                    if ($quote) {
48
                        $value = $this->getManager()->getAdapter()->quote($value);
49
                    }
50
                }
51 1
52
                $fields[] = "{$this->protect($key)} = $value";
53
            }
54
        }
55 1
56
        return implode(", ", $fields);
57
    }
58
}
59