Failed Conditions
Push — master ( fa3649...11c8ec )
by Bas
05:38
created

CompilesJoins::compileLeftJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 2
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 1
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use LaravelFreelancerNL\Aranguent\Query\Builder;
6
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
7
8
trait CompilesJoins
9
{
10
    /**
11
     * Compile the "join" portions of the query.
12
     *
13
     * @param Builder $builder
14
     * @param array   $joins
15
     *
16
     * @return string
17
     */
18 14
    protected function compileJoins(Builder $builder, $joins)
19
    {
20 14
        foreach ($joins as $join) {
21 14
            $compileMethod = 'compile' . ucfirst($join->type) . 'Join';
22 14
            $builder = $this->$compileMethod($builder, $join);
23
        }
24
25 14
        return $builder;
26
    }
27
28 12
    protected function compileInnerJoin(Builder $builder, $join)
29
    {
30 12
        $table = $join->table;
31 12
        $alias = $this->generateTableAlias($table);
0 ignored issues
show
Bug introduced by
It seems like generateTableAlias() 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
        $alias = $this->generateTableAlias($table);
Loading history...
32 12
        $this->registerTableAlias($table, $alias);
0 ignored issues
show
Bug introduced by
It seems like registerTableAlias() 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

32
        $this->/** @scrutinizer ignore-call */ 
33
               registerTableAlias($table, $alias);
Loading history...
33 12
        $builder->aqb = $builder->aqb->for($alias, $table)
34 12
            ->filter($this->compileWheresToArray($join));
0 ignored issues
show
Bug introduced by
It seems like compileWheresToArray() 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

34
            ->filter($this->/** @scrutinizer ignore-call */ compileWheresToArray($join));
Loading history...
35
36 12
        return $builder;
37
    }
38
39 1
    protected function compileLeftJoin(Builder $builder, $join)
40
    {
41 1
        $table = $join->table;
42 1
        $alias = $this->generateTableAlias($table);
43 1
        $this->registerTableAlias($table, $alias);
44
45 1
        $resultsToJoin = (new QueryBuilder())
46 1
            ->for($alias, $table)
47 1
            ->filter($this->compileWheresToArray($join))
48 1
            ->return($alias);
49
50 1
        $builder->aqb = $builder->aqb->let($table, $resultsToJoin)
51 1
            ->for(
52 1
                $alias,
53 1
                $builder->aqb->if(
54 1
                    [$builder->aqb->length($table), '>', 0],
55
                    $table,
56 1
                    '[]'
57
                )
58
            );
59
60 1
        return $builder;
61
    }
62
63 1
    protected function compileCrossJoin(Builder $builder, $join)
64
    {
65 1
        $table = $join->table;
66 1
        $alias = $this->generateTableAlias($table);
67 1
        $builder->aqb = $builder->aqb->for($alias, $table);
68
69 1
        return $builder;
70
    }
71
}
72