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

QueryCondition   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 72
ccs 34
cts 35
cp 0.9714
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 11
A getFlattenedConditions() 0 4 1
A bind() 0 8 2
A __toString() 0 15 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