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