Completed
Pull Request — 1.x (#1)
by Dorian
02:34
created

ReflectionUtils   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 73
Duplicated Lines 46.58 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 0
dl 34
loc 73
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D getFieldValue() 16 26 10
D setFieldValue() 18 29 10

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
namespace MetaHydrator\Utils;
3
4
use ReflectionClass;
5
use ReflectionException;
6
use ReflectionMethod;
7
8
class ReflectionUtils
9
{
10
    /**
11
     * @param $object
12
     * @param string $field
13
     * @return mixed
14
     *
15
     * @throws ReflectionException
16
     */
17
    static public function getFieldValue($object, $field)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
18
    {
19
        $reflectionClass = new ReflectionClass($object);
20
21 View Code Duplication
        foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
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...
22
            if (!$reflectionMethod->isStatic()
23
                && $reflectionMethod->getNumberOfRequiredParameters() == 0
24
                && strcasecmp($reflectionMethod->getName(), "get$field") == 0) {
0 ignored issues
show
Bug introduced by
Consider using $reflectionMethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
25
                return $reflectionMethod->invoke($object);
26
            }
27
        }
28 View Code Duplication
        if ($reflectionClass->hasProperty($field)) {
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...
29
            $property = $reflectionClass->getProperty($field);
30
            if (!$property->isStatic()) {
31
                if (!$property->isPublic()) {
32
                    $property->setAccessible(true);
33
                }
34
                return $property->getValue($object);
35
            }
36
        }
37
        if (is_array($object) || $object instanceof \ArrayAccess) {
38
            return $object[$field];
39
        }
40
41
        throw new ReflectionException("could not extract field '$field' from object");
42
    }
43
44
    /**
45
     * @param $object
46
     * @param string $field
47
     * @param mixed $value
48
     *
49
     * @throws ReflectionException
50
     */
51
    static public function setFieldValue(&$object, $field, $value)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
52
    {
53
        $reflectionClass = new ReflectionClass($object);
54
55 View Code Duplication
        foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
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...
56
            if (!$reflectionMethod->isStatic()
57
                && $reflectionMethod->getNumberOfRequiredParameters() == 1
58
                && strcasecmp($reflectionMethod->getName(), "set$field") == 0) {
0 ignored issues
show
Bug introduced by
Consider using $reflectionMethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
59
                $reflectionMethod->invoke($object, $value);
60
                return;
61
            }
62
        }
63 View Code Duplication
        if ($reflectionClass->hasProperty($field)) {
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...
64
            $property = $reflectionClass->getProperty($field);
65
            if (!$property->isStatic()) {
66
                if (!$property->isPublic()) {
67
                    $property->setAccessible(true);
68
                }
69
                $property->setValue($object, $value);
70
                return;
71
            }
72
        }
73
        if (is_array($object) || $object instanceof \ArrayAccess) {
74
            $object[$field] = $value;
75
            return;
76
        }
77
78
        throw new ReflectionException("could not set field '$field' from object");
79
    }
80
}
81