| Conditions | 7 |
| Paths | 64 |
| Total Lines | 103 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 13 | public function testQuery(): void |
||
| 14 | { |
||
| 15 | $this->checkFixture($this->db, 'customer', true); |
||
| 16 | |||
| 17 | $customerQuery = new ActiveQuery(Customer::class, $this->db); |
||
| 18 | |||
| 19 | $query = $customerQuery->orderBy('id'); |
||
| 20 | |||
| 21 | $result = $query->batch(2); |
||
| 22 | |||
| 23 | $this->assertInstanceOf(BatchQueryResultInterface::class, $result); |
||
| 24 | $this->assertEquals(2, $result->getBatchSize()); |
||
| 25 | $this->assertSame($result->getQuery(), $query); |
||
| 26 | |||
| 27 | /** normal query */ |
||
| 28 | $customerQuery = new ActiveQuery(Customer::class, $this->db); |
||
| 29 | |||
| 30 | $query = $customerQuery->orderBy('id'); |
||
| 31 | |||
| 32 | $allRows = []; |
||
| 33 | |||
| 34 | $batch = $query->batch(2); |
||
| 35 | |||
| 36 | foreach ($batch as $rows) { |
||
| 37 | $allRows = array_merge($allRows, $rows); |
||
| 38 | } |
||
| 39 | |||
| 40 | $this->assertCount(3, $allRows); |
||
| 41 | $this->assertEquals('user1', $allRows[0]->getName()); |
||
| 42 | $this->assertEquals('user2', $allRows[1]->getName()); |
||
| 43 | $this->assertEquals('user3', $allRows[2]->getName()); |
||
| 44 | |||
| 45 | /** rewind */ |
||
| 46 | $allRows = []; |
||
| 47 | |||
| 48 | foreach ($batch as $rows) { |
||
| 49 | $allRows = array_merge($allRows, $rows); |
||
| 50 | } |
||
| 51 | |||
| 52 | $this->assertCount(3, $allRows); |
||
| 53 | |||
| 54 | /** reset */ |
||
| 55 | $batch->reset(); |
||
| 56 | |||
| 57 | /** empty query */ |
||
| 58 | $query = $customerQuery->where(['id' => 100]); |
||
| 59 | |||
| 60 | $allRows = []; |
||
| 61 | |||
| 62 | $batch = $query->batch(2); |
||
| 63 | |||
| 64 | foreach ($batch as $rows) { |
||
| 65 | $allRows = array_merge($allRows, $rows); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->assertCount(0, $allRows); |
||
| 69 | |||
| 70 | /** query with index */ |
||
| 71 | $customerQuery = new ActiveQuery(Customer::class, $this->db); |
||
| 72 | |||
| 73 | $query = $customerQuery->indexBy('name'); |
||
| 74 | |||
| 75 | $allRows = []; |
||
| 76 | |||
| 77 | foreach ($query->batch(2) as $rows) { |
||
| 78 | $allRows = array_merge($allRows, $rows); |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->assertCount(3, $allRows); |
||
| 82 | $this->assertEquals('address1', $allRows['user1']->getAddress()); |
||
| 83 | $this->assertEquals('address2', $allRows['user2']->getAddress()); |
||
| 84 | $this->assertEquals('address3', $allRows['user3']->getAddress()); |
||
| 85 | |||
| 86 | /** each */ |
||
| 87 | $customerQuery = new ActiveQuery(Customer::class, $this->db); |
||
| 88 | |||
| 89 | $query = $customerQuery->orderBy('id'); |
||
| 90 | |||
| 91 | $allRows = []; |
||
| 92 | |||
| 93 | foreach ($query->each(2) as $index => $row) { |
||
| 94 | $allRows[$index] = $row; |
||
| 95 | } |
||
| 96 | $this->assertCount(3, $allRows); |
||
| 97 | $this->assertEquals('user1', $allRows[0]->getName()); |
||
| 98 | $this->assertEquals('user2', $allRows[1]->getName()); |
||
| 99 | $this->assertEquals('user3', $allRows[2]->getName()); |
||
| 100 | |||
| 101 | /** each with key */ |
||
| 102 | $customerQuery = new ActiveQuery(Customer::class, $this->db); |
||
| 103 | |||
| 104 | $query = $customerQuery->orderBy('id')->indexBy('name'); |
||
| 105 | |||
| 106 | $allRows = []; |
||
| 107 | |||
| 108 | foreach ($query->each(100) as $key => $row) { |
||
| 109 | $allRows[$key] = $row; |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->assertCount(3, $allRows); |
||
| 113 | $this->assertEquals('address1', $allRows['user1']->getAddress()); |
||
| 114 | $this->assertEquals('address2', $allRows['user2']->getAddress()); |
||
| 115 | $this->assertEquals('address3', $allRows['user3']->getAddress()); |
||
| 116 | } |
||
| 177 |