Passed
Push — refactor/improve-static-analys... ( bb4224...d678be )
by
unknown
07:03
created

CompilesJoins::extractTableAndAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 2
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
8
use Illuminate\Database\Query\Expression;
9
use LaravelFreelancerNL\Aranguent\Query\Builder;
10
11
trait CompilesJoins
12
{
13
    /**
14
     * @param $join
15
     * @param Builder $query
16
     * @return array
17
     */
18 15
    public function extractTableAndAlias(Builder $query, $join): array
19
    {
20 15
        if ($join->table instanceof Expression) {
21 15
            $tableParts = [];
22 15
            preg_match("/(^.*) as (.*?)$/", $join->table->getValue($query->grammar), $tableParts);
23
            $table = $tableParts[1];
24
            $alias = $tableParts[2];
25 15
26
            $query->registerTableAlias($join->table, $alias);
27
28 13
            return [$table, $alias];
29
        }
30 13
31 13
        $table = $this->wrapTable($join->table);
0 ignored issues
show
Bug introduced by
It seems like wrapTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        /** @scrutinizer ignore-call */ 
32
        $table = $this->wrapTable($join->table);
Loading history...
32 13
        $alias = $query->generateTableAlias($join->table);
33 13
        $query->registerTableAlias($join->table, $alias);
34 13
35
        return [$table, $alias];
36 13
    }
37
38
    /**
39 1
     * Compile the "join" portions of the query.
40
     *
41 1
     * @param IlluminateQueryBuilder $query
42 1
     * @param  array  $joins
43 1
     * @return string
44
     */
45 1
    protected function compileJoins(IlluminateQueryBuilder $query, $joins)
46 1
    {
47 1
        return collect($joins)->map(function ($join) use ($query) {
0 ignored issues
show
Bug introduced by
$joins of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        return collect(/** @scrutinizer ignore-type */ $joins)->map(function ($join) use ($query) {
Loading history...
48 1
            return match ($join->type) {
49
                'cross' => $this->compileCrossJoin($query, $join),
50 1
                'left' => $this->compileLeftJoin($query, $join),
51 1
                default => $this->compileInnerJoin($query, $join),
52 1
            };
53 1
        })->implode(' ');
54 1
    }
55
56 1
    protected function compileCrossJoin(IlluminateQueryBuilder $query, $join)
57
    {
58
        assert($query instanceof Builder);
59
60 1
        $table = $this->wrapTable($join->table);
61
        $alias = $query->generateTableAlias($join->table);
62
        $query->registerTableAlias($join->table, $alias);
63 1
64
        return 'FOR ' . $alias . ' IN ' . $table;
65 1
    }
66 1
67 1
    protected function compileInnerJoin(IlluminateQueryBuilder $query, $join)
68
    {
69 1
        assert($query instanceof Builder);
70
71
        list($table, $alias) = $this->extractTableAndAlias($query, $join);
72
73
        $filter = $this->compileWheres($join);
0 ignored issues
show
Bug introduced by
It seems like compileWheres() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        /** @scrutinizer ignore-call */ 
74
        $filter = $this->compileWheres($join);
Loading history...
74
75
        $aql = 'FOR ' . $alias . ' IN ' . $table;
76
        if ($filter) {
77
            $aql .= ' ' . $filter;
78
        }
79
80
        return $aql;
81
    }
82
83
    protected function compileLeftJoin(IlluminateQueryBuilder $query, $join)
84
    {
85
        assert($query instanceof Builder);
86
87
        list($table, $alias) = $this->extractTableAndAlias($query, $join);
88
89
        $filter = $this->compileWheres($join);
90
91
        $resultsToJoin = 'FOR ' . $alias . ' IN ' . $table;
92
        if ($filter) {
93
            $resultsToJoin .= ' ' . $filter;
94
        }
95
        $resultsToJoin .= ' RETURN ' . $alias;
96
97
        $aql = 'LET ' . $alias . 'List = (' . $resultsToJoin . ')';
98
        $aql .= ' FOR ' . $alias . ' IN (LENGTH(' . $alias . 'List) > 0) ? ' . $alias . 'List : [{}]';
99
100
        return $aql;
101
    }
102
}
103