BaseStruct   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 66
loc 66
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 2
A __get() 4 4 1
A __set() 4 4 1
A __unset() 4 4 1
A __clone() 12 12 4
A cloneArray() 12 12 4
A __set_state() 4 4 1

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
 * (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