ReflectionPropertyFactory   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 137
Duplicated Lines 7.3 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
c 1
b 0
f 0
lcom 1
cbo 1
dl 10
loc 137
rs 9.2

17 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A export() 0 4 1
A getDeclaringClass() 0 6 2
A getDocComment() 0 4 1
A getModifiers() 0 4 1
A getName() 0 4 1
B getValue() 0 20 8
A isDefault() 0 4 1
A isPrivate() 0 4 1
A isProtected() 0 4 1
A isPublic() 0 4 1
A isStatic() 0 4 1
C setValue() 0 25 8
A __toString() 0 4 1
A __construct() 10 10 3
A setAccessible() 0 4 1
A getReflector() 0 4 1

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
namespace DependencyInjection\Internal;
4
5
class ReflectionPropertyFactory implements ReflectionFactoryInterface
6
{
7
    /**
8
     * @var \ReflectionProperty
9
     */
10
    private $reflectionProperty;
11
12 View Code Duplication
    public function __construct($class, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
13
    {
14
        if (!is_string($class) && !class_exists($class)) {
15
            throw Exception\ReflectionExceptionFactory::invalidArgument(
16
                sprintf("Parameter 1 of %s must be either string and valid class name.", __METHOD__)
17
            );
18
        }
19
20
        $this->reflectionProperty = new \ReflectionProperty($class, $name);
21
    }
22
23
    public static function create($class, $name)
24
    {
25
        return new static($class, $name);
26
    }
27
28
    public static function export($class, $name, $return = false)
29
    {
30
        return \ReflectionProperty::export($class, $name, $return);
31
    }
32
33
    public function getDeclaringClass()
34
    {
35
        $decl = $this->reflectionProperty->getDeclaringClass();
36
37
        return ($decl instanceof \ReflectionClass ? $decl : null);
38
    }
39
40
    public function getDocComment()
41
    {
42
        return $this->reflectionProperty->getDocComment();
43
    }
44
45
    public function getModifiers()
46
    {
47
        return $this->reflectionProperty->getModifiers();
48
    }
49
50
    public function getName()
51
    {
52
        return $this->reflectionProperty->getName();
53
    }
54
55
    public function getValue($object = null)
56
    {
57
        if (is_null($object) && !$this->isStatic()) {
58
            throw Exception\ReflectionExceptionFactory::logic(
59
                sprintf(
60
                    "Parameter 1 of %s must be a string because the current property being analyze is not static.",
61
                    __METHOD__
62
                )
63
            );
64
        }
65
66
        $object = (is_string($object) && class_exists($object)
67
            ? ReflectionClassFactory::create($object)->newInstance()
68
            : (is_object($object)
69
                ? $object
70
                : null));
71
72
        return ($this->isStatic() ? $this->reflectionProperty->getValue()
73
            : (is_null($object) ? null : $this->reflectionProperty->getValue($object)));
74
    }
75
76
    public function isDefault()
77
    {
78
        return $this->reflectionProperty->isDefault();
79
    }
80
81
    public function isPrivate()
82
    {
83
        return $this->reflectionProperty->isPrivate();
84
    }
85
86
    public function isProtected()
87
    {
88
        return $this->reflectionProperty->isProtected();
89
    }
90
91
    public function isPublic()
92
    {
93
        return $this->reflectionProperty->isPublic();
94
    }
95
96
    public function isStatic()
97
    {
98
        return $this->reflectionProperty->isStatic();
99
    }
100
101
    public function setAccessible($accessible = false)
102
    {
103
        $this->reflectionProperty->setAccessible($accessible);
104
    }
105
106
    public function setValue($object, $value)
107
    {
108
        if (is_null($object) && !$this->isStatic()) {
109
            throw Exception\ReflectionExceptionFactory::logic(
110
                sprintf(
111
                    "Parameter 1 of %s must be a string because the current property being analyze is not static.",
112
                    __METHOD__
113
                )
114
            );
115
        }
116
117
        $object = (is_string($object) && class_exists($object)
118
            ? ReflectionClassFactory::create($object)->newInstance()
119
            : (is_object($object)
120
                ? $object
121
                : null));
122
123
        if ($this->isStatic()) {
124
            $this->reflectionProperty->setValue($value);
125
        } else {
126
            if (!is_null($object)) {
127
                $this->reflectionProperty->setValue($object, $value);
128
            }
129
        }
130
    }
131
132
    public function __toString()
133
    {
134
        return (string)$this->reflectionProperty;
135
    }
136
137
    public function getReflector()
138
    {
139
        return $this->reflectionProperty;
140
    }
141
}
142