Passed
Push — master ( ef1328...91a1d1 )
by Bas
13:32 queued 09:25
created

BuildsJoinClauses   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 55
ccs 9
cts 11
cp 0.8182
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A newJoinClause() 0 3 1
A join() 0 24 4
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Closure;
6
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
7
use LaravelFreelancerNL\Aranguent\Query\Builder;
8
use LaravelFreelancerNL\Aranguent\Query\JoinClause;
9
10
trait BuildsJoinClauses
11
{
12
    /**
13
     * Add a join clause to the query.
14
     *
15
     * The boolean argument flag is part of this method's API in Laravel.
16
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
17
     *
18
     * @param mixed          $table
19
     * @param \Closure|string $first
20
     * @param string|null     $operator
21
     * @param string|null     $second
22
     * @param string          $type
23
     * @param bool            $where
24
     *
25
     * @return Builder
26
     */
27 14
    public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): Builder
28
    {
29 14
        $join = $this->newJoinClause($this, $type, $table);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...cerns\BuildsJoinClauses is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $parentQuery of LaravelFreelancerNL\Aran...lauses::newJoinClause(). ( Ignorable by Annotation )

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

29
        $join = $this->newJoinClause(/** @scrutinizer ignore-type */ $this, $type, $table);
Loading history...
30
31
        // If the first "column" of the join is really a Closure instance the developer
32
        // is trying to build a join with a complex "on" clause containing more than
33
        // one condition, so we'll add the join and call a Closure with the query.
34 14
        if ($first instanceof Closure) {
35
            $first($join);
36
37
            $this->joins[] = $join;
0 ignored issues
show
Bug Best Practice introduced by
The property joins does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
        }
39 14
        if (! $first instanceof Closure) {
40
            // If the column is simply a string, we can assume the join simply has a basic
41
            // "on" clause with a single condition. So we will just build the join with
42
            // this simple join clauses attached to it. There is not a join callback.
43
44
            //where and on are the same for aql
45 14
            $method = $where ? 'where' : 'on';
46
47 14
            $this->joins[] = $join->$method($first, $operator, $second);
48
        }
49
50 14
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...cerns\BuildsJoinClauses which includes types incompatible with the type-hinted return LaravelFreelancerNL\Aranguent\Query\Builder.
Loading history...
51
    }
52
53
    /**
54
     * Get a new join clause.
55
     *
56
     * @param IlluminateQueryBuilder $parentQuery
57
     * @param string                 $type
58
     * @param string                 $table
59
     *
60
     * @return JoinClause
61
     */
62 15
    protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $table): JoinClause
63
    {
64 15
        return new JoinClause($parentQuery, $type, $table);
65
    }
66
}
67