Completed
Push — master ( 89795a...454e04 )
by Marco
07:56 queued 07:53
created

ReflectionProperty::hasType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\Reflection\Adapter;
6
7
use ReflectionException as CoreReflectionException;
8
use ReflectionProperty as CoreReflectionProperty;
9
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
10
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
11
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
12
use Throwable;
13
use function func_get_args;
14
15
class ReflectionProperty extends CoreReflectionProperty
16
{
17
    /** @var BetterReflectionProperty */
18
    private $betterReflectionProperty;
19
20
    /** @var bool */
21
    private $accessible = false;
22
23 21
    public function __construct(BetterReflectionProperty $betterReflectionProperty)
24
    {
25 21
        $this->betterReflectionProperty = $betterReflectionProperty;
26 21
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 1
    public static function export($class, $name, $return = null)
32
    {
33 1
        BetterReflectionProperty::export(...func_get_args());
0 ignored issues
show
Unused Code introduced by Jaroslav Hanslík
The call to ReflectionProperty::export() has too many arguments starting with \func_get_args().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 1
    public function __toString()
40
    {
41 1
        return $this->betterReflectionProperty->__toString();
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 1
    public function getName()
48
    {
49 1
        return $this->betterReflectionProperty->getName();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 5
    public function getValue($object = null)
56
    {
57 5
        if (! $this->isAccessible()) {
58 1
            throw new CoreReflectionException('Property not accessible');
59
        }
60
61
        try {
62 4
            return $this->betterReflectionProperty->getValue($object);
63 3
        } catch (NoObjectProvided | NotAnObject $e) {
64 2
            return null;
65 1
        } catch (Throwable $e) {
66 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
67
        }
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 5
    public function setValue($object, $value = null)
74
    {
75 5
        if (! $this->isAccessible()) {
76 1
            throw new CoreReflectionException('Property not accessible');
77
        }
78
79
        try {
80 4
            $this->betterReflectionProperty->setValue($object, $value);
81 3
        } catch (NoObjectProvided | NotAnObject $e) {
82 2
            return null;
83 1
        } catch (Throwable $e) {
84 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
85
        }
86 1
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 9
    public function hasType()
92
    {
93 9
        return $this->betterReflectionProperty->hasType();
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 1
    public function getType()
100
    {
101 1
        return ReflectionType::fromReturnTypeOrNull($this->betterReflectionProperty->getType());
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 1
    public function isPublic()
108
    {
109 1
        return $this->betterReflectionProperty->isPublic();
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115 1
    public function isPrivate()
116
    {
117 1
        return $this->betterReflectionProperty->isPrivate();
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123 1
    public function isProtected()
124
    {
125 1
        return $this->betterReflectionProperty->isProtected();
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131 1
    public function isStatic()
132
    {
133 1
        return $this->betterReflectionProperty->isStatic();
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 1
    public function isDefault()
140
    {
141 1
        return $this->betterReflectionProperty->isDefault();
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147 2
    public function getModifiers()
148
    {
149 2
        return $this->betterReflectionProperty->getModifiers();
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155 2
    public function getDeclaringClass()
156
    {
157 2
        return new ReflectionClass($this->betterReflectionProperty->getImplementingClass());
158 2
    }
159
160 10
    /**
161
     * {@inheritDoc}
162 10
     */
163
    public function getDocComment()
164
    {
165
        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 165 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...
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    public function setAccessible($accessible)
172
    {
173
        $this->accessible = true;
174
    }
175
176
    public function isAccessible() : bool
177
    {
178
        return $this->accessible || $this->isPublic();
179
    }
180
}
181