ReflectionProperty   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 3
dl 0
loc 168
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A export() 0 4 1
A __toString() 0 4 1
A getName() 0 4 1
A getValue() 0 14 4
A setValue() 0 14 4
A hasType() 0 4 1
A getType() 0 4 1
A isPublic() 0 4 1
A isPrivate() 0 4 1
A isProtected() 0 4 1
A isStatic() 0 4 1
A isDefault() 0 4 1
A getModifiers() 0 4 1
A getDeclaringClass() 0 4 1
A getDocComment() 0 4 2
A setAccessible() 0 4 1
A isAccessible() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\Reflection\Adapter;
6
7
use Exception;
8
use ReflectionException as CoreReflectionException;
9
use ReflectionProperty as CoreReflectionProperty;
10
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
11
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
12
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
13
use Throwable;
14
15
class ReflectionProperty extends CoreReflectionProperty
16
{
17
    /** @var BetterReflectionProperty */
18
    private $betterReflectionProperty;
19
20
    /** @var bool */
21
    private $accessible = false;
22
23 23
    public function __construct(BetterReflectionProperty $betterReflectionProperty)
24
    {
25 23
        $this->betterReflectionProperty = $betterReflectionProperty;
26 23
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @throws Exception
32
     */
33 1
    public static function export($class, $name, $return = null)
34
    {
35 1
        throw new Exception('Unable to export statically');
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 1
    public function __toString()
42
    {
43 1
        return $this->betterReflectionProperty->__toString();
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 1
    public function getName()
50
    {
51 1
        return $this->betterReflectionProperty->getName();
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 5
    public function getValue($object = null)
58
    {
59 5
        if (! $this->isAccessible()) {
60 1
            throw new CoreReflectionException('Property not accessible');
61
        }
62
63
        try {
64 4
            return $this->betterReflectionProperty->getValue($object);
65 3
        } catch (NoObjectProvided | NotAnObject $e) {
66 2
            return null;
67 1
        } catch (Throwable $e) {
68 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
69
        }
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 5
    public function setValue($object, $value = null)
76
    {
77 5
        if (! $this->isAccessible()) {
78 1
            throw new CoreReflectionException('Property not accessible');
79
        }
80
81
        try {
82 4
            $this->betterReflectionProperty->setValue($object, $value);
83 3
        } catch (NoObjectProvided | NotAnObject $e) {
84 2
            return null;
85 1
        } catch (Throwable $e) {
86 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
87
        }
88 1
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 1
    public function hasType()
94
    {
95 1
        return $this->betterReflectionProperty->hasType();
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 1
    public function getType()
102
    {
103 1
        return ReflectionType::fromReturnTypeOrNull($this->betterReflectionProperty->getType());
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 9
    public function isPublic()
110
    {
111 9
        return $this->betterReflectionProperty->isPublic();
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117 1
    public function isPrivate()
118
    {
119 1
        return $this->betterReflectionProperty->isPrivate();
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 1
    public function isProtected()
126
    {
127 1
        return $this->betterReflectionProperty->isProtected();
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 1
    public function isStatic()
134
    {
135 1
        return $this->betterReflectionProperty->isStatic();
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141 1
    public function isDefault()
142
    {
143 1
        return $this->betterReflectionProperty->isDefault();
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 1
    public function getModifiers()
150
    {
151 1
        return $this->betterReflectionProperty->getModifiers();
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 1
    public function getDeclaringClass()
158
    {
159 1
        return new ReflectionClass($this->betterReflectionProperty->getImplementingClass());
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 2
    public function getDocComment()
166
    {
167 2
        return $this->betterReflectionProperty->getDocComment() ?: false;
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaroslav Hanslík
The expression $this->betterReflectionP...tDocComment() ?: false; of type string|false adds false to the return on line 167 which is incompatible with the return type of the parent method ReflectionProperty::getDocComment of type string. It seems like you forgot to handle an error condition.
Loading history...
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173 2
    public function setAccessible($accessible)
174
    {
175 2
        $this->accessible = true;
176 2
    }
177
178 10
    public function isAccessible() : bool
179
    {
180 10
        return $this->accessible || $this->isPublic();
181
    }
182
}
183