AbstractCompositeCondition::buildPartCondition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Muffin\Conditions\Binaries;
4
5
use Muffin\Conditions\CompositeCondition;
6
use Muffin\Condition;
7
use Muffin\Escaper;
8
use Muffin\Conditions\AbstractCondition;
9
10
abstract class AbstractCompositeCondition extends AbstractCondition implements CompositeCondition
11
{
12
    protected
13
        $leftCondition,
14
        $rightCondition;
15
16 45
    public function __construct(Condition $leftCondition, Condition $rightCondition)
17
    {
18 45
        $this->leftCondition = $leftCondition;
19 45
        $this->rightCondition = $rightCondition;
20 45
    }
21
22 41
    public function toString(Escaper $escaper)
23
    {
24 41
        if($this->leftCondition->isEmpty())
25 41
        {
26 11
            return $this->rightCondition->toString($escaper);
27
        }
28
29 37
        if($this->rightCondition->isEmpty())
30 37
        {
31 10
            return $this->leftCondition->toString($escaper);
32
        }
33
34 33
        return $this->buildCondition($escaper);
35
    }
36
37 40
    public function isEmpty()
38
    {
39 40
        return $this->leftCondition->isEmpty() && $this->rightCondition->isEmpty();
40
    }
41
42 33
    protected function buildPartCondition(Condition $condition, Escaper $escaper)
43
    {
44 33
        $partCondition = $condition->toString($escaper);
45
46 33
        if($condition instanceof CompositeCondition)
47 33
        {
48 23
            $partCondition = sprintf('(%s)', $partCondition);
49 23
        }
50
51 33
        return $partCondition;
52
    }
53
54
    abstract protected function buildCondition(Escaper $escaper);
55
}
56