Hydrator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 23.23 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 23
loc 99
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C hydrate() 11 28 7
B extract() 12 22 5
A getReflectionProperties() 0 13 3
A getReflectionClass() 0 7 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
4
namespace SolBianca\Hydrator;
5
6
7
class Hydrator implements HydratorInterface
8
{
9
10
    /**
11
     * Local cache of reflection class instances
12
     * @var array
13
     */
14
    private $reflectionClassMap = [];
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function hydrate($object, array $data)
20
    {
21
        if (is_object($object)) {
22
            $className = get_class($object);
23
            $reflection = $this->getReflectionClass($className);
24
        } elseif (is_string($object)) {
25
            if (!class_exists($object)) {
26
                throw new HydrateObjectException("Given class name '{$object}' must be valid class name.");
27
            }
28
            $className = $object;
29
            $reflection = $this->getReflectionClass($className);
30
            $object = $reflection->newInstanceWithoutConstructor();
31
        } else {
32
            throw new HydrateObjectException("Invalid object.");
33
        }
34 View Code Duplication
        foreach ($data as $propertyName => $propertyValue) {
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...
35
            if (!$reflection->hasProperty($propertyName)) {
36
                throw new HydrateObjectException("There's no '$propertyName' property in '$className'.");
37
            }
38
            $property = $reflection->getProperty($propertyName);
39
            if ($property->isStatic()) {
40
                continue;
41
            }
42
            $property->setAccessible(true);
43
            $property->setValue($object, $propertyValue);
44
        }
45
        return $object;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function extract($object, array $properties = [])
52
    {
53
        $data = [];
54
        $className = get_class($object);
55
        $reflection = $this->getReflectionClass($className);
56
        if ([] === $properties) {
57
            $properties = $this->getReflectionProperties($reflection);
58
        }
59 View Code Duplication
        foreach ($properties as $propertyName) {
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...
60
            if ($reflection->hasProperty($propertyName)) {
61
                $property = $reflection->getProperty($propertyName);
62
                if ($property->isStatic()) {
63
                    continue;
64
                }
65
                $property->setAccessible(true);
66
                $data[$propertyName] = $property->getValue($object);
67
            } else {
68
                throw new ExctractObjectException("There's no '$propertyName' property in '$className'.");
69
            }
70
        }
71
        return $data;
72
    }
73
74
    /**
75
     * @param \ReflectionClass $reflection
76
     * @return array
77
     */
78
    protected function getReflectionProperties(\ReflectionClass $reflection)
79
    {
80
        $properties = $reflection->getProperties();
81
        $result = [];
82
        if (empty($properties)) {
83
            return $result;
84
        }
85
86
        foreach ($properties as $property) {
87
            $result[] = $property->getName();
88
        }
89
        return $result;
90
    }
91
92
    /**
93
     * Returns instance of reflection class for class name passed
94
     *
95
     * @param string $className
96
     * @return \ReflectionClass
97
     */
98
    protected function getReflectionClass($className)
99
    {
100
        if (!isset($this->reflectionClassMap[$className])) {
101
            $this->reflectionClassMap[$className] = new \ReflectionClass($className);
102
        }
103
        return $this->reflectionClassMap[$className];
104
    }
105
}
106