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