Passed
Push — v2 ( 3f2240 )
by Berend
04:06
created

QueryCondition::__toString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
namespace miBadger\Query;
10
11
class QueryCondition implements QueryExpression
12
{
13
	private $leftOperand;
14
15
	private $rightOperand;
16
17
	private $operator;
18
19
	private $binding;
20
21 15
	public function __construct($left, $operator, $right)
22
	{
23 15
		$this->leftOperand = $left;
24 15
		$this->operator = $operator;
25 15
		$this->binding = null;
26
27
		switch ($operator) {
28 15
			case '<':
29 15
			case '>':
30 15
			case '=':
31 9
			case '<>':
32 9
			case 'LIKE':
33 5
			case 'NOT LIKE':
34 4
			case 'IS':
35 4
			case 'IS NOT':
36 12
				$this->rightOperand = $right;
37 12
				break;
38
39 3
			case 'IN':
40 3
				if (is_string($right)) {
41 1
					$this->rightOperand = explode(', ', $right);
42
				} else {
43 2
					$this->rightOperand = $right;
44
				}
45 3
				break;
46
47
			default:
48
				throw new QueryException(sprintf("Unsupported operator \"%s\"", $operator));
49
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
50
		}
51 15
	}
52
53 7
	public function getFlattenedConditions()
54
	{
55 7
		return [$this];
56
	}
57
58 7
	public function bind(Query $query)
59
	{
60 7
		if (is_array($this->rightOperand)) {
61 1
			$this->binding = $query->addBindings('where', $this->rightOperand);
62
		} else {
63 6
			$this->binding = $query->addBinding('where', $this->rightOperand);
64
		}
65 7
	}
66
67 15
	public function __toString()
68
	{
69 15
		if ($this->binding === null) {
70 8
			$rhs = $this->rightOperand;
71
			// throw new QueryException("Currently unbound conditions are not supported!");
72
		} else {
73 7
			$rhs = $this->binding;
74
		}
75
76 15
		if (is_array($rhs)) {
77 3
			$rhs = sprintf('(%s)', implode(', ', $rhs));
78
		}
79
80 15
		return sprintf('%s %s %s', $this->leftOperand, $this->operator, $rhs);
81
	}
82
}
83