This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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.