GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 354c7c...907f89 )
by Jamie
05:42
created

JsonPatch::makeDiff()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
ccs 10
cts 10
cp 1
cc 4
eloc 9
nc 4
nop 3
crap 4
1
<?php
2
3
namespace OpenStack\Common\JsonSchema;
4
5
class JsonPatch
6
{
7
    const OP_ADD     = 'add';
8
    const OP_REPLACE = 'replace';
9
    const OP_REMOVE  = 'remove';
10
11 1
    public static function diff($src, $dest)
12
    {
13 1
        return (new static)->makeDiff($src, $dest);
14
    }
15
16 4
    public function makeDiff($srcStruct, $desStruct, $path = '')
17
    {
18 4
        $changes = [];
19
20 4
        if (is_object($srcStruct)) {
21 4
            $changes = $this->handleObject($srcStruct, $desStruct, $path);
22 4
        } elseif (is_array($srcStruct)) {
23 3
            $changes = $this->handleArray($srcStruct, $desStruct, $path);
24 3
        } elseif ($srcStruct != $desStruct) {
25 2
            $changes[] = $this->makePatch(self::OP_REPLACE, $path, $desStruct);
26 2
        }
27
28 4
        return $changes;
29
    }
30
31 1
    protected function handleArray($srcStruct, $desStruct, $path)
32
    {
33 1
        $changes = [];
34
35 1
        if ($diff = $this->arrayDiff($desStruct, $srcStruct)) {
36 1
            foreach ($diff as $key => $val) {
37 1
                if (is_object($val)) {
38 1
                    $changes = array_merge($changes, $this->makeDiff($srcStruct[$key], $val, $this->path($path, $key)));
39 1
                } else {
40 1
                    $op = array_key_exists($key, $srcStruct) && !in_array($srcStruct[$key], $desStruct, true)
41 1
                        ? self::OP_REPLACE : self::OP_ADD;
42 1
                    $changes[] = $this->makePatch($op, $this->path($path, $key), $val);
43
                }
44 1
            }
45 1
        } elseif ($srcStruct != $desStruct) {
46 1
            foreach ($srcStruct as $key => $val) {
47 1
                if (!in_array($val, $desStruct, true)) {
48 1
                    $changes[] = $this->makePatch(self::OP_REMOVE, $this->path($path, $key));
49 1
                }
50 1
            }
51 1
        }
52
53 1
        return $changes;
54
    }
55
56 1 View Code Duplication
    protected function handleObject($srcStruct, $desStruct, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 1
        $changes = [];
59
60 1
        if ($this->shouldPartiallyReplace($srcStruct, $desStruct)) {
61 1
            foreach ($desStruct as $key => $val) {
62 1
                if (!property_exists($srcStruct, $key)) {
63 1
                    $changes[] = $this->makePatch(self::OP_ADD, $this->path($path, $key), $val);
64 1
                } elseif ($srcStruct->$key != $val) {
65 1
                    $changes = array_merge($changes, $this->makeDiff($srcStruct->$key, $val, $this->path($path, $key)));
66 1
                }
67 1
            }
68 1
        } elseif ($this->shouldPartiallyReplace($desStruct, $srcStruct)) {
69 1
            foreach ($srcStruct as $key => $val) {
70 1
                if (!property_exists($desStruct, $key)) {
71 1
                    $changes[] = $this->makePatch(self::OP_REMOVE, $this->path($path, $key));
72 1
                }
73 1
            }
74 1
        }
75
76 1
        return $changes;
77
    }
78
79 4
    protected function shouldPartiallyReplace($o1, $o2)
80
    {
81 4
        return count(array_diff_key((array) $o1, (array) $o2)) < count($o1);
82
    }
83
84 3
    protected function arrayDiff(array $a1, array $a2)
85
    {
86 3
        $result = [];
87
88 3
        foreach($a1 as $key => $val) {
89 3
            if (!in_array($val, $a2, true)) {
90 2
                $result[$key] = $val;
91 2
            }
92 3
        }
93
94 3
        return $result;
95
    }
96
97 4
    protected function path($root, $path)
98
    {
99 4
        if ($path === '_empty_') {
100 1
            $path = '';
101 1
        }
102
103 4
        return rtrim($root, '/') . '/' . ltrim($path, '/');
104
    }
105
106 4
    protected function makePatch($op, $path, $val = null)
107
    {
108
        switch ($op) {
109 4
            default:
110 3
                return ['op' => $op, 'path' => $path, 'value' => $val];
111 4
            case self::OP_REMOVE:
112 3
                return ['op' => $op, 'path' => $path];
113 3
        }
114
115
    }
116
}