Total Complexity | 67 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Order often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Order extends ActiveRecord |
||
20 | { |
||
21 | public const TABLE_NAME = 'order'; |
||
22 | |||
23 | protected int|null $id; |
||
24 | protected int $customer_id; |
||
25 | protected int $created_at; |
||
26 | protected float $total; |
||
27 | |||
28 | protected string|int|null $virtualCustomerId = null; |
||
29 | |||
30 | public function getTableName(): string |
||
31 | { |
||
32 | return self::TABLE_NAME; |
||
33 | } |
||
34 | |||
35 | public function getId(): int|null |
||
36 | { |
||
37 | return $this->id; |
||
38 | } |
||
39 | |||
40 | public function getCustomerId(): int |
||
41 | { |
||
42 | return $this->customer_id; |
||
43 | } |
||
44 | |||
45 | public function getCreatedAt(): int |
||
46 | { |
||
47 | return $this->created_at; |
||
48 | } |
||
49 | |||
50 | public function getTotal(): float |
||
51 | { |
||
52 | return $this->total; |
||
53 | } |
||
54 | |||
55 | public function setId(int|null $id): void |
||
56 | { |
||
57 | $this->setAttribute('id', $id); |
||
58 | } |
||
59 | |||
60 | public function setCustomerId(int $customerId): void |
||
61 | { |
||
62 | $this->setAttribute('customer_id', $customerId); |
||
63 | } |
||
64 | |||
65 | public function setCreatedAt(int $createdAt): void |
||
66 | { |
||
67 | $this->created_at = $createdAt; |
||
68 | } |
||
69 | |||
70 | public function setTotal(float $total): void |
||
71 | { |
||
72 | $this->total = $total; |
||
73 | } |
||
74 | |||
75 | public function relationQuery(string $name): ActiveQueryInterface |
||
76 | { |
||
77 | return match ($name) { |
||
78 | 'virtualCustomer' => $this->getVirtualCustomerQuery(), |
||
79 | 'customer' => $this->getCustomerQuery(), |
||
80 | 'customerJoinedWithProfile' => $this->getCustomerJoinedWithProfileQuery(), |
||
81 | 'customerJoinedWithProfileIndexOrdered' => $this->getCustomerJoinedWithProfileIndexOrderedQuery(), |
||
82 | 'customer2' => $this->getCustomer2Query(), |
||
83 | 'orderItems' => $this->getOrderItemsQuery(), |
||
84 | 'orderItems2' => $this->getOrderItems2Query(), |
||
85 | 'orderItems3' => $this->getOrderItems3Query(), |
||
86 | 'orderItemsWithNullFK' => $this->getOrderItemsWithNullFKQuery(), |
||
87 | 'items' => $this->getItemsQuery(), |
||
88 | 'itemsIndexed' => $this->getItemsIndexedQuery(), |
||
89 | 'itemsWithNullFK' => $this->getItemsWithNullFKQuery(), |
||
90 | 'itemsInOrder1' => $this->getItemsInOrder1Query(), |
||
91 | 'itemsInOrder2' => $this->getItemsInOrder2Query(), |
||
92 | 'books' => $this->getBooksQuery(), |
||
93 | 'booksWithNullFK' => $this->getBooksWithNullFKQuery(), |
||
94 | 'booksViaTable' => $this->getBooksViaTableQuery(), |
||
95 | 'booksWithNullFKViaTable' => $this->getBooksWithNullFKViaTableQuery(), |
||
96 | 'books2' => $this->getBooks2Query(), |
||
97 | 'booksExplicit' => $this->getBooksExplicitQuery(), |
||
98 | 'booksExplicitA' => $this->getBooksExplicitAQuery(), |
||
99 | 'bookItems' => $this->getBookItemsQuery(), |
||
100 | 'movieItems' => $this->getMovieItemsQuery(), |
||
101 | 'limitedItems' => $this->getLimitedItemsQuery(), |
||
102 | 'expensiveItemsUsingViaWithCallable' => $this->getExpensiveItemsUsingViaWithCallableQuery(), |
||
103 | 'cheapItemsUsingViaWithCallable' => $this->getCheapItemsUsingViaWithCallableQuery(), |
||
104 | 'orderItemsFor8' => $this->getOrderItemsFor8Query(), |
||
105 | 'itemsFor8' => $this->getItemsFor8Query(), |
||
106 | default => parent::relationQuery($name), |
||
107 | }; |
||
108 | } |
||
109 | |||
110 | public function setVirtualCustomerId(string|int|null $virtualCustomerId = null): void |
||
111 | { |
||
112 | $this->virtualCustomerId = $virtualCustomerId; |
||
113 | } |
||
114 | |||
115 | public function getVirtualCustomer(): Customer|null |
||
116 | { |
||
117 | return $this->relation('virtualCustomer'); |
||
|
|||
118 | } |
||
119 | |||
120 | public function getVirtualCustomerQuery(): ActiveQuery |
||
121 | { |
||
122 | return $this->hasOne(Customer::class, ['id' => 'virtualCustomerId']); |
||
123 | } |
||
124 | |||
125 | public function getCustomer(): Customer|null |
||
126 | { |
||
127 | return $this->relation('customer'); |
||
128 | } |
||
129 | |||
130 | public function getCustomerQuery(): ActiveQuery |
||
131 | { |
||
132 | return $this->hasOne(Customer::class, ['id' => 'customer_id']); |
||
133 | } |
||
134 | |||
135 | public function getCustomerJoinedWithProfile(): Customer|null |
||
136 | { |
||
137 | return $this->relation('customerJoinedWithProfile'); |
||
138 | } |
||
139 | |||
140 | public function getCustomerJoinedWithProfileQuery(): ActiveQuery |
||
141 | { |
||
142 | return $this->hasOne(Customer::class, ['id' => 'customer_id'])->joinWith('profile'); |
||
143 | } |
||
144 | |||
145 | public function getCustomerJoinedWithProfileIndexOrdered(): array |
||
146 | { |
||
147 | return $this->relation('customerJoinedWithProfileIndexOrdered'); |
||
148 | } |
||
149 | |||
150 | public function getCustomerJoinedWithProfileIndexOrderedQuery(): ActiveQuery |
||
151 | { |
||
152 | return $this->hasMany( |
||
153 | Customer::class, |
||
154 | ['id' => 'customer_id'] |
||
155 | )->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name'); |
||
156 | } |
||
157 | |||
158 | public function getCustomer2(): Customer|null |
||
159 | { |
||
160 | return $this->relation('customer2'); |
||
161 | } |
||
162 | |||
163 | public function getCustomer2Query(): ActiveQuery |
||
164 | { |
||
165 | return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders2'); |
||
166 | } |
||
167 | |||
168 | public function getOrderItems(): array |
||
169 | { |
||
170 | return $this->relation('orderItems'); |
||
171 | } |
||
172 | |||
173 | public function getOrderItemsQuery(): ActiveQuery |
||
174 | { |
||
175 | return $this->hasMany(OrderItem::class, ['order_id' => 'id']); |
||
176 | } |
||
177 | |||
178 | public function getOrderItems2(): array |
||
179 | { |
||
180 | return $this->relation('orderItems2'); |
||
181 | } |
||
182 | |||
183 | public function getOrderItems2Query(): ActiveQuery |
||
184 | { |
||
185 | return $this->hasMany(OrderItem::class, ['order_id' => 'id'])->indexBy('item_id'); |
||
186 | } |
||
187 | |||
188 | public function getOrderItems3(): array |
||
189 | { |
||
190 | return $this->relation('orderItems3'); |
||
191 | } |
||
192 | |||
193 | public function getOrderItems3Query(): ActiveQuery |
||
194 | { |
||
195 | return $this->hasMany( |
||
196 | OrderItem::class, |
||
197 | ['order_id' => 'id'] |
||
198 | )->indexBy(fn ($row) => $row['order_id'] . '_' . $row['item_id']); |
||
199 | } |
||
200 | |||
201 | public function getOrderItemsWithNullFK(): array |
||
202 | { |
||
203 | return $this->relation('orderItemsWithNullFK'); |
||
204 | } |
||
205 | |||
206 | public function getOrderItemsWithNullFKQuery(): ActiveQuery |
||
207 | { |
||
208 | return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id']); |
||
209 | } |
||
210 | |||
211 | public function getItems(): array |
||
212 | { |
||
213 | return $this->relation('items'); |
||
214 | } |
||
215 | |||
216 | public function getItemsQuery(): ActiveQuery |
||
217 | { |
||
218 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) { |
||
219 | // additional query configuration |
||
220 | })->orderBy('item.id'); |
||
221 | } |
||
222 | |||
223 | public function getItemsIndexed(): array |
||
224 | { |
||
225 | return $this->relation('itemsIndexed'); |
||
226 | } |
||
227 | |||
228 | public function getItemsIndexedQuery(): ActiveQuery |
||
229 | { |
||
230 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->indexBy('id'); |
||
231 | } |
||
232 | |||
233 | public function getItemsWithNullFK(): array |
||
234 | { |
||
235 | return $this->relation('itemsWithNullFK'); |
||
236 | } |
||
237 | |||
238 | public function getItemsWithNullFKQuery(): ActiveQuery |
||
239 | { |
||
240 | return $this->hasMany( |
||
241 | Item::class, |
||
242 | ['id' => 'item_id'] |
||
243 | )->viaTable('order_item_with_null_fk', ['order_id' => 'id']); |
||
244 | } |
||
245 | |||
246 | public function getItemsInOrder1(): array |
||
247 | { |
||
248 | return $this->relation('itemsInOrder1'); |
||
249 | } |
||
250 | |||
251 | public function getItemsInOrder1Query(): ActiveQuery |
||
252 | { |
||
253 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) { |
||
254 | $q->orderBy(['subtotal' => SORT_ASC]); |
||
255 | })->orderBy('name'); |
||
256 | } |
||
257 | |||
258 | public function getItemsInOrder2(): array |
||
259 | { |
||
260 | return $this->relation('itemsInOrder2'); |
||
261 | } |
||
262 | |||
263 | public function getItemsInOrder2Query(): ActiveQuery |
||
264 | { |
||
265 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', static function ($q) { |
||
266 | $q->orderBy(['subtotal' => SORT_DESC]); |
||
267 | })->orderBy('name'); |
||
268 | } |
||
269 | |||
270 | public function getBooks(): array |
||
271 | { |
||
272 | return $this->relation('books'); |
||
273 | } |
||
274 | |||
275 | public function getBooksQuery(): ActiveQuery |
||
276 | { |
||
277 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems')->where(['category_id' => 1]); |
||
278 | } |
||
279 | |||
280 | public function getBooksWithNullFK(): array |
||
281 | { |
||
282 | return $this->relation('booksWithNullFK'); |
||
283 | } |
||
284 | |||
285 | public function getBooksWithNullFKQuery(): ActiveQuery |
||
286 | { |
||
287 | return $this->hasMany( |
||
288 | Item::class, |
||
289 | ['id' => 'item_id'] |
||
290 | )->via('orderItemsWithNullFK')->where(['category_id' => 1]); |
||
291 | } |
||
292 | |||
293 | public function getBooksViaTable(): array |
||
294 | { |
||
295 | return $this->relation('booksViaTable'); |
||
296 | } |
||
297 | |||
298 | public function getBooksViaTableQuery(): ActiveQuery |
||
299 | { |
||
300 | return $this->hasMany( |
||
301 | Item::class, |
||
302 | ['id' => 'item_id'] |
||
303 | )->viaTable('order_item', ['order_id' => 'id'])->where(['category_id' => 1]); |
||
304 | } |
||
305 | |||
306 | public function getBooksWithNullFKViaTable(): array |
||
307 | { |
||
308 | return $this->relation('booksWithNullFKViaTable'); |
||
309 | } |
||
310 | |||
311 | public function getBooksWithNullFKViaTableQuery(): ActiveQuery |
||
312 | { |
||
313 | return $this->hasMany( |
||
314 | Item::class, |
||
315 | ['id' => 'item_id'] |
||
316 | )->viaTable('order_item_with_null_fk', ['order_id' => 'id'])->where(['category_id' => 1]); |
||
317 | } |
||
318 | |||
319 | public function getBooks2(): array |
||
320 | { |
||
321 | return $this->relation('books2'); |
||
322 | } |
||
323 | |||
324 | public function getBooks2Query(): ActiveQuery |
||
325 | { |
||
326 | return $this->hasMany( |
||
327 | Item::class, |
||
328 | ['id' => 'item_id'] |
||
329 | )->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
||
330 | } |
||
331 | |||
332 | public function getBooksExplicit(): array |
||
333 | { |
||
334 | return $this->relation('booksExplicit'); |
||
335 | } |
||
336 | |||
337 | public function getBooksExplicitQuery(): ActiveQuery |
||
338 | { |
||
339 | return $this->hasMany( |
||
340 | Item::class, |
||
341 | ['id' => 'item_id'] |
||
342 | )->onCondition(['category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
||
343 | } |
||
344 | |||
345 | public function getBooksExplicitA(): array |
||
346 | { |
||
347 | return $this->relation('booksExplicitA'); |
||
348 | } |
||
349 | |||
350 | public function getBooksExplicitAQuery(): ActiveQuery |
||
351 | { |
||
352 | return $this->hasMany( |
||
353 | Item::class, |
||
354 | ['id' => 'item_id'] |
||
355 | )->alias('bo')->onCondition(['bo.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
||
356 | } |
||
357 | |||
358 | public function getBookItems(): array |
||
359 | { |
||
360 | return $this->relation('bookItems'); |
||
361 | } |
||
362 | |||
363 | public function getBookItemsQuery(): ActiveQuery |
||
364 | { |
||
365 | return $this->hasMany( |
||
366 | Item::class, |
||
367 | ['id' => 'item_id'] |
||
368 | )->alias('books')->onCondition(['books.category_id' => 1])->viaTable('order_item', ['order_id' => 'id']); |
||
369 | } |
||
370 | |||
371 | public function getMovieItems(): array |
||
372 | { |
||
373 | return $this->relation('movieItems'); |
||
374 | } |
||
375 | |||
376 | public function getMovieItemsQuery(): ActiveQuery |
||
382 | } |
||
383 | |||
384 | public function getLimitedItems(): array |
||
385 | { |
||
386 | return $this->relation('limitedItems'); |
||
387 | } |
||
388 | |||
389 | public function getLimitedItemsQuery(): ActiveQuery |
||
390 | { |
||
391 | return $this->hasMany(Item::class, ['id' => 'item_id'])->onCondition(['item.id' => [3, 5]])->via('orderItems'); |
||
392 | } |
||
393 | |||
394 | public function getExpensiveItemsUsingViaWithCallable(): array |
||
395 | { |
||
396 | return $this->relation('expensiveItemsUsingViaWithCallable'); |
||
397 | } |
||
398 | |||
399 | public function getExpensiveItemsUsingViaWithCallableQuery(): ActiveQuery |
||
400 | { |
||
401 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems', function (ActiveQuery $q) { |
||
402 | $q->where(['>=', 'subtotal', 10]); |
||
403 | }); |
||
404 | } |
||
405 | |||
406 | public function getCheapItemsUsingViaWithCallable(): array |
||
407 | { |
||
408 | return $this->relation('cheapItemsUsingViaWithCallable'); |
||
409 | } |
||
410 | |||
411 | public function getCheapItemsUsingViaWithCallableQuery(): ActiveQuery |
||
415 | }); |
||
416 | } |
||
417 | |||
418 | public function getOrderItemsFor8(): array |
||
419 | { |
||
420 | return $this->relation('orderItemsFor8'); |
||
421 | } |
||
422 | |||
423 | public function getOrderItemsFor8Query(): ActiveQuery |
||
424 | { |
||
425 | return $this->hasMany(OrderItemWithNullFK::class, ['order_id' => 'id'])->andOnCondition(['subtotal' => 8.0]); |
||
426 | } |
||
427 | |||
428 | public function getItemsFor8(): array |
||
429 | { |
||
430 | return $this->relation('itemsFor8'); |
||
431 | } |
||
432 | |||
433 | public function getItemsFor8Query(): ActiveQuery |
||
434 | { |
||
435 | return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItemsFor8'); |
||
436 | } |
||
437 | } |
||
438 |