| Conditions | 1 |
| Paths | 1 |
| Total Lines | 82 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 15 | public function testKeepOrder() |
||
| 16 | { |
||
| 17 | $originalJson = <<<'JSON' |
||
| 18 | { |
||
| 19 | "key1": [4, 1, 2, 3], |
||
| 20 | "key2": 2, |
||
| 21 | "key3": { |
||
| 22 | "sub0": 0, |
||
| 23 | "sub1": "a", |
||
| 24 | "sub2": "b" |
||
| 25 | }, |
||
| 26 | "key4": [ |
||
| 27 | {"a":1, "b":true}, {"a":2, "b":false}, {"a":3} |
||
| 28 | ] |
||
| 29 | } |
||
| 30 | JSON; |
||
| 31 | |||
| 32 | $newJson = <<<'JSON' |
||
| 33 | { |
||
| 34 | "key5": "wat", |
||
| 35 | "key1": [5, 1, 2, 3], |
||
| 36 | "key4": [ |
||
| 37 | {"c":false, "a":2}, {"a":1, "b":true}, {"c":1, "a":3} |
||
| 38 | ], |
||
| 39 | "key3": { |
||
| 40 | "sub3": 0, |
||
| 41 | "sub2": false, |
||
| 42 | "sub1": "c" |
||
| 43 | } |
||
| 44 | } |
||
| 45 | JSON; |
||
| 46 | |||
| 47 | $expected = <<<'JSON' |
||
| 48 | { |
||
| 49 | "key1": [5, 1, 2, 3], |
||
| 50 | "key3": { |
||
| 51 | "sub1": "c", |
||
| 52 | "sub2": false, |
||
| 53 | "sub3": 0 |
||
| 54 | }, |
||
| 55 | "key4": [ |
||
| 56 | {"a":1, "b":true}, {"a":2, "c":false}, {"a":3, "c":1} |
||
| 57 | ], |
||
| 58 | "key5": "wat" |
||
| 59 | } |
||
| 60 | JSON; |
||
| 61 | |||
| 62 | $r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS + JsonDiff::COLLECT_MODIFIED_DIFF); |
||
| 63 | $this->assertSame( |
||
| 64 | json_encode(json_decode($expected), JSON_PRETTY_PRINT), |
||
| 65 | json_encode($r->getRearranged(), JSON_PRETTY_PRINT) |
||
| 66 | ); |
||
| 67 | $this->assertSame('{"key3":{"sub3":0},"key4":{"1":{"c":false},"2":{"c":1}},"key5":"wat"}', |
||
| 68 | json_encode($r->getAdded())); |
||
| 69 | $this->assertSame(array( |
||
| 70 | '/key3/sub3', |
||
| 71 | '/key4/1/c', |
||
| 72 | '/key4/2/c', |
||
| 73 | '/key5', |
||
| 74 | ), $r->getAddedPaths()); |
||
| 75 | $this->assertSame('{"key2":2,"key3":{"sub0":0},"key4":{"1":{"b":false}}}', |
||
| 76 | json_encode($r->getRemoved())); |
||
| 77 | $this->assertSame(array( |
||
| 78 | '/key2', |
||
| 79 | '/key3/sub0', |
||
| 80 | '/key4/1/b', |
||
| 81 | ), $r->getRemovedPaths()); |
||
| 82 | |||
| 83 | $this->assertSame(array( |
||
| 84 | '/key1/0', |
||
| 85 | '/key3/sub1', |
||
| 86 | '/key3/sub2', |
||
| 87 | ), $r->getModifiedPaths()); |
||
| 88 | |||
| 89 | $this->assertSame('{"key1":[4],"key3":{"sub1":"a","sub2":"b"}}', json_encode($r->getModifiedOriginal())); |
||
| 90 | $this->assertSame('{"key1":[5],"key3":{"sub1":"c","sub2":false}}', json_encode($r->getModifiedNew())); |
||
| 91 | |||
| 92 | $this->assertEquals([ |
||
| 93 | new ModifiedPathDiff('/key1/0', 4, 5), |
||
| 94 | new ModifiedPathDiff('/key3/sub1', 'a', 'c'), |
||
| 95 | new ModifiedPathDiff('/key3/sub2', 'b', false), |
||
| 96 | ], $r->getModifiedDiff()); |
||
| 97 | } |
||
| 235 | } |