Total Complexity | 43 |
Total Lines | 261 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CborDecoder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CborDecoder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class CborDecoder |
||
43 | { |
||
44 | public const CBOR_MAJOR_UNSIGNED_INT = 0; |
||
45 | public const CBOR_MAJOR_NEGATIVE_INT = 1; |
||
46 | public const CBOR_MAJOR_BYTE_STRING = 2; |
||
47 | public const CBOR_MAJOR_TEXT_STRING = 3; |
||
48 | public const CBOR_MAJOR_ARRAY = 4; |
||
49 | public const CBOR_MAJOR_MAP = 5; |
||
50 | public const CBOR_MAJOR_TAG = 6; |
||
51 | public const CBOR_MAJOR_FLOAT_SIMPLE = 7; |
||
52 | |||
53 | /** |
||
54 | * Decode the given data |
||
55 | * @param ByteBuffer|string $data |
||
56 | * @return mixed |
||
57 | */ |
||
58 | public static function decode($data) |
||
59 | { |
||
60 | if (is_string($data)) { |
||
61 | $data = new ByteBuffer($data); |
||
62 | } |
||
63 | |||
64 | $offset = 0; |
||
65 | $result = self::parseItem($data, $offset); |
||
66 | if ($offset !== $data->getLength()) { |
||
67 | throw new WebauthnException(sprintf( |
||
68 | 'There still unsed bytes [%d] after parse data', |
||
69 | abs($offset - $data->getLength()) |
||
70 | )); |
||
71 | } |
||
72 | |||
73 | return $result; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Decode the data using custom start and end offset |
||
78 | * @param ByteBuffer|string $data |
||
79 | * @param int $startoffset |
||
80 | * @param int|null $endOffset |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public static function decodeInPlace($data, int $startoffset, ?int $endOffset = null) |
||
|
|||
84 | { |
||
85 | if (is_string($data)) { |
||
86 | $data = new ByteBuffer($data); |
||
87 | } |
||
88 | |||
89 | $offset = $startoffset; |
||
90 | $result = self::parseItem($data, $offset); |
||
91 | $endOffset = $offset; |
||
92 | |||
93 | return $result; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Parse the item in the given offset |
||
98 | * @param ByteBuffer $buffer |
||
99 | * @param int $offset |
||
100 | * @return mixed |
||
101 | */ |
||
102 | protected static function parseItem(ByteBuffer $buffer, int &$offset) |
||
103 | { |
||
104 | $first = $buffer->getByteValue($offset++); |
||
105 | $type = $first >> 5; |
||
106 | $value = $first & 0b11111; |
||
107 | if ($type === self::CBOR_MAJOR_FLOAT_SIMPLE) { |
||
108 | return self::parseSimpleFloat($value, $buffer, $offset); |
||
109 | } |
||
110 | |||
111 | $val = self::extractLength($value, $buffer, $offset); |
||
112 | |||
113 | return self::parseItemData($type, $val, $buffer, $offset); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Parse the simple float value |
||
118 | * @param int $value |
||
119 | * @param ByteBuffer $buffer |
||
120 | * @param int $offset |
||
121 | * @return mixed |
||
122 | */ |
||
123 | protected static function parseSimpleFloat(int $value, ByteBuffer $buffer, int &$offset) |
||
124 | { |
||
125 | switch ($value) { |
||
126 | case 24: |
||
127 | $value = $buffer->getByteValue($offset); |
||
128 | $offset++; |
||
129 | return self::parseSimpleValue($value); |
||
130 | |||
131 | case 25: |
||
132 | $floatValue = $buffer->getHalfFloatValue($offset); |
||
133 | $offset += 2; |
||
134 | |||
135 | return $floatValue; |
||
136 | |||
137 | case 26: |
||
138 | $floatValue = $buffer->getFloatValue($offset); |
||
139 | $offset += 4; |
||
140 | |||
141 | return $floatValue; |
||
142 | |||
143 | case 27: |
||
144 | $floatValue = $buffer->getDoubleValue($offset); |
||
145 | $offset += 8; |
||
146 | |||
147 | return $floatValue; |
||
148 | |||
149 | case 28: |
||
150 | case 29: |
||
151 | case 30: |
||
152 | throw new WebauthnException(sprintf('Reserved value [%d] used', $value)); |
||
153 | |||
154 | case 31: |
||
155 | throw new WebauthnException(sprintf('Indefinite value [%d] length is not supported', $value)); |
||
156 | } |
||
157 | |||
158 | return self::parseSimpleValue($value); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Parse simple value |
||
163 | * @param int $value |
||
164 | * @return bool|null |
||
165 | */ |
||
166 | protected static function parseSimpleValue(int $value): ?bool |
||
167 | { |
||
168 | if ($value === 20) { |
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | if ($value === 21) { |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | if ($value === 23) { |
||
177 | return null; |
||
178 | } |
||
179 | |||
180 | throw new WebauthnException(sprintf('Unsupported simple value [%d]', $value)); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Parse the item data |
||
185 | * @param int $type |
||
186 | * @param int $value |
||
187 | * @param ByteBuffer $buffer |
||
188 | * @param int $offset |
||
189 | * @return mixed |
||
190 | */ |
||
191 | protected static function parseItemData(int $type, int $value, ByteBuffer $buffer, int &$offset) |
||
192 | { |
||
193 | switch ($type) { |
||
194 | case self::CBOR_MAJOR_UNSIGNED_INT: |
||
195 | return $value; |
||
196 | |||
197 | case self::CBOR_MAJOR_NEGATIVE_INT: |
||
198 | return -1 - $value; |
||
199 | |||
200 | case self::CBOR_MAJOR_BYTE_STRING: |
||
201 | $data = $buffer->getBytes($offset, $value); |
||
202 | $offset += $value; |
||
203 | return new ByteBuffer($data); // bytes |
||
204 | |||
205 | case self::CBOR_MAJOR_TEXT_STRING: |
||
206 | $data = $buffer->getBytes($offset, $value); |
||
207 | $offset += $value; |
||
208 | return $data; // UTF-8 |
||
209 | |||
210 | case self::CBOR_MAJOR_ARRAY: |
||
211 | return self::parseArray($buffer, $offset, $value); |
||
212 | |||
213 | case self::CBOR_MAJOR_MAP: |
||
214 | return self::parseMap($buffer, $offset, $value); |
||
215 | |||
216 | case self::CBOR_MAJOR_TAG: |
||
217 | return self::parseItem($buffer, $offset); // 1 embedded data item |
||
218 | } |
||
219 | |||
220 | throw new WebauthnException(sprintf('Unsupported major type [%d]', $type)); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Parse an array of values |
||
225 | * @param ByteBuffer $buffer |
||
226 | * @param int $offset |
||
227 | * @param int $count |
||
228 | * @return array<mixed> |
||
229 | */ |
||
230 | protected static function parseArray(ByteBuffer $buffer, int &$offset, int $count): array |
||
231 | { |
||
232 | $arr = []; |
||
233 | for ($i = 0; $i < $count; $i++) { |
||
234 | $arr[] = self::parseItem($buffer, $offset); |
||
235 | } |
||
236 | |||
237 | return $arr; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Parse map of values |
||
242 | * @param ByteBuffer $buffer |
||
243 | * @param int $offset |
||
244 | * @param int $count |
||
245 | * @return array<string|int, mixed> |
||
246 | */ |
||
247 | protected static function parseMap(ByteBuffer $buffer, int &$offset, int $count): array |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * |
||
265 | * @param int $value |
||
266 | * @param ByteBuffer $buffer |
||
267 | * @param int $offset |
||
268 | * @return int |
||
269 | */ |
||
270 | protected static function extractLength(int $value, ByteBuffer $buffer, int &$offset): int |
||
303 | } |
||
304 | } |
||
305 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.