Conditions | 7 |
Paths | 64 |
Total Lines | 105 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
14 | public function testQuery(): void |
||
15 | { |
||
16 | /* initialize property test */ |
||
17 | $db = $this->getConnection(true); |
||
|
|||
18 | |||
19 | $query = new Query($db); |
||
20 | |||
21 | $query->from('customer')->orderBy('id'); |
||
22 | |||
23 | $result = $query->batch(2); |
||
24 | |||
25 | $this->assertInstanceOf(BatchQueryResult::class, $result); |
||
26 | $this->assertEquals(2, $result->getBatchSize()); |
||
27 | $this->assertSame($result->getQuery(), $query); |
||
28 | |||
29 | /* normal query */ |
||
30 | $query = new Query($db); |
||
31 | |||
32 | $query->from('customer')->orderBy('id'); |
||
33 | |||
34 | $allRows = []; |
||
35 | |||
36 | $batch = $query->batch(2); |
||
37 | |||
38 | foreach ($batch as $rows) { |
||
39 | $allRows = array_merge($allRows, $rows); |
||
40 | } |
||
41 | |||
42 | $this->assertCount(3, $allRows); |
||
43 | $this->assertEquals('user1', $allRows[0]['name']); |
||
44 | $this->assertEquals('user2', $allRows[1]['name']); |
||
45 | $this->assertEquals('user3', $allRows[2]['name']); |
||
46 | |||
47 | /* rewind */ |
||
48 | $allRows = []; |
||
49 | |||
50 | foreach ($batch as $rows) { |
||
51 | $allRows = array_merge($allRows, $rows); |
||
52 | } |
||
53 | |||
54 | $this->assertCount(3, $allRows); |
||
55 | |||
56 | /* reset */ |
||
57 | $batch->reset(); |
||
58 | |||
59 | /* empty query */ |
||
60 | $query = new Query($db); |
||
61 | |||
62 | $query->from('customer')->where(['id' => 100]); |
||
63 | |||
64 | $allRows = []; |
||
65 | |||
66 | $batch = $query->batch(2); |
||
67 | |||
68 | foreach ($batch as $rows) { |
||
69 | $allRows = array_merge($allRows, $rows); |
||
70 | } |
||
71 | |||
72 | $this->assertCount(0, $allRows); |
||
73 | |||
74 | /* query with index */ |
||
75 | $query = new Query($db); |
||
76 | |||
77 | $query->from('customer')->indexBy('name'); |
||
78 | |||
79 | $allRows = []; |
||
80 | |||
81 | foreach ($query->batch(2) as $rows) { |
||
82 | $allRows = array_merge($allRows, $rows); |
||
83 | } |
||
84 | |||
85 | $this->assertCount(3, $allRows); |
||
86 | $this->assertEquals('address1', $allRows['user1']['address']); |
||
87 | $this->assertEquals('address2', $allRows['user2']['address']); |
||
88 | $this->assertEquals('address3', $allRows['user3']['address']); |
||
89 | |||
90 | /* each */ |
||
91 | $query = new Query($db); |
||
92 | |||
93 | $query->from('customer')->orderBy('id'); |
||
94 | $allRows = []; |
||
95 | |||
96 | foreach ($query->each(2) as $index => $row) { |
||
97 | $allRows[$index] = $row; |
||
98 | } |
||
99 | $this->assertCount(3, $allRows); |
||
100 | $this->assertEquals('user1', $allRows[0]['name']); |
||
101 | $this->assertEquals('user2', $allRows[1]['name']); |
||
102 | $this->assertEquals('user3', $allRows[2]['name']); |
||
103 | |||
104 | /* each with key */ |
||
105 | $query = new Query($db); |
||
106 | |||
107 | $query->from('customer')->orderBy('id')->indexBy('name'); |
||
108 | |||
109 | $allRows = []; |
||
110 | |||
111 | foreach ($query->each(100) as $key => $row) { |
||
112 | $allRows[$key] = $row; |
||
113 | } |
||
114 | |||
115 | $this->assertCount(3, $allRows); |
||
116 | $this->assertEquals('address1', $allRows['user1']['address']); |
||
117 | $this->assertEquals('address2', $allRows['user2']['address']); |
||
118 | $this->assertEquals('address3', $allRows['user3']['address']); |
||
119 | } |
||
175 |