Passed
Push — master ( 044f74...556ebb )
by Dominik
02:16
created

UrlEncodedTypeDecoder::fixValue()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Decoder;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
9
final class UrlEncodedTypeDecoder implements TypeDecoderInterface
10
{
11
    /**
12
     * @return string
13
     */
14 2
    public function getContentType(): string
15
    {
16 2
        return 'application/x-www-form-urlencoded';
17
    }
18
19
    /**
20
     * @param string $data
21
     *
22
     * @return array
23
     *
24
     * @throws DeserializerRuntimeException
25
     */
26 3
    public function decode(string $data): array
27
    {
28 3
        $rawData = [];
29 3
        parse_str($data, $rawData);
30
31 3
        if ('' !== $data && [] === $rawData) {
32 1
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
33
        }
34
35 2
        return $this->fixValues($rawData);
0 ignored issues
show
Bug introduced by
It seems like $rawData can also be of type null; however, Chubbyphp\Deserializatio...ypeDecoder::fixValues() 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...
36
    }
37
38
    /**
39
     * @param array $rawData
40
     *
41
     * @return array
42
     */
43 2
    private function fixValues(array $rawData): array
44
    {
45 2
        $data = [];
46 2
        foreach ($rawData as $rawKey => $value) {
47 2
            $key = (string) (int) $rawKey === $rawData ? (int) $rawKey : $rawKey;
48
49 2
            if (is_array($value)) {
50 2
                $data[$key] = $this->fixValues($value);
51
            } else {
52 2
                $data[$key] = $this->fixValue($value);
53
            }
54
        }
55
56 2
        return $data;
57
    }
58
59
    /**
60
     * @param string $value
61
     *
62
     * @return mixed
63
     */
64 2
    private function fixValue(string $value)
65
    {
66 2
        if (is_numeric($value) && '0' !== $value[0]) {
67 2
            if ((string) (int) $value === $value) {
68 2
                return (int) $value;
69
            }
70
71 2
            return (float) $value;
72
        }
73
74 2
        if ('' === $value) {
75 1
            return null;
76
        }
77
78 2
        return $value;
79
    }
80
}
81