@@ -15,11 +15,11 @@ discard block |
||
| 15 | 15 | class Bencode |
| 16 | 16 | { |
| 17 | 17 | /** |
| 18 | - * Decode a bencoded string |
|
| 19 | - * |
|
| 20 | - * @param string $bencoded Bencoded string |
|
| 21 | - * @return mixed Decoded value |
|
| 22 | - */ |
|
| 18 | + * Decode a bencoded string |
|
| 19 | + * |
|
| 20 | + * @param string $bencoded Bencoded string |
|
| 21 | + * @return mixed Decoded value |
|
| 22 | + */ |
|
| 23 | 23 | public static function decode(string $bencoded) |
| 24 | 24 | { |
| 25 | 25 | if ($bencoded === '') |
@@ -221,8 +221,8 @@ discard block |
||
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | - * Bencode a value |
|
| 225 | - */ |
|
| 224 | + * Bencode a value |
|
| 225 | + */ |
|
| 226 | 226 | public static function encode($value): string |
| 227 | 227 | { |
| 228 | 228 | if (is_scalar($value)) |
@@ -246,8 +246,8 @@ discard block |
||
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** |
| 249 | - * Encode an array into either an array of a dictionary |
|
| 250 | - */ |
|
| 249 | + * Encode an array into either an array of a dictionary |
|
| 250 | + */ |
|
| 251 | 251 | protected static function encodeArray(array $value): string |
| 252 | 252 | { |
| 253 | 253 | if (empty($value)) |
@@ -265,8 +265,8 @@ discard block |
||
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
| 268 | - * Encode given ArrayObject instance into a dictionary |
|
| 269 | - */ |
|
| 268 | + * Encode given ArrayObject instance into a dictionary |
|
| 269 | + */ |
|
| 270 | 270 | protected static function encodeArrayObject(ArrayObject $dict): string |
| 271 | 271 | { |
| 272 | 272 | $vars = $dict->getArrayCopy(); |
@@ -283,8 +283,8 @@ discard block |
||
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /** |
| 286 | - * Encode a scalar value |
|
| 287 | - */ |
|
| 286 | + * Encode a scalar value |
|
| 287 | + */ |
|
| 288 | 288 | protected static function encodeScalar($value): string |
| 289 | 289 | { |
| 290 | 290 | if (is_int($value) || is_float($value) || is_bool($value)) |
@@ -296,10 +296,10 @@ discard block |
||
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /** |
| 299 | - * Adjust the rightmost boundary to the last safe character |
|
| 300 | - * |
|
| 301 | - * Will rewind the boundary to skip the rightmost digits, optionally preceded by "i" or "i-" |
|
| 302 | - */ |
|
| 299 | + * Adjust the rightmost boundary to the last safe character |
|
| 300 | + * |
|
| 301 | + * Will rewind the boundary to skip the rightmost digits, optionally preceded by "i" or "i-" |
|
| 302 | + */ |
|
| 303 | 303 | protected static function getSafeBoundary(string $bencoded, int $boundary): int |
| 304 | 304 | { |
| 305 | 305 | $c = $bencoded[$boundary]; |
@@ -58,8 +58,7 @@ discard block |
||
| 58 | 58 | { |
| 59 | 59 | throw new RuntimeException('Illegal character found at offset ' . $pos); |
| 60 | 60 | } |
| 61 | - } |
|
| 62 | - elseif (!$spn) |
|
| 61 | + } elseif (!$spn) |
|
| 63 | 62 | { |
| 64 | 63 | $pos -= ($negative) ? 2 : 1; |
| 65 | 64 | |
@@ -83,8 +82,7 @@ discard block |
||
| 83 | 82 | } |
| 84 | 83 | |
| 85 | 84 | ++$pos; |
| 86 | - } |
|
| 87 | - elseif ($c === 'e') |
|
| 85 | + } elseif ($c === 'e') |
|
| 88 | 86 | { |
| 89 | 87 | if (isset($currentKey)) |
| 90 | 88 | { |
@@ -106,18 +104,15 @@ discard block |
||
| 106 | 104 | $currentType = $types[$depth - 1]; |
| 107 | 105 | |
| 108 | 106 | continue; |
| 109 | - } |
|
| 110 | - elseif ($c === 'd') |
|
| 107 | + } elseif ($c === 'd') |
|
| 111 | 108 | { |
| 112 | 109 | ++$pos; |
| 113 | 110 | $value = clone $dictionary; |
| 114 | - } |
|
| 115 | - elseif ($c === 'l') |
|
| 111 | + } elseif ($c === 'l') |
|
| 116 | 112 | { |
| 117 | 113 | ++$pos; |
| 118 | 114 | $value = []; |
| 119 | - } |
|
| 120 | - else |
|
| 115 | + } else |
|
| 121 | 116 | { |
| 122 | 117 | $spn = strspn($bencoded, '1234567890', $pos); |
| 123 | 118 | if ($spn > 1) |
@@ -126,8 +121,7 @@ discard block |
||
| 126 | 121 | { |
| 127 | 122 | throw new RuntimeException('Illegal character found at offset ' . $pos); |
| 128 | 123 | } |
| 129 | - } |
|
| 130 | - elseif (!$spn) |
|
| 124 | + } elseif (!$spn) |
|
| 131 | 125 | { |
| 132 | 126 | throw new RuntimeException('Illegal character found at offset ' . $pos); |
| 133 | 127 | } |
@@ -153,8 +147,7 @@ discard block |
||
| 153 | 147 | } |
| 154 | 148 | $current->$currentKey = &$value; |
| 155 | 149 | $currentKey = null; |
| 156 | - } |
|
| 157 | - elseif ($currentType === 'd') |
|
| 150 | + } elseif ($currentType === 'd') |
|
| 158 | 151 | { |
| 159 | 152 | if (!is_string($value)) |
| 160 | 153 | { |
@@ -162,16 +155,13 @@ discard block |
||
| 162 | 155 | } |
| 163 | 156 | |
| 164 | 157 | $currentKey = $value; |
| 165 | - } |
|
| 166 | - elseif ($currentType === 'l') |
|
| 158 | + } elseif ($currentType === 'l') |
|
| 167 | 159 | { |
| 168 | 160 | $current[] = &$value; |
| 169 | - } |
|
| 170 | - elseif (isset($current)) |
|
| 161 | + } elseif (isset($current)) |
|
| 171 | 162 | { |
| 172 | 163 | throw new RuntimeException('Superfluous content found at offset ' . strlen(static::encode($current))); |
| 173 | - } |
|
| 174 | - else |
|
| 164 | + } else |
|
| 175 | 165 | { |
| 176 | 166 | $current = &$value; |
| 177 | 167 | } |
@@ -310,8 +300,7 @@ discard block |
||
| 310 | 300 | if ($c === '-' && $boundary > 0 && $bencoded[$boundary - 1] === 'i') |
| 311 | 301 | { |
| 312 | 302 | $boundary -= 2; |
| 313 | - } |
|
| 314 | - elseif ($c === 'i') |
|
| 303 | + } elseif ($c === 'i') |
|
| 315 | 304 | { |
| 316 | 305 | --$boundary; |
| 317 | 306 | } |