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); |
||
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) |
|
15 | |||
16 | 4 | public function makeDiff($srcStruct, $desStruct, string $path = ''): array |
|
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 |
|
|||
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(\stdClass $o1, \stdClass $o2): bool |
|
83 | |||
84 | 3 | protected function arrayDiff(array $a1, array $a2): array |
|
85 | { |
||
96 | |||
97 | 4 | protected function path(string $root, $path): string |
|
107 | |||
108 | protected function makePatch(string $op, string $path, $val = null): array |
||
117 | } |
||
118 |
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.