| Conditions | 1 |
| Paths | 1 |
| Total Lines | 55 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 testMultisort(): void |
||
| 15 | { |
||
| 16 | // empty key |
||
| 17 | $dataEmpty = []; |
||
| 18 | ArraySorter::multisort($dataEmpty, ''); |
||
| 19 | $this->assertEquals([], $dataEmpty); |
||
| 20 | |||
| 21 | // single key |
||
| 22 | $array = [ |
||
| 23 | ['name' => 'b', 'age' => 3], |
||
| 24 | ['name' => 'a', 'age' => 1], |
||
| 25 | ['name' => 'c', 'age' => 2], |
||
| 26 | ]; |
||
| 27 | ArraySorter::multisort($array, 'name'); |
||
| 28 | $this->assertEquals(['name' => 'a', 'age' => 1], $array[0]); |
||
| 29 | $this->assertEquals(['name' => 'b', 'age' => 3], $array[1]); |
||
| 30 | $this->assertEquals(['name' => 'c', 'age' => 2], $array[2]); |
||
| 31 | |||
| 32 | // multiple keys |
||
| 33 | $array = [ |
||
| 34 | ['name' => 'b', 'age' => 3], |
||
| 35 | ['name' => 'a', 'age' => 2], |
||
| 36 | ['name' => 'a', 'age' => 1], |
||
| 37 | ]; |
||
| 38 | ArraySorter::multisort($array, ['name', 'age']); |
||
| 39 | $this->assertEquals(['name' => 'a', 'age' => 1], $array[0]); |
||
| 40 | $this->assertEquals(['name' => 'a', 'age' => 2], $array[1]); |
||
| 41 | $this->assertEquals(['name' => 'b', 'age' => 3], $array[2]); |
||
| 42 | |||
| 43 | // case-insensitive |
||
| 44 | $array = [ |
||
| 45 | ['name' => 'a', 'age' => 3], |
||
| 46 | ['name' => 'b', 'age' => 2], |
||
| 47 | ['name' => 'B', 'age' => 4], |
||
| 48 | ['name' => 'A', 'age' => 1], |
||
| 49 | ]; |
||
| 50 | |||
| 51 | ArraySorter::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING, SORT_REGULAR]); |
||
| 52 | $this->assertEquals(['name' => 'A', 'age' => 1], $array[0]); |
||
| 53 | $this->assertEquals(['name' => 'B', 'age' => 4], $array[1]); |
||
| 54 | $this->assertEquals(['name' => 'a', 'age' => 3], $array[2]); |
||
| 55 | $this->assertEquals(['name' => 'b', 'age' => 2], $array[3]); |
||
| 56 | |||
| 57 | ArraySorter::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING | SORT_FLAG_CASE, SORT_REGULAR]); |
||
| 58 | $this->assertEquals(['name' => 'A', 'age' => 1], $array[0]); |
||
| 59 | $this->assertEquals(['name' => 'a', 'age' => 3], $array[1]); |
||
| 60 | $this->assertEquals(['name' => 'b', 'age' => 2], $array[2]); |
||
| 61 | $this->assertEquals(['name' => 'B', 'age' => 4], $array[3]); |
||
| 62 | |||
| 63 | ArraySorter::multisort($array, fn ($item) => ['age', 'name'], SORT_DESC); |
||
| 64 | |||
| 65 | $this->assertEquals(['name' => 'B', 'age' => 4], $array[0]); |
||
| 66 | $this->assertEquals(['name' => 'a', 'age' => 3], $array[1]); |
||
| 67 | $this->assertEquals(['name' => 'b', 'age' => 2], $array[2]); |
||
| 68 | $this->assertEquals(['name' => 'A', 'age' => 1], $array[3]); |
||
| 69 | } |
||
| 187 |