| Conditions | 7 |
| Paths | 64 |
| Total Lines | 107 |
| Code Lines | 57 |
| 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 |
||
| 15 | public function testQuery(): void |
||
| 16 | { |
||
| 17 | $db = Customer::getConnection(); |
||
| 18 | |||
| 19 | $this->loadFixture($db); |
||
| 20 | |||
| 21 | /** initialize property test */ |
||
| 22 | $query = new Query($db); |
||
| 23 | |||
| 24 | $query->from('customer')->orderBy('id'); |
||
| 25 | |||
| 26 | $result = $query->batch(2); |
||
| 27 | |||
| 28 | $this->assertInstanceOf(BatchQueryResult::class, $result); |
||
| 29 | $this->assertEquals(2, $result->getBatchSize()); |
||
| 30 | $this->assertSame($result->getQuery(), $query); |
||
| 31 | |||
| 32 | /** normal query */ |
||
| 33 | $query = new Query($db); |
||
| 34 | |||
| 35 | $query->from('customer')->orderBy('id'); |
||
| 36 | |||
| 37 | $allRows = []; |
||
| 38 | |||
| 39 | $batch = $query->batch(2); |
||
| 40 | |||
| 41 | foreach ($batch as $rows) { |
||
| 42 | $allRows = array_merge($allRows, $rows); |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->assertCount(3, $allRows); |
||
| 46 | $this->assertEquals('user1', $allRows[0]['name']); |
||
| 47 | $this->assertEquals('user2', $allRows[1]['name']); |
||
| 48 | $this->assertEquals('user3', $allRows[2]['name']); |
||
| 49 | |||
| 50 | /** rewind */ |
||
| 51 | $allRows = []; |
||
| 52 | |||
| 53 | foreach ($batch as $rows) { |
||
| 54 | $allRows = array_merge($allRows, $rows); |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->assertCount(3, $allRows); |
||
| 58 | |||
| 59 | /** reset */ |
||
| 60 | $batch->reset(); |
||
| 61 | |||
| 62 | /** empty query */ |
||
| 63 | $query = new Query($db); |
||
| 64 | |||
| 65 | $query->from('customer')->where(['id' => 100]); |
||
| 66 | |||
| 67 | $allRows = []; |
||
| 68 | |||
| 69 | $batch = $query->batch(2); |
||
| 70 | |||
| 71 | foreach ($batch as $rows) { |
||
| 72 | $allRows = array_merge($allRows, $rows); |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->assertCount(0, $allRows); |
||
| 76 | |||
| 77 | /** query with index */ |
||
| 78 | $query = new Query($db); |
||
| 79 | |||
| 80 | $query->from('customer')->indexBy('name'); |
||
| 81 | |||
| 82 | $allRows = []; |
||
| 83 | |||
| 84 | foreach ($query->batch(2) as $rows) { |
||
| 85 | $allRows = array_merge($allRows, $rows); |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->assertCount(3, $allRows); |
||
| 89 | $this->assertEquals('address1', $allRows['user1']['address']); |
||
| 90 | $this->assertEquals('address2', $allRows['user2']['address']); |
||
| 91 | $this->assertEquals('address3', $allRows['user3']['address']); |
||
| 92 | |||
| 93 | /** each */ |
||
| 94 | $query = new Query($db); |
||
| 95 | |||
| 96 | $query->from('customer')->orderBy('id'); |
||
| 97 | $allRows = []; |
||
| 98 | |||
| 99 | foreach ($query->each(2) as $index => $row) { |
||
| 100 | $allRows[$index] = $row; |
||
| 101 | } |
||
| 102 | $this->assertCount(3, $allRows); |
||
| 103 | $this->assertEquals('user1', $allRows[0]['name']); |
||
| 104 | $this->assertEquals('user2', $allRows[1]['name']); |
||
| 105 | $this->assertEquals('user3', $allRows[2]['name']); |
||
| 106 | |||
| 107 | /** each with key */ |
||
| 108 | $query = new Query($db); |
||
| 109 | |||
| 110 | $query->from('customer')->orderBy('id')->indexBy('name'); |
||
| 111 | |||
| 112 | $allRows = []; |
||
| 113 | |||
| 114 | foreach ($query->each(100) as $key => $row) { |
||
| 115 | $allRows[$key] = $row; |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->assertCount(3, $allRows); |
||
| 119 | $this->assertEquals('address1', $allRows['user1']['address']); |
||
| 120 | $this->assertEquals('address2', $allRows['user2']['address']); |
||
| 121 | $this->assertEquals('address3', $allRows['user3']['address']); |
||
| 122 | } |
||
| 179 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.