| Conditions | 1 |
| Paths | 1 |
| Total Lines | 70 |
| Code Lines | 63 |
| 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 testDiff() |
||
| 15 | { |
||
| 16 | $originalJson = <<<'JSON' |
||
| 17 | { |
||
| 18 | "key1": [4, 1, 2, 3], |
||
| 19 | "key2": 2, |
||
| 20 | "key3": { |
||
| 21 | "sub0": 0, |
||
| 22 | "sub1": "a", |
||
| 23 | "sub2": "b" |
||
| 24 | }, |
||
| 25 | "key4": [ |
||
| 26 | {"a":1, "b":true, "subs": [{"s":1}, {"s":2}, {"s":3}]}, {"a":2, "b":false}, {"a":3} |
||
| 27 | ] |
||
| 28 | } |
||
| 29 | JSON; |
||
| 30 | |||
| 31 | $newJson = <<<'JSON' |
||
| 32 | { |
||
| 33 | "key5": "wat", |
||
| 34 | "key1": [5, 1, 2, 3], |
||
| 35 | "key4": [ |
||
| 36 | {"c":false, "a":2}, {"a":1, "b":true, "subs": [{"s":3, "add": true}, {"s":2}, {"s":1}]}, {"c":1, "a":3} |
||
| 37 | ], |
||
| 38 | "key3": { |
||
| 39 | "sub3": 0, |
||
| 40 | "sub2": false, |
||
| 41 | "sub1": "c" |
||
| 42 | } |
||
| 43 | } |
||
| 44 | JSON; |
||
| 45 | |||
| 46 | $patchJson = <<<'JSON' |
||
| 47 | [ |
||
| 48 | {"value":4,"op":"test","path":"/key1/0"}, |
||
| 49 | {"value":5,"op":"replace","path":"/key1/0"}, |
||
| 50 | |||
| 51 | {"op":"remove","path":"/key2"}, |
||
| 52 | |||
| 53 | {"op":"remove","path":"/key3/sub0"}, |
||
| 54 | |||
| 55 | {"value":"a","op":"test","path":"/key3/sub1"}, |
||
| 56 | {"value":"c","op":"replace","path":"/key3/sub1"}, |
||
| 57 | |||
| 58 | {"value":"b","op":"test","path":"/key3/sub2"}, |
||
| 59 | {"value":false,"op":"replace","path":"/key3/sub2"}, |
||
| 60 | |||
| 61 | {"value":0,"op":"add","path":"/key3/sub3"}, |
||
| 62 | |||
| 63 | {"value":true,"op":"add","path":"/key4/0/subs/2/add"}, |
||
| 64 | |||
| 65 | {"op":"remove","path":"/key4/1/b"}, |
||
| 66 | |||
| 67 | {"value":false,"op":"add","path":"/key4/1/c"}, |
||
| 68 | |||
| 69 | {"value":1,"op":"add","path":"/key4/2/c"}, |
||
| 70 | |||
| 71 | {"value":"wat","op":"add","path":"/key5"} |
||
| 72 | ] |
||
| 73 | JSON; |
||
| 74 | |||
| 75 | |||
| 76 | $diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS); |
||
| 77 | $this->assertEquals(json_decode($patchJson), $diff->getPatch()->jsonSerialize()); |
||
| 78 | $this->assertSame(3, $diff->getModifiedCnt()); |
||
| 79 | |||
| 80 | $original = json_decode($originalJson); |
||
| 81 | $patch = JsonPatch::import(json_decode($patchJson)); |
||
| 82 | $patch->apply($original); |
||
| 83 | $this->assertEquals($diff->getRearranged(), $original); |
||
| 84 | } |
||
| 85 | } |