Passed
Push — master ( bab9dc...bf7b05 )
by Gabriel
02:36
created

Condition::and_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Nip\Database\Query\Condition;
5
6
use Nip\Database\Query\AbstractQuery as Query;
7
8
class Condition
9
{
10
    protected $_string;
11
    protected $_values;
12
    protected $_query;
13
14
    /**
15
     * @param string $string
16
     */
17 13
    public function __construct($string, $values = array())
18
    {
19 13
        $this->_string = $string;
20 13
        $this->_values = $values;
21 13
    }
22
23 6
    public function __toString()
24
    {
25 6
        return $this->getString();
26
    }
27
28 12
    public function getString()
29
    {
30 12
        return $this->parseString($this->_string, $this->_values);
31
    }
32
33
    /**
34
     * Parses $string and replaces all instances of "?" with corresponding $values
35
     *
36
     * @param string $string
37
     * @param array $values
38
     * @return string
39
     */
40 12
    public function parseString($string, $values)
41
    {
42 12
        $positions = [];
43 12
        $pos = 0;
0 ignored issues
show
Unused Code introduced by
$pos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44 12
        $offset = 0;
45
46 12
        while (($pos = strpos($string, "?", $offset)) !== false) {
47 3
            $positions[] = $pos;
48 3
            $offset = $pos + 1;
49
        }
50
51 12
        $count = count($positions);
52
53 12
        if ($count == 1) {
54 3
            $values = array($values);
55
        }
56
57 12
        for ($i = 0; $i < $count; $i++) {
58 3
            $value = $values[$i];
59 3
            if ($value instanceof Query) {
60 1
                $value = $this->parseValueQuery($value);
61 2
            } elseif (is_array($value)) {
62 1
                foreach ($value as $key => $subvalue) {
63 1
                    if (trim($subvalue) != '') {
64 1
                        $value[$key] = is_numeric($subvalue) ? $subvalue : $this->getQuery()->getManager()->getAdapter()->quote($subvalue);
65
                    } else {
66 1
                        unset($value[$key]);
67
                    }
68
                }
69 1
                $value = "(" . implode(", ", $value) . ")";
70 1
            } elseif (is_numeric($value)) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
71
            } else {
72 1
                $value = $this->getQuery()->getManager()->getAdapter()->quote($values[$i]);
73
            }
74 3
            $string = substr_replace($string, $value, strpos($string, '?'), 1);
75
        }
76
77 12
        return $string;
78
    }
79
80
    /**
81
     * @param Query $value
82
     */
83 1
    protected function parseValueQuery($value)
84
    {
85 1
        return "(" . $value->assemble() . ")";
86
    }
87
88
    /**
89
     * @return Query
90
     */
91 1
    public function getQuery()
92
    {
93 1
        return $this->_query;
94
    }
95
96
    /**
97
     * @param Query $query
98
     * @return $this
99
     */
100 13
    public function setQuery($query)
101
    {
102 13
        $this->_query = $query;
103
104 13
        return $this;
105
    }
106
107
    /**
108
     * @param Condition $condition
109
     */
110 5
    public function and_($condition)
111
    {
112 5
        return new AndCondition($this, $condition);
113
    }
114
115
    /**
116
     * @param Condition $condition
117
     */
118 4
    public function or_($condition)
119
    {
120 4
        return new OrCondition($this, $condition);
121
    }
122
123 7
    public function protectCondition($condition)
124
    {
125 7
        return strpos($condition, ' AND ') || strpos($condition, ' OR ') ? '(' . $condition . ')' : $condition;
126
    }
127
}
128