Conditions | 35 |
Paths | > 20000 |
Total Lines | 233 |
Code Lines | 166 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function testJoinWithAlias(string $aliasMethod): void |
||
35 | { |
||
36 | $orders = []; |
||
37 | $this->checkFixture($this->db(), 'order', true); |
||
38 | |||
39 | /** left join and eager loading */ |
||
40 | $orderQuery = new ActiveQuery(Order::class); |
||
41 | $query = $orderQuery->joinWith(['customer c']); |
||
42 | |||
43 | if ($aliasMethod === 'explicit') { |
||
44 | $orders = $query->orderBy('c.id DESC, order.id')->all(); |
||
45 | } elseif ($aliasMethod === 'querysyntax') { |
||
46 | $orders = $query->orderBy('{{@customer}}.id DESC, {{@order}}.id')->all(); |
||
47 | } elseif ($aliasMethod === 'applyAlias') { |
||
48 | $orders = $query->orderBy( |
||
49 | $query->applyAlias('customer', 'id') . ' DESC,' . $query->applyAlias('order', 'id') |
||
|
|||
50 | )->all(); |
||
51 | } |
||
52 | |||
53 | $this->assertCount(3, $orders); |
||
54 | $this->assertEquals(2, $orders[0]->getId()); |
||
55 | $this->assertEquals(3, $orders[1]->getId()); |
||
56 | $this->assertEquals(1, $orders[2]->getId()); |
||
57 | $this->assertTrue($orders[0]->isRelationPopulated('customer')); |
||
58 | $this->assertTrue($orders[1]->isRelationPopulated('customer')); |
||
59 | $this->assertTrue($orders[2]->isRelationPopulated('customer')); |
||
60 | |||
61 | /** inner join filtering and eager loading */ |
||
62 | $orderQuery = new ActiveQuery(Order::class); |
||
63 | $query = $orderQuery->innerJoinWith(['customer c']); |
||
64 | |||
65 | if ($aliasMethod === 'explicit') { |
||
66 | $orders = $query->where('{{c}}.[[id]]=2')->orderBy('order.id')->all(); |
||
67 | } elseif ($aliasMethod === 'querysyntax') { |
||
68 | $orders = $query->where('{{@customer}}.[[id]]=2')->orderBy('{{@order}}.id')->all(); |
||
69 | } elseif ($aliasMethod === 'applyAlias') { |
||
70 | $orders = $query->where( |
||
71 | [$query->applyAlias('customer', 'id') => 2] |
||
72 | )->orderBy($query->applyAlias('order', 'id'))->all(); |
||
73 | } |
||
74 | |||
75 | $this->assertCount(2, $orders); |
||
76 | $this->assertEquals(2, $orders[0]->getId()); |
||
77 | $this->assertEquals(3, $orders[1]->getId()); |
||
78 | $this->assertTrue($orders[0]->isRelationPopulated('customer')); |
||
79 | $this->assertTrue($orders[1]->isRelationPopulated('customer')); |
||
80 | |||
81 | /** inner join filtering without eager loading */ |
||
82 | $orderQuery = new ActiveQuery(Order::class); |
||
83 | $query = $orderQuery->innerJoinWith(['customer c'], false); |
||
84 | |||
85 | if ($aliasMethod === 'explicit') { |
||
86 | $orders = $query->where('{{c}}.[[id]]=2')->orderBy('order.id')->all(); |
||
87 | } elseif ($aliasMethod === 'querysyntax') { |
||
88 | $orders = $query->where('{{@customer}}.[[id]]=2')->orderBy('{{@order}}.id')->all(); |
||
89 | } elseif ($aliasMethod === 'applyAlias') { |
||
90 | $orders = $query->where( |
||
91 | [$query->applyAlias('customer', 'id') => 2] |
||
92 | )->orderBy($query->applyAlias('order', 'id'))->all(); |
||
93 | } |
||
94 | |||
95 | $this->assertCount(2, $orders); |
||
96 | $this->assertEquals(2, $orders[0]->getId()); |
||
97 | $this->assertEquals(3, $orders[1]->getId()); |
||
98 | $this->assertFalse($orders[0]->isRelationPopulated('customer')); |
||
99 | $this->assertFalse($orders[1]->isRelationPopulated('customer')); |
||
100 | |||
101 | /** join with via-relation */ |
||
102 | $orderQuery = new ActiveQuery(Order::class); |
||
103 | $query = $orderQuery->innerJoinWith(['books b']); |
||
104 | |||
105 | if ($aliasMethod === 'explicit') { |
||
106 | $orders = $query->where( |
||
107 | ['b.name' => 'Yii 1.1 Application Development Cookbook'] |
||
108 | )->orderBy('order.id')->all(); |
||
109 | } elseif ($aliasMethod === 'querysyntax') { |
||
110 | $orders = $query->where( |
||
111 | ['{{@item}}.name' => 'Yii 1.1 Application Development Cookbook'] |
||
112 | )->orderBy('{{@order}}.id')->all(); |
||
113 | } elseif ($aliasMethod === 'applyAlias') { |
||
114 | $orders = $query->where( |
||
115 | [$query->applyAlias('book', 'name') => 'Yii 1.1 Application Development Cookbook'] |
||
116 | )->orderBy($query->applyAlias('order', 'id'))->all(); |
||
117 | } |
||
118 | |||
119 | $this->assertCount(2, $orders); |
||
120 | $this->assertCount(2, $orders[0]->getBooks()); |
||
121 | $this->assertCount(1, $orders[1]->getBooks()); |
||
122 | $this->assertEquals(1, $orders[0]->getId()); |
||
123 | $this->assertEquals(3, $orders[1]->getId()); |
||
124 | $this->assertTrue($orders[0]->isRelationPopulated('books')); |
||
125 | $this->assertTrue($orders[1]->isRelationPopulated('books')); |
||
126 | |||
127 | /** joining sub relations */ |
||
128 | $orderQuery = new ActiveQuery(Order::class); |
||
129 | $query = $orderQuery->innerJoinWith( |
||
130 | [ |
||
131 | 'items i' => static function ($q) use ($aliasMethod) { |
||
132 | /** @var $q ActiveQuery */ |
||
133 | if ($aliasMethod === 'explicit') { |
||
134 | $q->orderBy('{{i}}.id'); |
||
135 | } elseif ($aliasMethod === 'querysyntax') { |
||
136 | $q->orderBy('{{@item}}.id'); |
||
137 | } elseif ($aliasMethod === 'applyAlias') { |
||
138 | $q->orderBy($q->applyAlias('item', 'id')); |
||
139 | } |
||
140 | }, |
||
141 | 'items.category c' => static function ($q) use ($aliasMethod) { |
||
142 | /** @var $q ActiveQuery */ |
||
143 | if ($aliasMethod === 'explicit') { |
||
144 | $q->where('{{c}}.[[id]] = 2'); |
||
145 | } elseif ($aliasMethod === 'querysyntax') { |
||
146 | $q->where('{{@category}}.[[id]] = 2'); |
||
147 | } elseif ($aliasMethod === 'applyAlias') { |
||
148 | $q->where([$q->applyAlias('category', 'id') => 2]); |
||
149 | } |
||
150 | }, |
||
151 | ] |
||
152 | ); |
||
153 | |||
154 | if ($aliasMethod === 'explicit') { |
||
155 | $orders = $query->orderBy('{{i}}.id')->all(); |
||
156 | } elseif ($aliasMethod === 'querysyntax') { |
||
157 | $orders = $query->orderBy('{{@item}}.id')->all(); |
||
158 | } elseif ($aliasMethod === 'applyAlias') { |
||
159 | $orders = $query->orderBy($query->applyAlias('item', 'id'))->all(); |
||
160 | } |
||
161 | |||
162 | $this->assertCount(1, $orders); |
||
163 | $this->assertCount(3, $orders[0]->getItems()); |
||
164 | $this->assertEquals(2, $orders[0]->getId()); |
||
165 | $this->assertEquals(2, $orders[0]->getItems()[0]->getCategory()->getId()); |
||
166 | $this->assertTrue($orders[0]->isRelationPopulated('items')); |
||
167 | $this->assertTrue($orders[0]->getItems()[0]->isRelationPopulated('category')); |
||
168 | |||
169 | /** join with ON condition */ |
||
170 | if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { |
||
171 | $relationName = 'books' . ucfirst($aliasMethod); |
||
172 | |||
173 | $orderQuery = new ActiveQuery(Order::class); |
||
174 | $orders = $orderQuery->joinWith(["$relationName b"])->orderBy('order.id')->all(); |
||
175 | |||
176 | $this->assertCount(3, $orders); |
||
177 | $this->assertCount(2, $orders[0]->relation($relationName)); |
||
178 | $this->assertCount(0, $orders[1]->relation($relationName)); |
||
179 | $this->assertCount(1, $orders[2]->relation($relationName)); |
||
180 | $this->assertEquals(1, $orders[0]->getId()); |
||
181 | $this->assertEquals(2, $orders[1]->getId()); |
||
182 | $this->assertEquals(3, $orders[2]->getId()); |
||
183 | $this->assertTrue($orders[0]->isRelationPopulated($relationName)); |
||
184 | $this->assertTrue($orders[1]->isRelationPopulated($relationName)); |
||
185 | $this->assertTrue($orders[2]->isRelationPopulated($relationName)); |
||
186 | } |
||
187 | |||
188 | /** join with ON condition and alias in relation definition */ |
||
189 | if ($aliasMethod === 'explicit' || $aliasMethod === 'querysyntax') { |
||
190 | $relationName = 'books' . ucfirst($aliasMethod) . 'A'; |
||
191 | |||
192 | $orderQuery = new ActiveQuery(Order::class); |
||
193 | $orders = $orderQuery->joinWith([(string)$relationName])->orderBy('order.id')->all(); |
||
194 | |||
195 | $this->assertCount(3, $orders); |
||
196 | $this->assertCount(2, $orders[0]->relation($relationName)); |
||
197 | $this->assertCount(0, $orders[1]->relation($relationName)); |
||
198 | $this->assertCount(1, $orders[2]->relation($relationName)); |
||
199 | $this->assertEquals(1, $orders[0]->getId()); |
||
200 | $this->assertEquals(2, $orders[1]->getId()); |
||
201 | $this->assertEquals(3, $orders[2]->getId()); |
||
202 | $this->assertTrue($orders[0]->isRelationPopulated($relationName)); |
||
203 | $this->assertTrue($orders[1]->isRelationPopulated($relationName)); |
||
204 | $this->assertTrue($orders[2]->isRelationPopulated($relationName)); |
||
205 | } |
||
206 | |||
207 | /** join with count and query */ |
||
208 | $orderQuery = new ActiveQuery(Order::class); |
||
209 | $query = $orderQuery->joinWith(['customer c']); |
||
210 | |||
211 | if ($aliasMethod === 'explicit') { |
||
212 | $count = $query->count('{{c}}.[[id]]'); |
||
213 | } elseif ($aliasMethod === 'querysyntax') { |
||
214 | $count = $query->count('{{@customer}}.id'); |
||
215 | } elseif ($aliasMethod === 'applyAlias') { |
||
216 | $count = $query->count($query->applyAlias('customer', 'id')); |
||
217 | } |
||
218 | |||
219 | $this->assertEquals(3, $count); |
||
220 | |||
221 | $orders = $query->all(); |
||
222 | $this->assertCount(3, $orders); |
||
223 | |||
224 | /** relational query */ |
||
225 | $orderQuery = new ActiveQuery(Order::class); |
||
226 | $order = $orderQuery->findOne(1); |
||
227 | |||
228 | $customerQuery = $order->getCustomerQuery()->innerJoinWith(['orders o'], false); |
||
229 | |||
230 | if ($aliasMethod === 'explicit') { |
||
231 | $customer = $customerQuery->where(['{{o}}.[[id]]' => 1])->onePopulate(); |
||
232 | } elseif ($aliasMethod === 'querysyntax') { |
||
233 | $customer = $customerQuery->where(['{{@order}}.id' => 1])->onePopulate(); |
||
234 | } elseif ($aliasMethod === 'applyAlias') { |
||
235 | $customer = $customerQuery->where([$query->applyAlias('order', 'id') => 1])->onePopulate(); |
||
236 | } |
||
237 | |||
238 | $this->assertEquals(1, $customer->getId()); |
||
239 | $this->assertNotNull($customer); |
||
240 | |||
241 | /** join with sub-relation called inside Closure */ |
||
242 | $orderQuery = new ActiveQuery(Order::class); |
||
243 | $orders = $orderQuery->joinWith( |
||
244 | [ |
||
245 | 'items' => static function ($q) use ($aliasMethod) { |
||
246 | /** @var $q ActiveQuery */ |
||
247 | $q->orderBy('item.id'); |
||
248 | $q->joinWith(['category c']); |
||
249 | |||
250 | if ($aliasMethod === 'explicit') { |
||
251 | $q->where('{{c}}.[[id]] = 2'); |
||
252 | } elseif ($aliasMethod === 'querysyntax') { |
||
253 | $q->where('{{@category}}.[[id]] = 2'); |
||
254 | } elseif ($aliasMethod === 'applyAlias') { |
||
255 | $q->where([$q->applyAlias('category', 'id') => 2]); |
||
256 | } |
||
257 | }, |
||
258 | ] |
||
259 | )->orderBy('order.id')->all(); |
||
260 | |||
261 | $this->assertCount(1, $orders); |
||
262 | $this->assertCount(3, $orders[0]->getItems()); |
||
263 | $this->assertEquals(2, $orders[0]->getId()); |
||
264 | $this->assertEquals(2, $orders[0]->getItems()[0]->getCategory()->getId()); |
||
265 | $this->assertTrue($orders[0]->isRelationPopulated('items')); |
||
266 | $this->assertTrue($orders[0]->getItems()[0]->isRelationPopulated('category')); |
||
267 | } |
||
408 |
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.