Passed
Push — master ( d00047...c0d95e )
by Camilo
02:17
created

DebugTools   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToBinaryRepresentation() 0 11 2
A convertBinaryToString() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT;
6
7
final class DebugTools
8
{
9
    /**
10
     * Handy debugging function
11
     *
12
     * @param string $rawString
13
     * @return string
14
     */
15 1
    public static function convertToBinaryRepresentation(string $rawString): string
16
    {
17 1
        $out = null;
18 1
        $strLength = \strlen($rawString);
19 1
        for ($a = 0; $a < $strLength; $a++) {
20 1
            $dec = \ord($rawString[$a]); //determine symbol ASCII-code
21 1
            $bin = sprintf('%08d', base_convert($dec, 10, 2)); //convert to binary representation and add leading zeros
22 1
            $out .= $bin;
23
        }
24
25 1
        return $out;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $out could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
26
    }
27
28
    /**
29
     * Handy debugging function
30
     *
31
     * @param string $binaryRepresentation
32
     * @param bool $applyBase64
33
     * @return string
34
     */
35 2
    public static function convertBinaryToString(string $binaryRepresentation, bool $applyBase64 = false): string
36
    {
37 2
        $output = '';
38 2
        $binaryStringLength = \strlen($binaryRepresentation);
39 2
        for ($i = 0; $i < $binaryStringLength; $i += 8) {
40 2
            $output .= \chr((int)base_convert(substr($binaryRepresentation, $i, 8), 2, 10));
41
        }
42
43 2
        if ($applyBase64 === true) {
44 1
            return base64_encode($output);
45
        }
46
47 1
        return $output;
48
    }
49
}
50