Where   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
ccs 23
cts 23
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A where() 0 6 1
A toString() 0 10 2
A buildConditionString() 0 14 3
A addCondition() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Queries\Snippets;
6
7
use Puzzle\QueryBuilder\Snippet;
8
use Puzzle\QueryBuilder\Condition;
9
use Puzzle\QueryBuilder\Traits\EscaperAware;
10
11
class Where implements Snippet
12
{
13
    use EscaperAware;
14
15
    private
16
        $conditions;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $conditions.

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...
17
18 40
    public function __construct(Condition $condition = null)
19
    {
20 40
        $this->conditions = array();
21
22 40
        if($condition instanceof Condition)
23
        {
24 4
            $this->addCondition($condition);
25
        }
26 40
    }
27
28 24
    public function where(Condition $condition): self
29
    {
30 24
        $this->addCondition($condition);
31
32 24
        return $this;
33
    }
34
35 34
    public function toString(): string
36
    {
37 34
        $conditionString = $this->buildConditionString();
38 34
        if(empty($conditionString))
39
        {
40 8
            return '';
41
        }
42
43 27
        return sprintf('WHERE %s', $conditionString);
44
    }
45
46 34
    private function buildConditionString(): string
47
    {
48 34
        $whereConditions = array();
49 34
        foreach($this->conditions as $condition)
50
        {
51 28
            $conditionString = $condition->toString($this->escaper);
52 28
            if(! empty($conditionString))
53
            {
54 27
                $whereConditions[] = $conditionString;
55
            }
56
        }
57
58 34
        return implode(' AND ', $whereConditions);
59
    }
60
61 28
    private function addCondition(Condition $condition): void
62
    {
63 28
        $this->conditions[] = $condition;
64 28
    }
65
}
66