Completed
Push — v2 ( 4037a7...349d7f )
by Berend
02:22
created

QueryPredicate::__toString()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.005

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 17
cp 0.9412
rs 9.2408
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 5.005
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
	/* @var string The kind of predicate (AND or OR) */
14
	private $type;
15
16
	/* @var Array The list of subclauses that are combined by this predicate. */
17
	private $conditions;
18
19 5
	public function __construct($type, QueryExpression $left, QueryExpression ...$others)
20
	{
21
		switch ($type) {
22 5
			case 'AND':
23 2
			case 'OR':
24 4
				$this->type = $type;
25 4
				break;
26
27 1
			case 'NOT':
28 1
				if (!empty($others)) {
29
					throw new QueryException("NOT Operator can only accept 1 argument");
30
				}
31 1
				$this->type = $type;
32 1
				break;
33
				
34
			default:
35
				throw new QueryException(sprintf("Invalid predicate operator \"%s\"", $type));
36
		}
37
38 5
		$this->conditions = array_merge([$left], $others);
39 5
	}
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44 2
	public function getFlattenedConditions()
45
	{
46 2
		$conditions = [];
47
48 2
		foreach ($this->conditions as $condition) {
49 2
			$conditions = array_merge($conditions, $condition->getFlattenedConditions());
50
		}
51
52 2
		return $conditions;
53
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58 5
	public function __toString()
59
	{
60 5
		$conditionSql = [];
61 5
		foreach ($this->conditions as $cond) {
62 5
			$conditionSql[] = sprintf('( %s )', (string) $cond);
63
		}
64
		
65 5
		$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...
66 5
		switch ($this->type) {
67 5
			case 'AND':
68 3
				$sql = join($conditionSql, ' AND ');
69 3
				break;
70 2
			case 'OR':
71 1
				$sql = join($conditionSql, ' OR ');
72 1
				break;
73 1
			case 'NOT':
74 1
				$sql = sprintf('NOT %s', $conditionSql[0]);
75 1
				break;
76
			default:
77
				throw new QueryException(sprintf("Invalid predicate operator \"%s\"", $this->type));
78
		}
79 5
		return $sql;
80
	}
81
}
82