JsonMergePatch::apply()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 8.8333
cc 7
nc 8
nop 2
crap 7
1
<?php
2
3
namespace Swaggest\JsonDiff;
4
5
6
class JsonMergePatch
7
{
8 21
    public static function apply(&$original, $patch)
9
    {
10 21
        if (null === $patch) {
11 1
            $original = null;
12 20
        } elseif (is_object($patch)) {
13 15
            foreach (get_object_vars($patch) as $key => $val) {
14 14
                if ($val === null) {
15 7
                    unset($original->$key);
16
                } else {
17 12
                    if (!is_object($original)) {
18 2
                        $original = new \stdClass();
19
                    }
20 12
                    $branch = &$original->$key;
21 12
                    if (null === $branch) {
22 6
                        $branch = new \stdClass();
23
                    }
24 12
                    self::apply($branch, $val);
25
                }
26
            }
27
        } else {
28 15
            $original = $patch;
29
        }
30
    }
31
}