BaseStruct::__clone()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 0
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Struct;
9
10 View Code Duplication
abstract class BaseStruct
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    public function __construct(array $values = [])
13
    {
14
        foreach ($values as $name => $value) {
15
            $this->$name = $value;
16
        }
17
    }
18
19
    public function __get($name)
20
    {
21
        throw new \OutOfRangeException("Unknown property \${$name} in " . get_class($this) . '.');
22
    }
23
24
    public function __set($name, $value)
25
    {
26
        throw new \OutOfRangeException("Unknown property \${$name} in " . get_class($this) . '.');
27
    }
28
29
    public function __unset($name)
30
    {
31
        throw new \OutOfRangeException("Unknown property \${$name} in " . get_class($this) . '.');
32
    }
33
34
    public function __clone()
35
    {
36
        foreach ($this as $property => $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<ShopwarePlugins\Connect\Struct\BaseStruct> is not traversable.
Loading history...
37
            if (is_object($value)) {
38
                $this->$property = clone $value;
39
            }
40
41
            if (is_array($value)) {
42
                $this->cloneArray($this->$property);
43
            }
44
        }
45
    }
46
47
    /**
48
     * Clone array
49
     *
50
     * @param array $array
51
     */
52
    private function cloneArray(array &$array)
53
    {
54
        foreach ($array as $key => $value) {
55
            if (is_object($value)) {
56
                $array[$key] = clone $value;
57
            }
58
59
            if (is_array($value)) {
60
                $this->cloneArray($array[$key]);
61
            }
62
        }
63
    }
64
65
    /**
66
     * Restores struct from a previously stored state array.
67
     *
68
     * @param array $state
69
     * @return static
70
     */
71
    public static function __set_state(array $state)
72
    {
73
        return new static($state);
74
    }
75
}
76