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.

JsonPatch   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 113
Duplicated Lines 19.47 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 0
dl 22
loc 113
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A diff() 0 4 1
A makeDiff() 0 14 4
C handleArray() 0 24 9
C handleObject() 22 22 8
A shouldPartiallyReplace() 0 4 1
A arrayDiff() 0 12 3
A path() 0 10 2
A makePatch() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
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, string $path = ''): array
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(array $srcStruct, array $desStruct, string $path): array
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(\stdClass $srcStruct, \stdClass $desStruct, string $path): array
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) {
0 ignored issues
show
Bug introduced by
The expression $desStruct of type object<stdClass> is not traversable.
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The expression $srcStruct of type object<stdClass> is not traversable.
Loading history...
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(\stdClass $o1, \stdClass $o2): bool
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): array
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(string $root, $path): string
98
    {
99 4
        $path = (string) $path;
100 1
101 1
        if ($path === '_empty_') {
102
            $path = '';
103 4
        }
104
105
        return rtrim($root, '/') . '/' . ltrim($path, '/');
106 4
    }
107
108
    protected function makePatch(string $op, string $path, $val = null): array
109 4
    {
110 3
        switch ($op) {
111 4
            default:
112 3
                return ['op' => $op, 'path' => $path, 'value' => $val];
113 3
            case self::OP_REMOVE:
114
                return ['op' => $op, 'path' => $path];
115
        }
116
    }
117
}
118