StructMetadata   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 196
Duplicated Lines 4.59 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 9
loc 196
ccs 79
cts 79
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A hasProperty() 0 4 1
A getProperty() 0 7 2
A addProperty() 0 6 1
A removeProperty() 0 6 1
A clearProperties() 0 6 1
A serialize() 9 9 1
A getProperties() 0 4 1
A setProperties() 0 8 2
B unserialize() 0 19 6
A toArray() 0 15 2
A getHash() 0 16 3

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
namespace Flying\Struct\Metadata;
4
5
use Flying\Struct\Exception;
6
7
/**
8
 * Structure metadata storage class
9
 */
10
class StructMetadata extends PropertyMetadata
11
{
12
    /**
13
     * Structure properties
14
     *
15
     * @var array
16
     */
17
    private $properties = [];
18
19
    /**
20
     * Class constructor
21
     *
22
     * @param string $name      OPTIONAL Property name
23
     * @param string $class     OPTIONAL Class name for property object
24
     * @param array $config     OPTIONAL Configuration options for property object
25
     * @param array $properties OPTIONAL Structure properties
26
     * @throws \InvalidArgumentException
27
     */
28 199
    public function __construct($name = null, $class = null, $config = null, $properties = null)
29
    {
30 199
        parent::__construct($name, $class, $config);
31 199
        if ($properties !== null) {
32 1
            $this->setProperties($properties);
33 1
        }
34 199
    }
35
36
    /**
37
     * Check if structure property with given name is available
38
     *
39
     * @param string $name Property name
40
     * @return boolean
41
     */
42 2
    public function hasProperty($name)
43
    {
44 2
        return array_key_exists($name, $this->properties);
45
    }
46
47
    /**
48
     * Get metadata for structure property with given name
49
     *
50
     * @param string $name Property name
51
     * @throws Exception
52
     * @return MetadataInterface
53
     */
54 40
    public function getProperty($name)
55
    {
56 40
        if (!array_key_exists($name, $this->properties)) {
57 1
            throw new Exception('No metadata is available for property: ' . $name);
58
        }
59 39
        return $this->properties[$name];
60
    }
61
62
    /**
63
     * Add given metadata as structure property
64
     *
65
     * @param MetadataInterface $metadata Property metadata
66
     * @return $this
67
     */
68 185
    public function addProperty(MetadataInterface $metadata)
69
    {
70 185
        $this->properties[$metadata->getName()] = $metadata;
71 185
        $this->hash = null;
72 185
        return $this;
73
    }
74
75
    /**
76
     * Remove structure property with given name
77
     *
78
     * @param string $name Property name
79
     * @return $this
80
     */
81 39
    public function removeProperty($name)
82
    {
83 39
        unset($this->properties[$name]);
84 39
        $this->hash = null;
85 39
        return $this;
86
    }
87
88
    /**
89
     * Clear all registered structure properties
90
     *
91
     * @return $this
92
     */
93 20
    public function clearProperties()
94
    {
95 20
        $this->properties = [];
96 20
        $this->hash = null;
97 20
        return $this;
98
    }
99
100
    /**
101
     * Defined by Serializable interface
102
     *
103
     * @return string
104
     */
105 17 View Code Duplication
    public function serialize()
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...
106
    {
107 17
        return serialize([
108 17
            'name'       => $this->getName(),
109 17
            'class'      => $this->getClass(),
110 17
            'config'     => $this->getConfig(),
111 17
            'properties' => $this->getProperties(),
112 17
        ]);
113
    }
114
115
    /**
116
     * Get metadata for all registered structure properties
117
     *
118
     * @return array
119
     */
120 186
    public function getProperties()
121
    {
122 186
        return $this->properties;
123
    }
124
125
    /**
126
     * Set structure properties metadata
127
     *
128
     * @param array $properties
129
     * @return $this
130
     */
131 19
    public function setProperties(array $properties)
132
    {
133 19
        $this->clearProperties();
134 19
        foreach ($properties as $metadata) {
135 17
            $this->addProperty($metadata);
136 19
        }
137 19
        return $this;
138
    }
139
140
    /**
141
     * Defined by Serializable interface
142
     *
143
     * @param string $serialized
144
     * @return void
145
     * @throws \InvalidArgumentException
146
     */
147 18
    public function unserialize($serialized)
148
    {
149 18
        $array = unserialize($serialized);
150 18
        if (!is_array($array)) {
151 1
            return;
152
        }
153 17
        if (array_key_exists('name', $array)) {
154 17
            $this->setName($array['name']);
155 17
        }
156 17
        if (array_key_exists('class', $array)) {
157 17
            $this->setClass($array['class']);
158 17
        }
159 17
        if (array_key_exists('config', $array)) {
160 17
            $this->setConfig($array['config']);
161 17
        }
162 17
        if (array_key_exists('properties', $array)) {
163 17
            $this->setProperties($array['properties']);
164 17
        }
165 17
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 14
    public function toArray()
171
    {
172
        $array = [
173 14
            'name'       => $this->getName(),
174 14
            'class'      => $this->getClass(),
175 14
            'config'     => $this->getConfig(),
176 14
            'hash'       => $this->getHash(),
177 14
            'properties' => [],
178 14
        ];
179
        /** @var $property MetadataInterface */
180 14
        foreach ($this->getProperties() as $name => $property) {
181 13
            $array['properties'][$name] = $property->toArray();
182 14
        }
183 14
        return $array;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189 94
    public function getHash()
190
    {
191 94
        if (!$this->hash) {
192
            $hash = [
193 94
                $this->getName(),
194 94
                $this->getClass(),
195 94
                serialize($this->getConfig()),
196 94
            ];
197
            /** @var $property PropertyMetadata */
198 94
            foreach ($this->getProperties() as $property) {
199 92
                $hash[] = $property->getHash();
200 94
            }
201 94
            $this->hash = sha1(implode('|', $hash));
202 94
        }
203 94
        return $this->hash;
204
    }
205
}
206