From::build()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0261
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Sql\Component;
5
6
use Sirius\Sql\Bindings;
7
8
class From extends Component
9
{
10
    protected $bindings;
11
12
    protected $list = [];
13
14 17
    public function __construct(Bindings $bindings)
15
    {
16 17
        $this->bindings = $bindings;
17 17
    }
18
19 17
    public function table(string $ref): void
20
    {
21 17
        $this->list[] = [$ref];
22 17
    }
23
24 2
    public function join(string $join, string $ref, string $condition = '', ...$bindingsInline): void
25
    {
26 2
        $condition = ltrim($condition);
27
28 2
        if ($condition !== ''
29 2
            && strtoupper(substr($condition, 0, 3)) !== 'ON '
30 2
            && strtoupper(substr($condition, 0, 6)) !== 'USING '
31
        ) {
32 2
            $condition = 'ON ' . $condition;
33
        }
34
35 2
        if (! empty($bindingsInline)) {
36 1
            $condition = $this->bindings->sprintf($condition, ...$bindingsInline);
37
        }
38
39 2
        end($this->list);
40 2
        $end                = key($this->list);
41 2
        $this->list[$end][] = "    {$join} {$ref} {$condition}";
42 2
    }
43
44 16
    public function build(): string
45
    {
46 16
        if (empty($this->list)) {
47
            return '';
48
        }
49
50 16
        $from = [];
51 16
        foreach ($this->list as $list) {
52 16
            $from[] = array_shift($list) . $this->indent($list);
53
        }
54
55 16
        return PHP_EOL . 'FROM' . $this->indentCsv($from);
56
    }
57
}
58