Completed
Push — master ( 011717...660375 )
by Dmitry
04:27
created

Converter   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 80
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 24
lcom 2
cbo 0
dl 10
loc 80
ccs 32
cts 38
cp 0.8421
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B toObject() 5 21 7
A toUnderscore() 0 12 4
B toCamelCase() 0 19 7
B toArray() 5 18 6

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
namespace Basis;
4
5
use stdClass;
6
7
class Converter
8
{
9 13
    public function toObject($data) : stdClass
10
    {
11 13
        if (is_array($data)) {
12 13
            if (array_keys($data) === range(0, count($data) -1)) {
13
                foreach ($data as $k => $v) {
14
                    $data[$k] = $this->toObject($v);
15
                }
16
                return $data;
17
            }
18
        }
19
20 13
        $data = (object) $data;
21
22 13 View Code Duplication
        foreach ($data as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
23 13
            if (is_array($v) || is_object($v)) {
24 13
                $data->$k = $this->toObject($v);
25
            }
26
        }
27
28 13
        return $data;
29
    }
30
31 4
    public function toArray($data) : array
32
    {
33 4
        if (!$data) {
34
            return [];
35
        }
36
37 4
        if (is_object($data)) {
38 4
            $data = get_object_vars($data);
39
        }
40
41 4 View Code Duplication
        foreach ($data as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
42
            if (is_array($v) || is_object($v)) {
43
                $data[$k] = $this->toArray($v);
44
            }
45
        }
46
47 4
        return $data;
48
    }
49
50
    private $underscores = [];
51
52 1
    public function toUnderscore(string $input) : string
53
    {
54 1
        if (!array_key_exists($input, $this->underscores)) {
55 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
56 1
            $ret = $matches[0];
57 1
            foreach ($ret as &$match) {
58 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
59
            }
60 1
            $this->underscores[$input] = implode('_', $ret);
61
        }
62 1
        return $this->underscores[$input];
63
    }
64
65
    private $camelcased = [];
66
67 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
68
    {
69 1
        $capitalize = $capitalize ? 1 : 0;
70
71 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
72 1
            $this->camelcased[$capitalize] = [];
73
        }
74
75 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
76 1
            $chain = explode('_', $string);
77 1
            foreach ($chain as $index => $word) {
78 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
79
            }
80
81 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
82
        }
83
84 1
        return $this->camelcased[$capitalize][$string];
85
    }
86
}
87