Total Complexity | 51 |
Total Lines | 294 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
Complex classes like Bencode 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 Bencode, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
15 | class Bencode |
||
16 | { |
||
17 | /** |
||
18 | * @var string Bencoded string being decoded |
||
19 | */ |
||
20 | protected string $bencoded; |
||
21 | |||
22 | /** |
||
23 | * @var int Length of the bencoded string |
||
24 | */ |
||
25 | protected int $len; |
||
26 | |||
27 | /** |
||
28 | * @var int Safe rightmost boundary |
||
29 | */ |
||
30 | protected int $max; |
||
31 | |||
32 | /** |
||
33 | * @var int Position of the cursor while decoding |
||
34 | */ |
||
35 | protected int $pos; |
||
36 | |||
37 | protected function __construct(string $bencoded) |
||
38 | { |
||
39 | if ($bencoded === '') |
||
40 | { |
||
41 | throw new InvalidArgumentException; |
||
42 | } |
||
43 | |||
44 | $this->bencoded = $bencoded; |
||
45 | $this->len = strlen($bencoded); |
||
46 | $this->pos = 0; |
||
47 | |||
48 | $this->computeSafeBoundary(); |
||
49 | $this->checkBoundary(); |
||
50 | } |
||
51 | |||
52 | public static function decode(string $bencoded) |
||
60 | } |
||
61 | |||
62 | protected function checkBoundary(): void |
||
63 | { |
||
64 | if ($this->max < 1) |
||
65 | { |
||
66 | if (strpos('-e', $this->bencoded[0]) === false) |
||
67 | { |
||
68 | throw new RuntimeException('Premature end of data'); |
||
69 | } |
||
70 | else |
||
71 | { |
||
72 | throw new RuntimeException('Illegal character found at offset 0'); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | protected function checkCursorPosition(): void |
||
78 | { |
||
79 | if ($this->pos !== $this->len) |
||
80 | { |
||
81 | if ($this->pos < $this->len) |
||
82 | { |
||
83 | throw new RuntimeException('Superfluous content found at offset ' . $this->pos); |
||
84 | } |
||
85 | |||
86 | throw new RuntimeException('Premature end of data'); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Adjust the rightmost boundary to the last safe character |
||
92 | * |
||
93 | * Will rewind the boundary to skip the rightmost digits, optionally preceded by "i" or "i-" |
||
94 | */ |
||
95 | protected function computeSafeBoundary(): void |
||
96 | { |
||
97 | $boundary = $this->len - 1; |
||
98 | $c = $this->bencoded[$boundary]; |
||
99 | while (is_numeric($c) && --$boundary >= 0) |
||
100 | { |
||
101 | $c = $this->bencoded[$boundary]; |
||
102 | } |
||
103 | if ($c === '-' && $boundary > 0 && $this->bencoded[$boundary - 1] === 'i') |
||
104 | { |
||
105 | $boundary -= 2; |
||
106 | } |
||
107 | elseif ($c === 'i') |
||
108 | { |
||
109 | --$boundary; |
||
110 | } |
||
111 | |||
112 | $this->max = $boundary; |
||
113 | } |
||
114 | |||
115 | protected function decodeAnything() |
||
116 | { |
||
117 | $c = $this->bencoded[$this->pos]; |
||
118 | if ($c === 'i') |
||
119 | { |
||
120 | return $this->decodeInteger(); |
||
121 | } |
||
122 | if ($c === 'd') |
||
123 | { |
||
124 | return $this->decodeDictionary(); |
||
125 | } |
||
126 | if ($c === 'l') |
||
127 | { |
||
128 | return $this->decodeList(); |
||
129 | } |
||
130 | |||
131 | return $this->decodeString(); |
||
132 | } |
||
133 | |||
134 | protected function decodeDictionary(): ArrayObject |
||
160 | } |
||
161 | |||
162 | protected function decodeDigits(string $terminator): int |
||
163 | { |
||
164 | $spn = strspn($this->bencoded, '1234567890', $this->pos); |
||
165 | if ($spn > 1) |
||
166 | { |
||
167 | if ($this->bencoded[$this->pos] === '0') |
||
168 | { |
||
169 | throw new RuntimeException('Illegal character found at offset ' . $this->pos); |
||
170 | } |
||
171 | } |
||
172 | elseif (!$spn) |
||
173 | { |
||
174 | throw new RuntimeException('Illegal character found at offset ' . $this->pos); |
||
175 | } |
||
176 | |||
177 | // Capture the value and cast it as an integer |
||
178 | $value = (int) substr($this->bencoded, $this->pos, $spn); |
||
179 | |||
180 | $this->pos += $spn; |
||
181 | if ($this->bencoded[$this->pos] !== $terminator) |
||
182 | { |
||
183 | throw new RuntimeException('Illegal character found at offset ' . $this->pos); |
||
184 | } |
||
185 | ++$this->pos; |
||
186 | |||
187 | return $value; |
||
188 | } |
||
189 | |||
190 | protected function decodeInteger(): int |
||
191 | { |
||
192 | $negative = ($this->bencoded[++$this->pos] === '-'); |
||
193 | if ($negative && $this->bencoded[++$this->pos] === '0') |
||
194 | { |
||
195 | throw new RuntimeException('Illegal character found at offset ' . $this->pos); |
||
196 | } |
||
197 | |||
198 | $value = $this->decodeDigits('e'); |
||
199 | if ($negative) |
||
200 | { |
||
201 | $value = -$value; |
||
202 | } |
||
203 | |||
204 | return $value; |
||
205 | } |
||
206 | |||
207 | protected function decodeList(): array |
||
208 | { |
||
209 | ++$this->pos; |
||
210 | |||
211 | $list = []; |
||
212 | while ($this->pos <= $this->max) |
||
213 | { |
||
214 | if ($this->bencoded[$this->pos] === 'e') |
||
215 | { |
||
216 | ++$this->pos; |
||
217 | |||
218 | return $list; |
||
219 | } |
||
220 | |||
221 | $list[] = $this->decodeAnything(); |
||
222 | } |
||
223 | |||
224 | throw new RuntimeException('Premature end of data'); |
||
225 | } |
||
226 | |||
227 | protected function decodeString(): string |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Bencode a value |
||
238 | */ |
||
239 | public static function encode($value): string |
||
240 | { |
||
241 | if (is_scalar($value)) |
||
242 | { |
||
243 | return self::encodeScalar($value); |
||
244 | } |
||
245 | if (is_array($value)) |
||
246 | { |
||
247 | return self::encodeArray($value); |
||
248 | } |
||
249 | if ($value instanceof stdClass) |
||
250 | { |
||
251 | $value = new ArrayObject(get_object_vars($value)); |
||
252 | } |
||
253 | if ($value instanceof ArrayObject) |
||
254 | { |
||
255 | return self::encodeArrayObject($value); |
||
256 | } |
||
257 | |||
258 | throw new InvalidArgumentException('Unsupported value'); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Encode an array into either an array of a dictionary |
||
263 | */ |
||
264 | protected static function encodeArray(array $value): string |
||
265 | { |
||
266 | if (empty($value)) |
||
267 | { |
||
268 | return 'le'; |
||
269 | } |
||
270 | |||
271 | if (array_keys($value) === range(0, count($value) - 1)) |
||
272 | { |
||
273 | return 'l' . implode('', array_map([__CLASS__, 'encode'], $value)) . 'e'; |
||
274 | } |
||
275 | |||
276 | // Encode associative arrays as dictionaries |
||
277 | return self::encodeArrayObject(new ArrayObject($value)); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Encode given ArrayObject instance into a dictionary |
||
282 | */ |
||
283 | protected static function encodeArrayObject(ArrayObject $dict): string |
||
284 | { |
||
285 | $vars = $dict->getArrayCopy(); |
||
286 | ksort($vars); |
||
287 | |||
288 | $str = 'd'; |
||
289 | foreach ($vars as $k => $v) |
||
290 | { |
||
291 | $str .= strlen($k) . ':' . $k . self::encode($v); |
||
292 | } |
||
293 | $str .= 'e'; |
||
294 | |||
295 | return $str; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Encode a scalar value |
||
300 | */ |
||
301 | protected static function encodeScalar($value): string |
||
309 | } |
||
310 | } |