Customer   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 42
eloc 75
c 3
b 0
f 0
dl 0
loc 255
rs 9.0399

42 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 3 1
A getExpensiveOrdersWithNullFKQuery() 0 6 1
A getOrdersWithItemsQuery() 0 3 1
A getOrderItems2() 0 3 1
A getOrders2() 0 3 1
A getName() 0 3 1
A getItem() 0 2 1
A getEmail() 0 3 1
A getId() 0 3 1
A getOrders2Query() 0 3 1
A getStatus() 0 3 1
A setAddress() 0 3 1
A getOrderItems() 0 3 1
A getExpensiveOrdersWithNullFK() 0 3 1
A getOrdersNoOrderQuery() 0 3 1
A getOrderItemsQuery() 0 8 1
A getBoolStatus() 0 3 1
A getProfile() 0 3 1
A getOrdersPlainQuery() 0 3 1
A getOrders() 0 3 1
A getOrdersWithNullFKQuery() 0 3 1
A setProfileId() 0 3 1
A getItems2() 0 3 1
A setBoolStatus() 0 3 1
A getItems2Query() 0 4 1
A getProfileId() 0 3 1
A getAddress() 0 3 1
A getOrdersQuery() 0 3 1
A getOrdersWithNullFK() 0 3 1
A getExpensiveOrders() 0 3 1
A setName() 0 3 1
A getOrderItems2Query() 0 4 1
A setId() 0 3 1
A getOrdersPlain() 0 3 1
A getProfileQuery() 0 3 1
A getOrdersNoOrder() 0 3 1
A getExpensiveOrdersQuery() 0 3 1
A setStatus() 0 3 1
A setEmail() 0 3 1
A getOrdersWithItems() 0 3 1
A relationQuery() 0 17 1
A getOrdersUsingInstance() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
6
7
use Yiisoft\ActiveRecord\ActiveQuery;
8
use Yiisoft\ActiveRecord\ActiveQueryInterface;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;
10
11
/**
12
 * Class Customer.
13
 */
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
            '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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->bool_status could return the type string which is incompatible with the type-hinted return boolean|null. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('profile') could return the type array which is incompatible with the type-hinted return Yiisoft\ActiveRecord\Tes...tiveRecord\Profile|null. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('ordersPlain') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('orders') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('ordersNoOrder') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('expensiveOrders') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('ordersWithItems') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('...nsiveOrdersWithNullFK') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
198
    }
199
200
    public function getExpensiveOrdersWithNullFKQuery(): ActiveQuery
201
    {
202
        return $this->hasMany(
203
            OrderWithNullFK::class,
204
            ['customer_id' => 'id']
205
        )->andWhere('[[total]] > 50')->orderBy('id');
206
    }
207
208
    public function getOrdersWithNullFK(): array
209
    {
210
        return $this->relation('ordersWithNullFK');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('ordersWithNullFK') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('orders2') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('orderItems') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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
245
    {
246
        return $this->relation('orderItems2');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('orderItems2') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('items2') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
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
267
    {
268
        return $this->relation('ordersUsingInstance');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->relation('ordersUsingInstance') could return the type Yiisoft\ActiveRecord\ActiveRecordInterface|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
269
    }
270
}
271