Passed
Push — master ( 94dc81...d2c9a0 )
by Josh
10:43
created

NonCompliantDecoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decodeDictionary() 0 12 2
A complianceError() 0 2 1
A dictionaryComplianceError() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\Bencode
5
* @copyright Copyright (c) 2014-2021 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, false, true;
12
use function strcmp;
13
14
class NonCompliantDecoder extends Decoder
15
{
16
	/**
17
	* @var bool Whether current dictionary needs to be sorted
18
	*/
19
	protected bool $sortDictionary = false;
20
21 7
	protected function complianceError(string $message, int $offset): void
22
	{
23
		// Do nothing
24 7
	}
25
26 4
	protected function decodeDictionary(): ArrayObject
27
	{
28 4
		$previousState        = $this->sortDictionary;
29 4
		$this->sortDictionary = false;
30 4
		$dictionary           = parent::decodeDictionary();
31 4
		if ($this->sortDictionary)
32
		{
33 4
			$dictionary->ksort(SORT_STRING);
34
		}
35 4
		$this->sortDictionary = $previousState;
36
37 4
		return $dictionary;
38
	}
39
40 4
	protected function dictionaryComplianceError(string $key, string $lastKey): void
41
	{
42 4
		$this->sortDictionary = true;
43
	}
44
}