Completed
Pull Request — develop (#605)
by
unknown
14:48
created

ArrayDefinitionMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 7.58 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 5
loc 66
ccs 14
cts 21
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B map() 5 25 4
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 ArrayMapper
4
 */
5
namespace Graviton\ProxyApiBundle\Helper;
6
7
use Graviton\ProxyApiBundle\Listener\ProxyExceptionListener;
8
use Psr\Log\InvalidArgumentException;
9
use Symfony\Component\DependencyInjection\Definition;
10
11
/**
12
 * Automatically map ARRAY structures into objects.
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class ArrayDefinitionMapper
19
{
20
    /**
21
     * Maps data into object class
22
     *
23
     * @param object $array  to be casted
24
     * @param object $object Class to receive data
25
     * @throws ProxyExceptionListener
26
     * @return object
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Definition.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
27
     */
28 2
    public function map($array, $object)
29
    {
30 2
        if (!is_array($array)) {
31
            throw new ProxyExceptionListener(
32
                422,
33
                'JsonMapper::map() requires first argument to be an Array'
34
                . ', ' . gettype($array) . ' given.'
35
            );
36
        }
37 2
        if (!($object instanceof Definition)) {
38
            throw new ProxyExceptionListener(
39
                422,
40
                'JsonMapper::map() requires second argument to be a Definition'
41
                . ', ' . gettype($object) . ' given.'
42
            );
43
        }
44
45 2 View Code Duplication
        foreach ($array as $key => $value) {
46 2
            $key = $this->getSafeName($key);
47 2
            $setter = 'set' . $this->getCamelCaseName($key);
48 2
            $object->addMethodCall($setter, [$value]);
49
        }
50
51 2
        return $object;
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 2
    protected function getCamelCaseName($name)
62
    {
63 2
        return str_replace(
64 2
            ' ', '', 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 2
    protected function getSafeName($name)
76
    {
77 2
        if (strpos($name, '-') !== false) {
78
            $name = $this->getCamelCaseName($name);
79
        }
80
81 2
        return $name;
82
    }
83
}
84