Passed
Push — master ( 71dbb5...c2c480 )
by Josh
02:11
created

NonCompliantDecoder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decodeDictionary() 0 12 2
A complianceError() 0 2 1
A checkDictionaryCompliance() 0 5 3
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 string Whether current dictionary needs to be sorted
18
	*/
19
	protected bool $sortDictionary = false;
20
21 4
	protected function checkDictionaryCompliance(string $key, string $lastKey): void
22
	{
23 4
		if (!$this->sortDictionary && strcmp($key, $lastKey) < 0)
24
		{
25 2
			$this->sortDictionary = true;
0 ignored issues
show
Documentation Bug introduced by
The property $sortDictionary was declared of type string, but true is of type true. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
26
		}
27 4
	}
28
29 7
	protected function complianceError(string $message, int $offset): void
30
	{
31
		// Do nothing
32 7
	}
33
34 4
	protected function decodeDictionary(): ArrayObject
35
	{
36 4
		$previousState        = $this->sortDictionary;
0 ignored issues
show
Documentation Bug introduced by
The property $sortDictionary was declared of type string, but $this->sortDictionary is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
37 4
		$this->sortDictionary = false;
38 4
		$dictionary           = parent::decodeDictionary();
39 4
		if ($this->sortDictionary)
40
		{
41 2
			$dictionary->ksort(SORT_STRING);
42
		}
43 4
		$this->sortDictionary = $previousState;
44
45 4
		return $dictionary;
46
	}
47
}