| 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 |
||
| 45 | public function testQuery(): void |
||
| 46 | { |
||
| 47 | /* initialize property test */ |
||
| 48 | $db = $this->getConnection(); |
||
| 49 | |||
| 50 | $query = new Query($db); |
||
| 51 | $query->from('customer')->orderBy('id'); |
||
| 52 | $result = $query->batch(2); |
||
| 53 | |||
| 54 | $this->assertInstanceOf(BatchQueryResultInterface::class, $result); |
||
| 55 | $this->assertSame(2, $result->getBatchSize()); |
||
| 56 | $this->assertSame($result->getQuery(), $query); |
||
| 57 | |||
| 58 | /* normal query */ |
||
| 59 | $query = new Query($db); |
||
| 60 | $query->from('customer')->orderBy('id'); |
||
| 61 | $allRows = []; |
||
| 62 | $batch = $query->batch(2); |
||
| 63 | $step = 0; |
||
| 64 | |||
| 65 | foreach ($batch as $rows) { |
||
| 66 | $allRows = array_merge($allRows, $rows); |
||
| 67 | $step++; |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->assertCount(3, $allRows); |
||
| 71 | $this->assertSame(2, $step); |
||
| 72 | $this->assertSame('user1', $allRows[0]['name']); |
||
| 73 | $this->assertSame('user2', $allRows[1]['name']); |
||
| 74 | $this->assertSame('user3', $allRows[2]['name']); |
||
| 75 | |||
| 76 | /* rewind */ |
||
| 77 | $allRows = []; |
||
| 78 | $step = 0; |
||
| 79 | |||
| 80 | foreach ($batch as $rows) { |
||
| 81 | $allRows = array_merge($allRows, $rows); |
||
| 82 | $step++; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->assertCount(3, $allRows); |
||
| 86 | $this->assertSame(2, $step); |
||
| 87 | |||
| 88 | /* reset */ |
||
| 89 | $batch->reset(); |
||
| 90 | |||
| 91 | /* empty query */ |
||
| 92 | $query = new Query($db); |
||
| 93 | $query->from('customer')->where(['id' => 100]); |
||
| 94 | $allRows = []; |
||
| 95 | $batch = $query->batch(2); |
||
| 96 | |||
| 97 | foreach ($batch as $rows) { |
||
| 98 | $allRows = array_merge($allRows, $rows); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->assertCount(0, $allRows); |
||
| 102 | |||
| 103 | /* query with index */ |
||
| 104 | $query = new Query($db); |
||
| 105 | $query->from('customer')->indexBy('name'); |
||
| 106 | $allRows = []; |
||
| 107 | |||
| 108 | foreach ($query->batch(2) as $rows) { |
||
| 109 | $allRows = array_merge($allRows, $rows); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->assertCount(3, $allRows); |
||
| 113 | $this->assertSame('address1', $allRows['user1']['address']); |
||
| 114 | $this->assertSame('address2', $allRows['user2']['address']); |
||
| 115 | $this->assertSame('address3', $allRows['user3']['address']); |
||
| 116 | |||
| 117 | /* each */ |
||
| 118 | $query = new Query($db); |
||
| 119 | $query->from('customer')->orderBy('id'); |
||
| 120 | $allRows = []; |
||
| 121 | |||
| 122 | foreach ($query->each(2) as $index => $row) { |
||
| 123 | $allRows[$index] = $row; |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->assertCount(3, $allRows); |
||
| 127 | $this->assertSame('user1', $allRows[0]['name']); |
||
| 128 | $this->assertSame('user2', $allRows[1]['name']); |
||
| 129 | $this->assertSame('user3', $allRows[2]['name']); |
||
| 130 | |||
| 131 | /* each with key */ |
||
| 132 | $query = new Query($db); |
||
| 133 | $query->from('customer')->orderBy('id')->indexBy('name'); |
||
| 134 | $allRows = []; |
||
| 135 | |||
| 136 | foreach ($query->each(100) as $key => $row) { |
||
| 137 | $allRows[$key] = $row; |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->assertCount(3, $allRows); |
||
| 141 | $this->assertSame('address1', $allRows['user1']['address']); |
||
| 142 | $this->assertSame('address2', $allRows['user2']['address']); |
||
| 143 | $this->assertSame('address3', $allRows['user3']['address']); |
||
| 144 | } |
||
| 168 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.