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

ReflectionUtils::getFieldValue()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 26
Code Lines 16

Duplication

Lines 16
Ratio 61.54 %

Importance

Changes 0
Metric Value
dl 16
loc 26
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 16
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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