Passed
Branch master (d8bc18)
by Josh
12:04
created

Encoder::arrayIsList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\Bencode
5
* @copyright Copyright (c) 2014-2022 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\Bencode;
9
10
use ArrayObject;
11
use const SORT_STRING;
12
use function array_is_list, get_object_vars, is_array, is_bool, is_float, is_int, is_object, is_string, ksort, strlen;
1 ignored issue
show
introduced by
The function array_is_list was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
use s9e\Bencode\Exceptions\EncodingException;
14
use stdClass;
15
16
class Encoder
17
{
18 20
	public static function encode($value): string
19
	{
20 20
		if (is_string($value))
21
		{
22 2
			return strlen($value) . ':' . $value;
23
		}
24 20
		if (is_array($value))
25
		{
26 7
			return self::encodeArray($value);
27
		}
28 17
		if (is_int($value))
29
		{
30 12
			return "i{$value}e";
31
		}
32 10
		if (is_object($value))
33
		{
34 5
			return self::encodeObject($value);
35
		}
36
37 5
		return self::encode(self::coerceUnsupportedValue($value));
38
	}
39
40 2
	protected static function coerceBool(bool $value): int
41
	{
42 2
		return (int) $value;
43
	}
44
45 2
	protected static function coerceFloat(float $value): int
46
	{
47 2
		$int = (int) $value;
48 2
		if ((float) $int === $value)
49
		{
50 1
			return $int;
51
		}
52
53 1
		throw new EncodingException('Unsupported value', $value);
54
	}
55
56 5
	protected static function coerceUnsupportedValue($value): array|int|string
57
	{
58 5
		if (is_float($value))
59
		{
60 2
			return self::coerceFloat($value);
61
		}
62 3
		if (is_bool($value))
63
		{
64 2
			return self::coerceBool($value);
65
		}
66
67 1
		throw new EncodingException('Unsupported value', $value);
68
	}
69
70
	/**
71
	* Encode a PHP array into either a list of a dictionary
72
	*/
73 7
	protected static function encodeArray(array $value): string
74
	{
75 7
		return array_is_list($value)
1 ignored issue
show
Bug introduced by
The function array_is_list was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
		return /** @scrutinizer ignore-call */ array_is_list($value)
Loading history...
76 3
			? self::encodeIndexedArray($value)
77 7
			: self::encodeAssociativeArray($value);
78
	}
79
80 10
	protected static function encodeAssociativeArray(array $array): string
81
	{
82 10
		ksort($array, SORT_STRING);
83
84 10
		$str = 'd';
85 10
		foreach ($array as $k => $v)
86
		{
87 8
			$str .= strlen((string) $k) . ':' . $k . self::encode($v);
88
		}
89 10
		$str .= 'e';
90
91 10
		return $str;
92
	}
93
94 3
	protected static function encodeIndexedArray(array $array): string
95
	{
96 3
		$str = 'l';
97 3
		foreach ($array as $v)
98
		{
99 2
			$str .= self::encode($v);
100
		}
101 3
		$str .= 'e';
102
103 3
		return $str;
104
	}
105
106 5
	protected static function encodeObject(object $value): string
107
	{
108 5
		if ($value instanceof ArrayObject)
109
		{
110 1
			return self::encodeAssociativeArray($value->getArrayCopy());
111
		}
112 4
		if ($value instanceof stdClass)
113
		{
114 3
			return self::encodeAssociativeArray(get_object_vars($value));
115
		}
116
117 1
		throw new EncodingException('Unsupported value', $value);
118
	}
119
}