FromConverter::convert()   B
last analyzed

Complexity

Conditions 9
Paths 18

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 20
cts 20
cp 1
rs 8.0555
c 0
b 0
f 0
cc 9
nc 18
nop 1
crap 9
1
<?php
2
3
namespace BigShark\SQLToBuilder\Converter;
4
5
class FromConverter extends Converter implements ConverterInterface
6
{
7 102
    public function convert($from)
8
    {
9 102
        $result = [];
10 102
        if ('table' === $from[0]['expr_type']) {
11 102
            $value = $this->getValueWithoutQuotes($from[0], 'table');
12 102
            if (isset($from[0]['alias']) && is_array($from[0]['alias'])) {
13 3
                $value .= ' AS '.$this->getValueWithoutQuotes($from[0]['alias'], 'name');
14
            }
15 102
            $result[] = $this->format('table', [$value]);
16
        }
17 102
        unset($from[0]);
18 102
        foreach ($from as $item) {
19 15
            if (in_array($item['join_type'], ['LEFT', 'RIGHT'], true)) {
20 15
                $table = $this->getValueWithoutQuotes($item, 'table');
21 15
                if (isset($item['alias']) && is_array($item['alias'])) {
22 6
                    $table .= ' AS '.$this->getValueWithoutQuotes($item['alias'], 'name');
23
                }
24 15
                if ('ON' === strtoupper($item['ref_type'])) {
25
                    $args = [
26 15
                        $table,
27 15
                        $this->getValueWithoutQuotes($item['ref_clause'][0], 'base_expr'),
28 15
                        $item['ref_clause'][1]['base_expr'],
29 15
                        $this->getValueWithoutQuotes($item['ref_clause'][2], 'base_expr'),
30
                    ];
31 15
                    $result[] = $this->format(strtolower($item['join_type']).'Join', $args);
32
                }
33
            }
34
        }
35
36 102
        return $result;
37
    }
38
}
39