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

QueryCondition::__construct()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.013

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 20
cts 21
cp 0.9524
rs 7.3166
c 0
b 0
f 0
cc 11
nc 11
nop 3
crap 11.013

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