| Total Complexity | 41 |
| Total Lines | 249 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Customer 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 Customer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Customer extends ActiveRecord |
||
| 15 | { |
||
| 16 | public const STATUS_ACTIVE = 1; |
||
| 17 | public const STATUS_INACTIVE = 2; |
||
| 18 | |||
| 19 | protected int $id; |
||
| 20 | protected string $email; |
||
| 21 | protected string|null $name = null; |
||
| 22 | protected string|null $address = null; |
||
| 23 | protected int|null $status = 0; |
||
| 24 | protected bool|string|null $bool_status = false; |
||
| 25 | protected int|null $profile_id = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int|string |
||
| 29 | */ |
||
| 30 | public $status2; |
||
| 31 | /** |
||
| 32 | * @var int|string|null |
||
| 33 | */ |
||
| 34 | public $sumTotal; |
||
| 35 | |||
| 36 | public function getTableName(): string |
||
| 37 | { |
||
| 38 | return 'customer'; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function relationQuery(string $name): ActiveQueryInterface |
||
| 42 | { |
||
| 43 | return match ($name) { |
||
| 44 | 'profile' => $this->getProfileQuery(), |
||
| 45 | 'orders' => $this->getOrdersQuery(), |
||
| 46 | 'ordersPlain' => $this->getOrdersPlainQuery(), |
||
| 47 | 'ordersNoOrder' => $this->getOrdersNoOrderQuery(), |
||
| 48 | 'expensiveOrders' => $this->getExpensiveOrdersQuery(), |
||
| 49 | 'ordersWithItems' => $this->getOrdersWithItemsQuery(), |
||
| 50 | 'expensiveOrdersWithNullFK' => $this->getExpensiveOrdersWithNullFKQuery(), |
||
| 51 | 'ordersWithNullFK' => $this->getOrdersWithNullFKQuery(), |
||
| 52 | 'orders2' => $this->getOrders2Query(), |
||
| 53 | 'orderItems' => $this->getOrderItemsQuery(), |
||
| 54 | 'orderItems2' => $this->getOrderItems2Query(), |
||
| 55 | 'items2' => $this->getItems2Query(), |
||
| 56 | default => parent::relationQuery($name), |
||
| 57 | }; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getId(): int |
||
| 61 | { |
||
| 62 | return $this->id; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getEmail(): string |
||
| 66 | { |
||
| 67 | return $this->email; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getName(): string|null |
||
| 71 | { |
||
| 72 | return $this->name; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getAddress(): string|null |
||
| 76 | { |
||
| 77 | return $this->address; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getStatus(): int|null |
||
| 81 | { |
||
| 82 | return $this->status; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getBoolStatus(): bool|null |
||
| 86 | { |
||
| 87 | return $this->bool_status; |
||
|
|
|||
| 88 | } |
||
| 89 | |||
| 90 | public function getProfileId(): int|null |
||
| 91 | { |
||
| 92 | return $this->profile_id; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function setId(int $id): void |
||
| 96 | { |
||
| 97 | $this->id = $id; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function setEmail(string $email): void |
||
| 101 | { |
||
| 102 | $this->email = $email; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function setName(string|null $name): void |
||
| 106 | { |
||
| 107 | $this->name = $name; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function setAddress(string|null $address): void |
||
| 111 | { |
||
| 112 | $this->address = $address; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function setStatus(int|null $status): void |
||
| 116 | { |
||
| 117 | $this->status = $status; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function setBoolStatus(bool|null $bool_status): void |
||
| 121 | { |
||
| 122 | $this->bool_status = $bool_status; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function setProfileId(int|null $profile_id): void |
||
| 126 | { |
||
| 127 | $this->setAttribute('profile_id', $profile_id); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getProfile(): Profile|null |
||
| 131 | { |
||
| 132 | return $this->relation('profile'); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getProfileQuery(): ActiveQuery |
||
| 136 | { |
||
| 137 | return $this->hasOne(Profile::class, ['id' => 'profile_id']); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getOrdersPlain(): array |
||
| 141 | { |
||
| 142 | return $this->relation('ordersPlain'); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getOrdersPlainQuery(): ActiveQuery |
||
| 146 | { |
||
| 147 | return $this->hasMany(Order::class, ['customer_id' => 'id']); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getOrders(): array |
||
| 151 | { |
||
| 152 | return $this->relation('orders'); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getOrdersQuery(): ActiveQuery |
||
| 156 | { |
||
| 157 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('[[id]]'); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getOrdersNoOrder(): array |
||
| 161 | { |
||
| 162 | return $this->relation('ordersNoOrder'); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function getOrdersNoOrderQuery(): ActiveQuery |
||
| 166 | { |
||
| 167 | return $this->hasMany(Order::class, ['customer_id' => 'id']); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getExpensiveOrders(): array |
||
| 171 | { |
||
| 172 | return $this->relation('expensiveOrders'); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function getExpensiveOrdersQuery(): ActiveQuery |
||
| 176 | { |
||
| 177 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id'); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getItem(): void |
||
| 181 | { |
||
| 182 | } |
||
| 183 | |||
| 184 | public function getOrdersWithItems(): array |
||
| 185 | { |
||
| 186 | return $this->relation('ordersWithItems'); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function getOrdersWithItemsQuery(): ActiveQuery |
||
| 190 | { |
||
| 191 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->with('orderItems'); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getExpensiveOrdersWithNullFK(): array |
||
| 195 | { |
||
| 196 | return $this->relation('expensiveOrdersWithNullFK'); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getExpensiveOrdersWithNullFKQuery(): ActiveQuery |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getOrdersWithNullFK(): array |
||
| 208 | { |
||
| 209 | return $this->relation('ordersWithNullFK'); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function getOrdersWithNullFKQuery(): ActiveQuery |
||
| 213 | { |
||
| 214 | return $this->hasMany(OrderWithNullFK::class, ['customer_id' => 'id'])->orderBy('id'); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function getOrders2(): array |
||
| 218 | { |
||
| 219 | return $this->relation('orders2'); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function getOrders2Query(): ActiveQuery |
||
| 223 | { |
||
| 224 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id'); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function getOrderItems(): array |
||
| 228 | { |
||
| 229 | return $this->relation('orderItems'); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** deeply nested table relation */ |
||
| 233 | public function getOrderItemsQuery(): ActiveQuery |
||
| 234 | { |
||
| 235 | $rel = $this->hasMany(Item::class, ['id' => 'item_id']); |
||
| 236 | |||
| 237 | return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) { |
||
| 238 | /* @var $q ActiveQuery */ |
||
| 239 | $q->viaTable('order', ['customer_id' => 'id']); |
||
| 240 | })->orderBy('id'); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getOrderItems2(): array |
||
| 246 | } |
||
| 247 | |||
| 248 | public function getOrderItems2Query(): ActiveQuery |
||
| 249 | { |
||
| 250 | return $this->hasMany(OrderItem::class, ['order_id' => 'id']) |
||
| 251 | ->via('ordersNoOrder'); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function getItems2(): array |
||
| 255 | { |
||
| 256 | return $this->relation('items2'); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function getItems2Query(): ActiveQuery |
||
| 260 | { |
||
| 261 | return $this->hasMany(Item::class, ['id' => 'item_id']) |
||
| 262 | ->via('orderItems2'); |
||
| 263 | } |
||
| 264 | } |
||
| 265 |