Passed
Push — master ( f03a56...8d49be )
by Dominik
02:42
created

UrlEncodedTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Transformer;
6
7
final class UrlEncodedTransformer implements TransformerInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $numericPrefix;
13
14
    /**
15
     * @var string
16
     */
17
    private $argSeperator;
18
19
    /**
20
     * @param string $numericPrefix
21
     * @param string $argSeperator
22
     */
23 2
    public function __construct(string $numericPrefix = '', string $argSeperator = '&')
24
    {
25 2
        $this->numericPrefix = $numericPrefix;
26 2
        $this->argSeperator = $argSeperator;
27 2
    }
28
29
    /**
30
     * @param string $string
31
     *
32
     * @return array
33
     */
34 2
    public function transform(string $string): array
35
    {
36 2
        $rawData = [];
37 2
        parse_str(str_replace($this->argSeperator, '&', $string), $rawData);
38
39 2
        return $this->cleanRawData($rawData, strlen($this->numericPrefix));
0 ignored issues
show
Bug introduced by
It seems like $rawData can also be of type null; however, Chubbyphp\Deserializatio...sformer::cleanRawData() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
    }
41
42
    /**
43
     * @param array $rawData
44
     * @param int   $numericPrefixLength
45
     *
46
     * @return array
47
     */
48 2
    private function cleanRawData(array $rawData, int $numericPrefixLength): array
49
    {
50 2
        $data = [];
51 2
        foreach ($rawData as $rawKey => $value) {
52 2
            if (0 !== $numericPrefixLength && 0 === strpos($rawKey, $this->numericPrefix)) {
53 1
                $rawSubKey = substr($rawKey, $numericPrefixLength);
54 1
                if (is_numeric($rawSubKey)) {
55 1
                    $rawKey = $rawSubKey;
56
                }
57
            }
58
59 2
            $key = is_numeric($rawKey) ? (int) $rawKey : $rawKey;
60
61 2
            if (is_array($value)) {
62 2
                $data[$key] = $this->cleanRawData($value, $numericPrefixLength);
63
            } else {
64 2
                $data[$key] = $value;
65
            }
66
        }
67
68 2
        return $data;
69
    }
70
}
71