| Total Complexity | 42 |
| Total Lines | 255 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 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 | 'ordersUsingInstance' => $this->hasMany(new Order($this->db()), ['customer_id' => 'id']), |
||
| 57 | default => parent::relationQuery($name), |
||
| 58 | }; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function getId(): int |
||
| 62 | { |
||
| 63 | return $this->id; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function getEmail(): string |
||
| 67 | { |
||
| 68 | return $this->email; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function getName(): string|null |
||
| 72 | { |
||
| 73 | return $this->name; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getAddress(): string|null |
||
| 77 | { |
||
| 78 | return $this->address; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function getStatus(): int|null |
||
| 82 | { |
||
| 83 | return $this->status; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getBoolStatus(): bool|null |
||
| 87 | { |
||
| 88 | return $this->bool_status; |
||
|
|
|||
| 89 | } |
||
| 90 | |||
| 91 | public function getProfileId(): int|null |
||
| 92 | { |
||
| 93 | return $this->profile_id; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function setId(int $id): void |
||
| 97 | { |
||
| 98 | $this->id = $id; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function setEmail(string $email): void |
||
| 102 | { |
||
| 103 | $this->email = $email; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function setName(string|null $name): void |
||
| 107 | { |
||
| 108 | $this->name = $name; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function setAddress(string|null $address): void |
||
| 112 | { |
||
| 113 | $this->address = $address; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function setStatus(int|null $status): void |
||
| 117 | { |
||
| 118 | $this->status = $status; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setBoolStatus(bool|null $bool_status): void |
||
| 122 | { |
||
| 123 | $this->bool_status = $bool_status; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function setProfileId(int|null $profile_id): void |
||
| 127 | { |
||
| 128 | $this->setAttribute('profile_id', $profile_id); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function getProfile(): Profile|null |
||
| 132 | { |
||
| 133 | return $this->relation('profile'); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getProfileQuery(): ActiveQuery |
||
| 137 | { |
||
| 138 | return $this->hasOne(Profile::class, ['id' => 'profile_id']); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function getOrdersPlain(): array |
||
| 142 | { |
||
| 143 | return $this->relation('ordersPlain'); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function getOrdersPlainQuery(): ActiveQuery |
||
| 147 | { |
||
| 148 | return $this->hasMany(Order::class, ['customer_id' => 'id']); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getOrders(): array |
||
| 152 | { |
||
| 153 | return $this->relation('orders'); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getOrdersQuery(): ActiveQuery |
||
| 157 | { |
||
| 158 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('[[id]]'); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getOrdersNoOrder(): array |
||
| 162 | { |
||
| 163 | return $this->relation('ordersNoOrder'); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getOrdersNoOrderQuery(): ActiveQuery |
||
| 167 | { |
||
| 168 | return $this->hasMany(Order::class, ['customer_id' => 'id']); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function getExpensiveOrders(): array |
||
| 172 | { |
||
| 173 | return $this->relation('expensiveOrders'); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function getExpensiveOrdersQuery(): ActiveQuery |
||
| 177 | { |
||
| 178 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id'); |
||
| 179 | } |
||
| 180 | |||
| 181 | public function getItem(): void |
||
| 182 | { |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getOrdersWithItems(): array |
||
| 186 | { |
||
| 187 | return $this->relation('ordersWithItems'); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getOrdersWithItemsQuery(): ActiveQuery |
||
| 191 | { |
||
| 192 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->with('orderItems'); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function getExpensiveOrdersWithNullFK(): array |
||
| 196 | { |
||
| 197 | return $this->relation('expensiveOrdersWithNullFK'); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function getExpensiveOrdersWithNullFKQuery(): ActiveQuery |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getOrdersWithNullFK(): array |
||
| 209 | { |
||
| 210 | return $this->relation('ordersWithNullFK'); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getOrdersWithNullFKQuery(): ActiveQuery |
||
| 214 | { |
||
| 215 | return $this->hasMany(OrderWithNullFK::class, ['customer_id' => 'id'])->orderBy('id'); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getOrders2(): array |
||
| 219 | { |
||
| 220 | return $this->relation('orders2'); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getOrders2Query(): ActiveQuery |
||
| 224 | { |
||
| 225 | return $this->hasMany(Order::class, ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id'); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getOrderItems(): array |
||
| 229 | { |
||
| 230 | return $this->relation('orderItems'); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** deeply nested table relation */ |
||
| 234 | public function getOrderItemsQuery(): ActiveQuery |
||
| 235 | { |
||
| 236 | $rel = $this->hasMany(Item::class, ['id' => 'item_id']); |
||
| 237 | |||
| 238 | return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) { |
||
| 239 | /* @var $q ActiveQuery */ |
||
| 240 | $q->viaTable('order', ['customer_id' => 'id']); |
||
| 241 | })->orderBy('id'); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function getOrderItems2(): array |
||
| 247 | } |
||
| 248 | |||
| 249 | public function getOrderItems2Query(): ActiveQuery |
||
| 250 | { |
||
| 251 | return $this->hasMany(OrderItem::class, ['order_id' => 'id']) |
||
| 252 | ->via('ordersNoOrder'); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function getItems2(): array |
||
| 256 | { |
||
| 257 | return $this->relation('items2'); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function getItems2Query(): ActiveQuery |
||
| 261 | { |
||
| 262 | return $this->hasMany(Item::class, ['id' => 'item_id']) |
||
| 263 | ->via('orderItems2'); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function getOrdersUsingInstance(): array |
||
| 269 | } |
||
| 270 | } |
||
| 271 |