Failed Conditions
Push — refactor/improve-static-analys... ( bdf823...46faab )
by Bas
10:08
created

BuildsJoins::rightJoinSub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use Closure;
8
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
9
use Illuminate\Database\Query\Expression;
10
use LaravelFreelancerNL\Aranguent\Query\Builder;
11
use LaravelFreelancerNL\Aranguent\Query\JoinClause;
12
use LogicException;
13
14
trait BuildsJoins
15
{
16
    /**
17
     * Add a right join to the query.
18
     *
19
     * @param  \Illuminate\Contracts\Database\Query\Expression|string  $table
20
     * @param  \Closure|string  $first
21
     * @param  string|null  $operator
22
     * @param  \Illuminate\Contracts\Database\Query\Expression|string|null  $second
23
     * @return $this
24
     */
25
    public function rightJoin($table, $first, $operator = null, $second = null)
26
    {
27
        // TODO: right outer joins should be doable by basically transforming them to a left outer join.
28
        // The right join would need to be checked before generating the FOR clause on the first called collection
29
        // with the filter after that.
30
        // FOR x IN rightJoinCollection
31
        //  FOR x IN firstCollection
32
        //    FILTER ...
33
        throw new LogicException('This database driver does not support the rightJoin method.');
34
    }
35
36
    /**
37
     * Add a subquery right join to the query.
38
     *
39
     * @param  \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string  $query
40
     * @param  string  $as
41
     * @param  \Closure|string  $first
42
     * @param  string|null  $operator
43
     * @param  \Illuminate\Contracts\Database\Query\Expression|string|null  $second
44
     * @return $this
45
     */
46
    public function rightJoinSub($query, $as, $first, $operator = null, $second = null)
47
    {
48
        throw new LogicException('This database driver does not support the rightJoinSub method.');
49
    }
50
51
    /**
52
     * Add a "right join where" clause to the query.
53
     *
54
     * @param  \Illuminate\Contracts\Database\Query\Expression|string  $table
55
     * @param  \Closure|string  $first
56
     * @param  string  $operator
57
     * @param  \Illuminate\Contracts\Database\Query\Expression|string  $second
58
     * @return $this
59
     */
60
    public function rightJoinWhere($table, $first, $operator, $second)
61
    {
62
        throw new LogicException('This database driver does not support the rightJoinWhere method.');
63
    }
64
65
66
    /**
67
     * Add a join clause to the query.
68
     *
69
     * The boolean argument flag is part of this method's API in Laravel.
70
     *
71
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
72
     *
73
     * @param  mixed  $table
74
     * @param  \Closure|string  $first
75
     * @param  string|null  $operator
76
     * @param  string|null  $second
77
     * @param  string  $type
78
     * @param  bool  $where
79
     */
80
    public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): Builder
81
    {
82
        $join = $this->newJoinClause($this, $type, $table);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...ry\Concerns\BuildsJoins is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $parentQuery of LaravelFreelancerNL\Aran...sJoins::newJoinClause(). ( Ignorable by Annotation )

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

82
        $join = $this->newJoinClause(/** @scrutinizer ignore-type */ $this, $type, $table);
Loading history...
83
84
        // If the first "column" of the join is really a Closure instance the developer
85
        // is trying to build a join with a complex "on" clause containing more than
86
        // one condition, so we'll add the join and call a Closure with the query.
87
        if ($first instanceof Closure) {
88
            $first($join);
89
90
            $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...
91
        }
92
        if (!$first instanceof Closure) {
93
            // If the column is simply a string, we can assume the join simply has a basic
94
            // "on" clause with a single condition. So we will just build the join with
95
            // this simple join clauses attached to it. There is not a join callback.
96
97
            //where and on are the same for aql
98
            $method = $where ? 'where' : 'on';
99
100
            $this->joins[] = $join->$method($first, $operator, $second);
101
        }
102
103
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...ry\Concerns\BuildsJoins which includes types incompatible with the type-hinted return LaravelFreelancerNL\Aranguent\Query\Builder.
Loading history...
104
    }
105
106
    /**
107
     * Add a subquery join clause to the query.
108
     *
109
     * @param  \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string  $query
110
     * @param  string  $as
111
     * @param  \Closure|string  $first
112
     * @param  string|null  $operator
113
     * @param  string|null  $second
114
     * @param  string  $type
115
     * @param  bool  $where
116
     * @return $this
117
     *
118
     * @throws \InvalidArgumentException
119
     */
120
    public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
121
    {
122
        $query->importTableAliases($this);
0 ignored issues
show
Bug introduced by
The method importTableAliases() does not exist on Closure. ( Ignorable by Annotation )

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

122
        $query->/** @scrutinizer ignore-call */ 
123
                importTableAliases($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
        $query->importTableAliases([$as => $as]);
124
125
        [$query, $bindings] = $this->createSub($query);
126
127
        $this->bindings['join'] = array_merge($this->bindings['join'], $bindings);
0 ignored issues
show
Bug Best Practice introduced by
The property bindings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
128
129
        return  $this->join(new Expression('(' . $query . ') as ' . $as), $first, $operator, $second, $type, $where);
130
    }
131
132
133
    /**
134
     * Get a new join clause.
135
     *
136
     * @param  string  $type
137
     * @param  string  $table
138
     */
139
    protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $table): JoinClause
140
    {
141
        return new JoinClause($parentQuery, $type, $table);
142
    }
143
144
    /**
145
     * Creates a subquery and parse it.
146
     *
147
     * @param  \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string  $query
148
     * @return array
149
     */
150
    protected function createSub($query)
151
    {
152
        // If the given query is a Closure, we will execute it while passing in a new
153
        // query instance to the Closure. This will give the developer a chance to
154
        // format and work with the query before we cast it to a raw SQL string.
155
        if ($query instanceof Closure) {
156
            $callback = $query;
157
158
            $callback($query = $this->forSubQuery());
0 ignored issues
show
Bug introduced by
It seems like forSubQuery() 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

158
            $callback($query = $this->/** @scrutinizer ignore-call */ forSubQuery());
Loading history...
159
        }
160
161
        return $this->parseSub($query);
0 ignored issues
show
Bug introduced by
It seems like parseSub() 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

161
        return $this->/** @scrutinizer ignore-call */ parseSub($query);
Loading history...
162
    }
163
164
}
165