1 | <?php |
||||||||
2 | |||||||||
3 | declare(strict_types=1); |
||||||||
4 | |||||||||
5 | namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
||||||||
6 | |||||||||
7 | use Closure; |
||||||||
8 | use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
||||||||
9 | use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
||||||||
10 | use Illuminate\Database\Query\Expression; |
||||||||
11 | use LaravelFreelancerNL\Aranguent\Query\Builder; |
||||||||
12 | use LaravelFreelancerNL\Aranguent\Query\JoinClause; |
||||||||
13 | use LogicException; |
||||||||
14 | |||||||||
15 | trait BuildsJoins |
||||||||
16 | { |
||||||||
17 | /** |
||||||||
18 | * Add a right join to the query. |
||||||||
19 | * |
||||||||
20 | * @param \Illuminate\Contracts\Database\Query\Expression|string $table |
||||||||
21 | * @param Closure|string $first |
||||||||
22 | * @param string|null $operator |
||||||||
23 | * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second |
||||||||
24 | * @return $this |
||||||||
25 | * |
||||||||
26 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||||||||
27 | */ |
||||||||
28 | public function rightJoin($table, $first, $operator = null, $second = null) |
||||||||
29 | { |
||||||||
30 | throw new LogicException('This database driver does not support the rightJoin method.'); |
||||||||
31 | } |
||||||||
32 | |||||||||
33 | /** |
||||||||
34 | * Add a subquery right join to the query. |
||||||||
35 | * |
||||||||
36 | * @param Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|string $query |
||||||||
37 | * @param string $as |
||||||||
38 | * @param Closure|string $first |
||||||||
39 | * @param string|null $operator |
||||||||
40 | * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second |
||||||||
41 | * @return $this |
||||||||
42 | * |
||||||||
43 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||||||||
44 | */ |
||||||||
45 | public function rightJoinSub($query, $as, $first, $operator = null, $second = null) |
||||||||
46 | { |
||||||||
47 | throw new LogicException('This database driver does not support the rightJoinSub method.'); |
||||||||
48 | } |
||||||||
49 | |||||||||
50 | /** |
||||||||
51 | * Add a "right join where" clause to the query. |
||||||||
52 | * |
||||||||
53 | * @param \Illuminate\Contracts\Database\Query\Expression|string $table |
||||||||
54 | * @param Closure|string $first |
||||||||
55 | * @param string $operator |
||||||||
56 | * @param \Illuminate\Contracts\Database\Query\Expression|string $second |
||||||||
57 | * @return $this |
||||||||
58 | * |
||||||||
59 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||||||||
60 | */ |
||||||||
61 | public function rightJoinWhere($table, $first, $operator, $second) |
||||||||
62 | { |
||||||||
63 | throw new LogicException('This database driver does not support the rightJoinWhere method.'); |
||||||||
64 | } |
||||||||
65 | |||||||||
66 | |||||||||
67 | /** |
||||||||
68 | * Add a join clause to the query. |
||||||||
69 | * |
||||||||
70 | * The boolean argument flag is part of this method's API in Laravel. |
||||||||
71 | * |
||||||||
72 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||||||||
73 | * |
||||||||
74 | * @param mixed $table |
||||||||
75 | * @param Closure|string $first |
||||||||
76 | * @param string|null $operator |
||||||||
77 | * @param float|int|string|null $second |
||||||||
78 | * @param string $type |
||||||||
79 | * @param bool $where |
||||||||
80 | */ |
||||||||
81 | 27 | public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): IlluminateQueryBuilder |
|||||||
82 | { |
||||||||
83 | 27 | $join = $this->newJoinClause($this, $type, $table); |
|||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
84 | |||||||||
85 | // If the first "column" of the join is really a Closure instance the developer |
||||||||
86 | // is trying to build a join with a complex "on" clause containing more than |
||||||||
87 | // one condition, so we'll add the join and call a Closure with the query. |
||||||||
88 | 27 | if ($first instanceof Closure) { |
|||||||
89 | 3 | $first($join); |
|||||||
90 | |||||||||
91 | 3 | $this->joins[] = $join; |
|||||||
0 ignored issues
–
show
|
|||||||||
92 | } |
||||||||
93 | 27 | if (!$first instanceof Closure) { |
|||||||
94 | // If the column is simply a string, we can assume the join simply has a basic |
||||||||
95 | // "on" clause with a single condition. So we will just build the join with |
||||||||
96 | // this simple join clauses attached to it. There is not a join callback. |
||||||||
97 | |||||||||
98 | //where and on are the same for aql |
||||||||
99 | 24 | $method = $where ? 'where' : 'on'; |
|||||||
100 | |||||||||
101 | 24 | $this->joins[] = $join->$method($first, $operator, $second); |
|||||||
102 | } |
||||||||
103 | |||||||||
104 | 27 | return $this; |
|||||||
0 ignored issues
–
show
|
|||||||||
105 | } |
||||||||
106 | |||||||||
107 | /** |
||||||||
108 | * Add a subquery join clause to the query. |
||||||||
109 | * |
||||||||
110 | * @param Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|string $query |
||||||||
111 | * @param string $as |
||||||||
112 | * @param Closure|string $first |
||||||||
113 | * @param string|null $operator |
||||||||
114 | * @param float|int|string|null $second |
||||||||
115 | * @param string $type |
||||||||
116 | * @param bool $where |
||||||||
117 | * |
||||||||
118 | * @throws \InvalidArgumentException |
||||||||
119 | * |
||||||||
120 | * @SuppressWarnings("PHPMD.BooleanArgumentFlag") |
||||||||
121 | */ |
||||||||
122 | 3 | public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false): IlluminateQueryBuilder |
|||||||
123 | { |
||||||||
124 | assert($query instanceof Builder); |
||||||||
125 | |||||||||
126 | 3 | $query->importTableAliases($this); |
|||||||
0 ignored issues
–
show
$this of type LaravelFreelancerNL\Aran...ry\Concerns\BuildsJoins is incompatible with the type Illuminate\Database\Query\Builder|array expected by parameter $aliases of LaravelFreelancerNL\Aran...r::importTableAliases() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
127 | 3 | $query->importTableAliases([$as => $as]); |
|||||||
128 | 3 | $this->importTableAliases($query); |
|||||||
0 ignored issues
–
show
It seems like
importTableAliases() 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
![]() |
|||||||||
129 | |||||||||
130 | 3 | [$query] = $this->createSub($query); |
|||||||
0 ignored issues
–
show
It seems like
createSub() 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
![]() |
|||||||||
131 | |||||||||
132 | 3 | return $this->join(new Expression($query . ' as ' . $as), $first, $operator, $second, $type, $where); |
|||||||
0 ignored issues
–
show
$query . ' as ' . $as of type string is incompatible with the type Illuminate\Database\Query\TValue expected by parameter $value of Illuminate\Database\Quer...pression::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
133 | } |
||||||||
134 | |||||||||
135 | |||||||||
136 | /** |
||||||||
137 | * Add a left join to the query. |
||||||||
138 | * |
||||||||
139 | * @param \Illuminate\Contracts\Database\Query\Expression|string $table |
||||||||
140 | * @param Closure|string $first |
||||||||
141 | * @param string|null $operator |
||||||||
142 | * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second |
||||||||
143 | */ |
||||||||
144 | 1 | public function leftJoin($table, $first, $operator = null, $second = null): IlluminateQueryBuilder |
|||||||
145 | { |
||||||||
146 | |||||||||
147 | 1 | return $this->join( |
|||||||
148 | 1 | $table, |
|||||||
149 | 1 | $first, |
|||||||
150 | 1 | $operator, |
|||||||
151 | /** @phpstan-ignore-next-line */ |
||||||||
152 | 1 | $this->grammar->getValue($second), |
|||||||
153 | 1 | 'left', |
|||||||
154 | 1 | ); |
|||||||
155 | } |
||||||||
156 | |||||||||
157 | /** |
||||||||
158 | * Add a subquery left join to the query. |
||||||||
159 | * |
||||||||
160 | * @param Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|string $query |
||||||||
161 | * @param string $as |
||||||||
162 | * @param Closure|string $first |
||||||||
163 | * @param string|null $operator |
||||||||
164 | * @param \Illuminate\Contracts\Database\Query\Expression|string|null $second |
||||||||
165 | */ |
||||||||
166 | 2 | public function leftJoinSub($query, $as, $first, $operator = null, $second = null): IlluminateQueryBuilder |
|||||||
167 | { |
||||||||
168 | /** @phpstan-ignore-next-line */ |
||||||||
169 | 2 | return $this->joinSub($query, $as, $first, $operator, $this->grammar->getValue($second), 'left'); |
|||||||
170 | } |
||||||||
171 | |||||||||
172 | |||||||||
173 | /** |
||||||||
174 | * Get a new join clause. |
||||||||
175 | * |
||||||||
176 | * @param string $type |
||||||||
177 | * @param string $table |
||||||||
178 | */ |
||||||||
179 | 28 | protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $table) |
|||||||
180 | { |
||||||||
181 | // @phpstan-ignore-next-line |
||||||||
182 | 28 | return new JoinClause($parentQuery, $type, $table); |
|||||||
183 | } |
||||||||
184 | |||||||||
185 | /** |
||||||||
186 | * Add a lateral join clause to the query. |
||||||||
187 | * |
||||||||
188 | * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<*>|string $query |
||||||||
189 | * @param string $as |
||||||||
190 | * @param string $type |
||||||||
191 | * @return $this |
||||||||
192 | */ |
||||||||
193 | 2 | public function joinLateral($query, string $as, string $type = 'inner') |
|||||||
194 | { |
||||||||
195 | assert($query instanceof Builder); |
||||||||
196 | |||||||||
197 | 2 | $query->importTableAliases($this); |
|||||||
0 ignored issues
–
show
$this of type LaravelFreelancerNL\Aran...ry\Concerns\BuildsJoins is incompatible with the type Illuminate\Database\Query\Builder|array expected by parameter $aliases of LaravelFreelancerNL\Aran...r::importTableAliases() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
198 | 2 | $query->importTableAliases([$as => $as]); |
|||||||
199 | 2 | $this->importTableAliases($query); |
|||||||
200 | |||||||||
201 | 2 | [$query] = $this->createSub($query); |
|||||||
202 | |||||||||
203 | 2 | $expression = $query . ' as ' . $this->grammar->wrapTable($as); |
|||||||
204 | |||||||||
205 | 2 | $this->joins[] = $this->newJoinLateralClause($this, $type, new Expression($expression)); |
|||||||
0 ignored issues
–
show
It seems like
newJoinLateralClause() 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
![]() $expression of type string is incompatible with the type Illuminate\Database\Query\TValue expected by parameter $value of Illuminate\Database\Quer...pression::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
206 | |||||||||
207 | 2 | return $this; |
|||||||
208 | } |
||||||||
209 | } |
||||||||
210 |