Completed
Push — v2 ( 3f2240...40dd63 )
by Berend
03:07
created

QueryPredicate   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 61
ccs 21
cts 35
cp 0.6
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 5
A getFlattenedConditions() 0 10 2
A __toString() 0 23 5
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 QueryPredicate implements QueryExpression
12
{
13
	private $type;
14
15
	private $conditions;
16
17 3
	public function __construct($type, QueryExpression $left, QueryExpression ...$others)
18
	{
19
		switch ($type) {
20 3
			case 'AND':
21
			case 'OR':
22 3
				$this->type = $type;
23 3
				break;
24
			case 'NOT':
25
				if (!empty($others)) {
26
					throw new QueryException("NOT Operator can only accept 1 argument");
27
				}
28
				$this->type = $type;
29
				break;
30
			default:
31
				throw new QueryException(sprintf("Invalid predicate operator \"%s\"", $type));
32
		}
33
34 3
		$this->conditions = array_merge([$left], $others);
35 3
	}
36
37 2
	public function getFlattenedConditions()
38
	{
39 2
		$conditions = [];
40
41 2
		foreach ($this->conditions as $condition) {
42 2
			$conditions = array_merge($conditions, $condition->getFlattenedConditions());
43
		}
44
45 2
		return $conditions;
46
	}
47
48 3
	public function __toString()
49
	{
50 3
		$conditionSql = [];
51 3
		foreach ($this->conditions as $cond) {
52 3
			$conditionSql[] = sprintf('( %s )', (string) $cond);
53
		}
54
		
55 3
		$sql = '';
0 ignored issues
show
Unused Code introduced by
$sql 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...
56 3
		switch ($this->type) {
57 3
			case 'AND':
58 3
				$sql = join($conditionSql, ' AND ');
59 3
				break;
60
			case 'OR':
61
				$sql = join($conditionSql, ' OR ');
62
				break;
63
			case 'NOT':
64
				$sql = sprintf('NOT %s', $conditionSql[0]);
65
				break;
66
			default:
67
				throw new QueryException(sprintf("Invalid predicate operator \"%s\"", $this->type));
68
		}
69 3
		return $sql;
70
	}
71
}
72