Issues (119)

src/Query/Update.php (3 issues)

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
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
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