Passed
Push — master ( ec00fe...edf722 )
by Dominik
02:37
created

UrlEncodedTypeDecoder::fixValue()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 12
cp 0.9167
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 6
nop 1
crap 7.0283
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 ('' === $value) {
67 1
            return null;
68
        }
69
70 2
        if ('true' === $value) {
71 1
            return true;
72
        }
73
74 2
        if ('false' === $value) {
75
            return false;
76
        }
77
78 2
        if (is_numeric($value) && '0' !== $value[0]) {
79 2
            if ((string) (int) $value === $value) {
80 2
                return (int) $value;
81
            }
82
83 2
            return (float) $value;
84
        }
85
86 2
        return $value;
87
    }
88
}
89