| Conditions | 7 |
| Paths | 64 |
| Total Lines | 112 |
| 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 |
||
| 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(BatchQueryResultInterface::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 | $step = 0; |
||
| 38 | foreach ($batch as $rows) { |
||
| 39 | $allRows = array_merge($allRows, $rows); |
||
| 40 | $step++; |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->assertCount(3, $allRows); |
||
| 44 | $this->assertEquals(2, $step); |
||
| 45 | $this->assertEquals('user1', $allRows[0]['name']); |
||
| 46 | $this->assertEquals('user2', $allRows[1]['name']); |
||
| 47 | $this->assertEquals('user3', $allRows[2]['name']); |
||
| 48 | |||
| 49 | /* rewind */ |
||
| 50 | $allRows = []; |
||
| 51 | |||
| 52 | $step = 0; |
||
| 53 | foreach ($batch as $rows) { |
||
| 54 | $allRows = array_merge($allRows, $rows); |
||
| 55 | $step++; |
||
| 56 | } |
||
| 57 | |||
| 58 | $this->assertCount(3, $allRows); |
||
| 59 | $this->assertEquals(2, $step); |
||
| 60 | |||
| 61 | /* reset */ |
||
| 62 | $batch->reset(); |
||
| 63 | |||
| 64 | /* empty query */ |
||
| 65 | $query = new Query($db); |
||
| 66 | |||
| 67 | $query->from('customer')->where(['id' => 100]); |
||
| 68 | |||
| 69 | $allRows = []; |
||
| 70 | |||
| 71 | $batch = $query->batch(2); |
||
| 72 | |||
| 73 | foreach ($batch as $rows) { |
||
| 74 | $allRows = array_merge($allRows, $rows); |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->assertCount(0, $allRows); |
||
| 78 | |||
| 79 | /* query with index */ |
||
| 80 | $query = new Query($db); |
||
| 81 | |||
| 82 | $query->from('customer')->indexBy('name'); |
||
| 83 | |||
| 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->assertEquals('address1', $allRows['user1']['address']); |
||
| 92 | $this->assertEquals('address2', $allRows['user2']['address']); |
||
| 93 | $this->assertEquals('address3', $allRows['user3']['address']); |
||
| 94 | |||
| 95 | /* each */ |
||
| 96 | $query = new Query($db); |
||
| 97 | |||
| 98 | $query->from('customer')->orderBy('id'); |
||
| 99 | $allRows = []; |
||
| 100 | |||
| 101 | foreach ($query->each(2) as $index => $row) { |
||
| 102 | /** @psalm-suppress PossiblyNullArrayOffset */ |
||
| 103 | $allRows[$index] = $row; |
||
| 104 | } |
||
| 105 | $this->assertCount(3, $allRows); |
||
| 106 | $this->assertEquals('user1', $allRows[0]['name']); |
||
| 107 | $this->assertEquals('user2', $allRows[1]['name']); |
||
| 108 | $this->assertEquals('user3', $allRows[2]['name']); |
||
| 109 | |||
| 110 | /* each with key */ |
||
| 111 | $query = new Query($db); |
||
| 112 | |||
| 113 | $query->from('customer')->orderBy('id')->indexBy('name'); |
||
| 114 | |||
| 115 | $allRows = []; |
||
| 116 | |||
| 117 | foreach ($query->each(100) as $key => $row) { |
||
| 118 | /** @psalm-suppress PossiblyNullArrayOffset */ |
||
| 119 | $allRows[$key] = $row; |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->assertCount(3, $allRows); |
||
| 123 | $this->assertEquals('address1', $allRows['user1']['address']); |
||
| 124 | $this->assertEquals('address2', $allRows['user2']['address']); |
||
| 125 | $this->assertEquals('address3', $allRows['user3']['address']); |
||
| 126 | } |
||
| 183 |