Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

ObjectSlicer   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 141
Duplicated Lines 5.67 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.77%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 0
dl 8
loc 141
ccs 68
cts 71
cp 0.9577
rs 9.3999
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A slice() 0 8 3
B sliceMulti() 0 23 6
C sliceRecursive() 3 31 13
B mergeSliceRecursive() 5 18 9
A normalizePaths() 0 13 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
2
/**
3
 * ObjectSlicer class file
4
 */
5
6
namespace Graviton\RestBundle\Utils;
7
8
/**
9
 * Object slicer like MongoDb "select" operator
10
 *
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class ObjectSlicer
16
{
17
    /**
18
     * Make slice
19
     *
20
     * @param object|array $value Value
21
     * @param string       $path  Path
22
     * @return object|array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \stdClass|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
23
     *
24
     * @throws \InvalidArgumentException
25
     */
26 42
    public function slice($value, $path)
27
    {
28 42
        if (!is_array($value) && !is_object($value)) {
29 2
            throw new \InvalidArgumentException('Value must be an array or an object');
30
        }
31
32 40
        return $this->sliceRecursive($value, explode('.', $path));
33
    }
34
35
    /**
36
     * Make and merge slices
37
     *
38
     * @param object|array $value Value
39
     * @param array        $paths Paths
40
     * @return object|array
41
     *
42
     * @throws \InvalidArgumentException
43
     */
44 38
    public function sliceMulti($value, array $paths)
45
    {
46 38
        if (!is_array($value) && !is_object($value)) {
47
            throw new \InvalidArgumentException('Value must be an array or an object');
48
        }
49 38
        if (empty($paths)) {
50
            throw new \InvalidArgumentException('Paths must contain at least one element');
51
        }
52
53 38
        $paths = array_unique($paths);
54 38
        $paths = $this->normalizePaths($paths);
55
56 38
        $parts = [];
57 38
        foreach ($paths as $path) {
58 38
            $parts[] = $this->sliceRecursive($value, explode('.', $path));
59 19
        }
60
61 38
        $result = array_shift($parts);
62 38
        foreach ($parts as $part) {
63 34
            $this->mergeSliceRecursive($result, $part);
64 19
        }
65 38
        return $result;
66
    }
67
68
    /**
69
     * Make slice recursive
70
     *
71
     * @param object|array $value Value
72
     * @param array        $keys  Keys
73
     * @return object|array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \stdClass|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
74
     */
75 78
    private function sliceRecursive($value, array $keys)
76
    {
77 78
        if (is_array($value) && !empty($value) && array_keys($value) !== range(0, count($value) - 1)) {
78 10
            $value = (object) $value;
79 5
        }
80
81 78
        if (is_object($value)) {
82 78
            $result = new \stdClass();
83
84 78
            $key = array_shift($keys);
85 78
            if (property_exists($value, $key)) {
86 78
                if (empty($keys)) {
87 68
                    $result->{$key} = $value->{$key};
88 70 View Code Duplication
                } elseif (is_object($value->{$key}) || is_array($value->{$key})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89 62
                    $result->{$key} = $this->sliceRecursive($value->{$key}, $keys);
90 31
                }
91 39
            }
92
93 78
            return $result;
94
        } else {
95 30
            $result = [];
96 30
            foreach ($value as $subvalue) {
97 30
                if (empty($keys)) {
98
                    $result[] = $subvalue;
99 30
                } elseif (is_object($subvalue) || is_array($subvalue)) {
100 30
                    $result[] = $this->sliceRecursive($subvalue, $keys);
101 15
                }
102 15
            }
103 30
            return $result;
104
        }
105
    }
106
107
    /**
108
     * Merge slices
109
     *
110
     * @param object|array $dst Dst
111
     * @param object|array $src Src
112
     * @return void
113
     */
114 34
    private function mergeSliceRecursive($dst, $src)
115
    {
116 34
        if (is_object($dst)) {
117 34
            foreach ((array) $src as $key => $value) {
118 30 View Code Duplication
                if (!property_exists($dst, $key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
119 28
                    $dst->{$key} = $value;
120 25
                } elseif (is_object($value) || is_array($value)) {
121 27
                    $this->mergeSliceRecursive($dst->{$key}, $value);
122 10
                }
123 17
            }
124 17
        } else {
125 14
            foreach ($src as $key => $value) {
126 14
                if (is_object($value) || is_array($value)) {
127 14
                    $this->mergeSliceRecursive($dst[$key], $value);
128 7
                }
129 7
            }
130
        }
131 34
    }
132
133
    /**
134
     * Normalize paths
135
     *
136
     * It removes duplicates:
137
     * ["a.b", "b.c.d", "c.d", "a", "b"] -> ["a", "b", "c.d"]
138
     *
139
     * @param array $paths Paths
140
     * @return array
141
     */
142 38
    private function normalizePaths(array $paths)
143
    {
144 38
        $result = $paths;
145 38
        foreach ($paths as $path) {
146 38
            $result = array_filter(
147 19
                $result,
148 38
                function ($item) use ($path) {
149 38
                    return strpos($item, $path.'.') !== 0;
150 19
                }
151 19
            );
152 19
        }
153 38
        return $result;
154
    }
155
}
156