Completed
Push — master ( 664783...554f9d )
by
unknown
15:59
created

JsonMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 17.91 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 12
loc 67
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B map() 12 26 5
A getCamelCaseName() 0 6 1
A getSafeName() 0 8 2

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
 * Part of JsonMapper
4
 */
5
namespace Graviton\AnalyticsBundle\Helper;
6
7
8
use Psr\Log\InvalidArgumentException;
9
10
/**
11
 * Automatically map JSON structures into objects.
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class JsonMapper
18
{
19
    /**
20
     * Maps data into object class
21
     *
22
     * @param object $json   to be casted
23
     * @param object $object Class to receive data
24
     * @throws InvalidArgumentException
25
     * @return object
26
     */
27
    public function map($json, $object)
28
    {
29 View Code Duplication
        if (!is_object($json)) {
30
            throw new InvalidArgumentException(
31
                'JsonMapper::map() requires first argument to be an object'
32
                . ', ' . gettype($json) . ' given.'
33
            );
34
        }
35 View Code Duplication
        if (!is_object($object)) {
36
            throw new InvalidArgumentException(
37
                'JsonMapper::map() requires second argument to be an object'
38
                . ', ' . gettype($object) . ' given.'
39
            );
40
        }
41
42
        foreach ($json as $key => $jvalue) {
43
            $key = $this->getSafeName($key);
44
            $setter = 'set' . $this->getCamelCaseName($key);
45
            if (method_exists($object, $setter)) {
46
                $object->{$setter}($jvalue);
47
            }
48
        }
49
50
        return $object;
51
52
    }
53
54
    /**
55
     * Removes - and _ and makes the next letter uppercase
56
     *
57
     * @param string $name Property name
58
     *
59
     * @return string CamelCasedVariableName
60
     */
61
    protected function getCamelCaseName($name)
62
    {
63
        return str_replace(
64
            ' ', '', ucwords(str_replace(array('_', '-'), ' ', $name))
65
        );
66
    }
67
68
    /**
69
     * Since hyphens cannot be used in variables we have to uppercase them.
70
     *
71
     * @param string $name Property name
72
     *
73
     * @return string Name without hyphen
74
     */
75
    protected function getSafeName($name)
76
    {
77
        if (strpos($name, '-') !== false) {
78
            $name = $this->getCamelCaseName($name);
79
        }
80
81
        return $name;
82
    }
83
}
84
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...