| Conditions | 5 |
| Paths | 16 |
| Total Lines | 90 |
| Code Lines | 58 |
| 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 |
||
| 16 | public function testBatchQueryResult(): void |
||
| 17 | { |
||
| 18 | // initialize property test |
||
| 19 | $db = $this->getConnection(true); |
||
| 20 | |||
| 21 | $query = new Query($db); |
||
| 22 | $query->from('customer')->orderBy('id'); |
||
| 23 | $result = $query->batch(2); |
||
| 24 | |||
| 25 | $this->assertInstanceOf(BatchQueryResultInterface::class, $result); |
||
| 26 | $this->assertSame(2, $result->getBatchSize()); |
||
| 27 | $this->assertSame($result->getQuery(), $query); |
||
| 28 | |||
| 29 | // normal query |
||
| 30 | $query = new Query($db); |
||
| 31 | $query->from('customer')->orderBy('id'); |
||
| 32 | $allRows = []; |
||
| 33 | $batch = $query->batch(2); |
||
| 34 | $step = 0; |
||
| 35 | |||
| 36 | foreach ($batch as $rows) { |
||
| 37 | $allRows = array_merge($allRows, $rows); |
||
| 38 | $step++; |
||
| 39 | } |
||
| 40 | |||
| 41 | $this->assertCount(3, $allRows); |
||
| 42 | $this->assertSame(2, $step); |
||
| 43 | $this->assertSame('user1', $allRows[0]['name']); |
||
| 44 | $this->assertSame('user2', $allRows[1]['name']); |
||
| 45 | $this->assertSame('user3', $allRows[2]['name']); |
||
| 46 | |||
| 47 | // rewind |
||
| 48 | $allRows = []; |
||
| 49 | $step = 0; |
||
| 50 | |||
| 51 | foreach ($batch as $rows) { |
||
| 52 | $allRows = array_merge($allRows, $rows); |
||
| 53 | $step++; |
||
| 54 | } |
||
| 55 | |||
| 56 | $this->assertCount(3, $allRows); |
||
| 57 | $this->assertSame(2, $step); |
||
| 58 | |||
| 59 | $batch->reset(); |
||
| 60 | |||
| 61 | // empty query |
||
| 62 | $query = new Query($db); |
||
| 63 | $query->from('customer')->where(['id' => 100]); |
||
| 64 | $allRows = []; |
||
| 65 | $batch = $query->batch(2); |
||
| 66 | |||
| 67 | foreach ($batch as $rows) { |
||
| 68 | $allRows = array_merge($allRows, $rows); |
||
| 69 | } |
||
| 70 | |||
| 71 | $this->assertCount(0, $allRows); |
||
| 72 | |||
| 73 | // query with index |
||
| 74 | $query = new Query($db); |
||
| 75 | $query->from('customer')->indexBy('name'); |
||
| 76 | $allRows = []; |
||
| 77 | |||
| 78 | foreach ($query->batch(2) as $rows) { |
||
| 79 | $allRows = array_merge($allRows, $rows); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->assertCount(3, $allRows); |
||
| 83 | $this->assertSame('address1', $allRows['user1']['address']); |
||
| 84 | $this->assertSame('address2', $allRows['user2']['address']); |
||
| 85 | $this->assertSame('address3', $allRows['user3']['address']); |
||
| 86 | |||
| 87 | // each |
||
| 88 | $query = new Query($db); |
||
| 89 | $query->from('customer')->orderBy('id'); |
||
| 90 | $allRows = $this->getAllRowsFromEach($query->each(2)); |
||
| 91 | |||
| 92 | $this->assertCount(3, $allRows); |
||
| 93 | $this->assertSame('user1', $allRows[0]['name']); |
||
| 94 | $this->assertSame('user2', $allRows[1]['name']); |
||
| 95 | $this->assertSame('user3', $allRows[2]['name']); |
||
| 96 | |||
| 97 | // each with key |
||
| 98 | $query = new Query($db); |
||
| 99 | $query->from('customer')->orderBy('id')->indexBy('name'); |
||
| 100 | $allRows = $this->getAllRowsFromEach($query->each(100)); |
||
| 101 | |||
| 102 | $this->assertCount(3, $allRows); |
||
| 103 | $this->assertSame('address1', $allRows['user1']['address']); |
||
| 104 | $this->assertSame('address2', $allRows['user2']['address']); |
||
| 105 | $this->assertSame('address3', $allRows['user3']['address']); |
||
| 106 | } |
||
| 158 |