AbstractCompositeCondition::toString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Conditions\Binaries;
6
7
use Puzzle\QueryBuilder\Conditions\CompositeCondition;
8
use Puzzle\QueryBuilder\Condition;
9
use Puzzle\QueryBuilder\Escaper;
10
use Puzzle\QueryBuilder\Conditions\AbstractCondition;
11
12
abstract class AbstractCompositeCondition extends AbstractCondition implements CompositeCondition
13
{
14
    protected
15
        $leftCondition,
1 ignored issue
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $leftCondition.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
        $rightCondition;
17
18 45
    public function __construct(Condition $leftCondition, Condition $rightCondition)
19
    {
20 45
        $this->leftCondition = $leftCondition;
21 45
        $this->rightCondition = $rightCondition;
22 45
    }
23
24 41
    public function toString(Escaper $escaper): string
25
    {
26 41
        if($this->leftCondition->isEmpty())
27
        {
28 11
            return $this->rightCondition->toString($escaper);
29
        }
30
31 37
        if($this->rightCondition->isEmpty())
32
        {
33 10
            return $this->leftCondition->toString($escaper);
34
        }
35
36 33
        return $this->buildCondition($escaper);
37
    }
38
39 40
    public function isEmpty(): bool
40
    {
41 40
        return $this->leftCondition->isEmpty() && $this->rightCondition->isEmpty();
42
    }
43
44 33
    protected function buildPartCondition(Condition $condition, Escaper $escaper): string
45
    {
46 33
        $partCondition = $condition->toString($escaper);
47
48 33
        if($condition instanceof CompositeCondition)
49
        {
50 23
            $partCondition = sprintf('(%s)', $partCondition);
51
        }
52
53 33
        return $partCondition;
54
    }
55
56
    abstract protected function buildCondition(Escaper $escaper): string;
57
}
58