Passed
Push — master ( f4a0a2...9fb418 )
by Dominik
02:30
created

UrlEncodedTypeEncoder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 85
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 0
dl 25
loc 85
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 4 1
A encode() 0 4 1
B buildQuery() 0 25 6
B getValueAsString() 25 25 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Encoder;
6
7
final class UrlEncodedTypeEncoder implements TypeEncoderInterface
8
{
9
    /**
10
     * @return string
11
     */
12 1
    public function getContentType(): string
13
    {
14 1
        return 'application/x-www-form-urlencoded';
15
    }
16
17
    /**
18
     * @param array $data
19
     *
20
     * @return string
21
     */
22 2
    public function encode(array $data): string
23
    {
24 2
        return $this->buildQuery($data);
25
    }
26
27
    /**
28
     * @param array  $data
29
     * @param string $path
30
     *
31
     * @return string
32
     */
33 2
    private function buildQuery(array $data, string $path = ''): string
34
    {
35 2
        if ([] === $data) {
36 1
            return '';
37
        }
38
39 2
        $query = '';
40 2
        foreach ($data as $key => $value) {
41 2
            if (null === $value) {
42 1
                continue;
43
            }
44
45 2
            $subPath = '' !== $path ? $path.'['.$key.']' : (string) $key;
46 2
            if (is_array($value)) {
47 1
                $query .= $this->buildQuery($value, $subPath);
48
            } else {
49 2
                $query .= $subPath.'='.urlencode($this->getValueAsString($value));
50
            }
51 1
            $query .= '&';
52
        }
53
54 1
        $query = substr($query, 0, -strlen('&'));
55
56 1
        return $query;
57
    }
58
59
    /**
60
     * @param bool|int|float|string $value
61
     *
62
     * @return string
63
     *
64
     * @throws \InvalidArgumentException
65
     */
66 2 View Code Duplication
    private function getValueAsString($value): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 2
        if (is_string($value)) {
69 1
            return $value;
70
        }
71
72 2
        if (is_bool($value)) {
73 1
            return $value ? 'true' : 'false';
74
        }
75
76 2
        if (is_float($value)) {
77 1
            $value = (string) $value;
78 1
            if (false === strpos($value, '.')) {
79 1
                $value .= '.0';
80
            }
81
82 1
            return $value;
83
        }
84
85 2
        if (is_int($value)) {
86 1
            return (string) $value;
87
        }
88
89 1
        throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value)));
90
    }
91
}
92