ReflectionMethod   C
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 381
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 54
lcom 1
cbo 5
dl 0
loc 381
ccs 105
cts 108
cp 0.9722
rs 6.4799
c 0
b 0
f 0

43 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A export() 0 4 1
A __toString() 0 4 1
A inNamespace() 0 4 1
A isClosure() 0 4 1
A isDeprecated() 0 4 1
A isInternal() 0 4 1
A isUserDefined() 0 4 1
A getClosureThis() 0 4 1
A getClosureScopeClass() 0 4 1
A getDocComment() 0 4 2
A getEndLine() 0 4 1
A getExtension() 0 4 1
A getExtensionName() 0 4 1
A getFileName() 0 4 1
A getName() 0 4 1
A getNamespaceName() 0 4 1
A getNumberOfParameters() 0 4 1
A getNumberOfRequiredParameters() 0 4 1
A getParameters() 0 11 2
A getReturnType() 0 4 1
A getShortName() 0 4 1
A getStartLine() 0 4 1
A getStaticVariables() 0 4 1
A returnsReference() 0 4 1
A isGenerator() 0 4 1
A isVariadic() 0 4 1
A isPublic() 0 4 1
A isPrivate() 0 4 1
A isProtected() 0 4 1
A isAbstract() 0 4 1
A isFinal() 0 4 1
A isStatic() 0 4 1
A isConstructor() 0 4 1
A isDestructor() 0 4 1
A getClosure() 0 10 3
A getModifiers() 0 4 1
A invoke() 0 14 4
A invokeArgs() 0 14 4
A getDeclaringClass() 0 4 1
A getPrototype() 0 4 1
A setAccessible() 0 4 1
A isAccessible() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like ReflectionMethod often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ReflectionMethod, and based on these observations, apply Extract Interface, too.

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 ReflectionMethod as CoreReflectionMethod;
10
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented;
11
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
12
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
13
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
14
use Throwable;
15
use function func_get_args;
16
17
class ReflectionMethod extends CoreReflectionMethod
18
{
19
    /** @var BetterReflectionMethod */
20
    private $betterReflectionMethod;
21
22
    /** @var bool */
23
    private $accessible = false;
24
25 52
    public function __construct(BetterReflectionMethod $betterReflectionMethod)
26
    {
27 52
        $this->betterReflectionMethod = $betterReflectionMethod;
28 52
    }
29
30
    /**
31
     * {@inheritDoc}
32
     *
33
     * @throws Exception
34
     */
35 1
    public static function export($class, $name, $return = null)
36
    {
37 1
        throw new Exception('Unable to export statically');
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 1
    public function __toString()
44
    {
45 1
        return $this->betterReflectionMethod->__toString();
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 1
    public function inNamespace()
52
    {
53 1
        return $this->betterReflectionMethod->inNamespace();
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59 1
    public function isClosure()
60
    {
61 1
        return $this->betterReflectionMethod->isClosure();
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    public function isDeprecated()
68
    {
69 1
        return $this->betterReflectionMethod->isDeprecated();
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 1
    public function isInternal()
76
    {
77 1
        return $this->betterReflectionMethod->isInternal();
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 1
    public function isUserDefined()
84
    {
85 1
        return $this->betterReflectionMethod->isUserDefined();
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 1
    public function getClosureThis()
92
    {
93 1
        throw new NotImplemented('Not implemented');
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 1
    public function getClosureScopeClass()
100
    {
101 1
        throw new NotImplemented('Not implemented');
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 2
    public function getDocComment()
108
    {
109 2
        return $this->betterReflectionMethod->getDocComment() ?: false;
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaroslav Hanslík
The expression $this->betterReflectionM...tDocComment() ?: false; of type string|false adds false to the return on line 109 which is incompatible with the return type of the parent method ReflectionMethod::getDocComment of type string. It seems like you forgot to handle an error condition.
Loading history...
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115 1
    public function getEndLine()
116
    {
117 1
        return $this->betterReflectionMethod->getEndLine();
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123 1
    public function getExtension()
124
    {
125 1
        throw new NotImplemented('Not implemented');
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131 2
    public function getExtensionName()
132
    {
133 2
        return $this->betterReflectionMethod->getExtensionName() ?? false;
0 ignored issues
show
Bug Best Practice introduced by Jaroslav Hanslík
The return type of return $this->betterRefl...tensionName() ?? false; (false) is incompatible with the return type of the parent method ReflectionMethod::getExtensionName of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 2
    public function getFileName()
140
    {
141 2
        return $this->betterReflectionMethod->getFileName() ?? false;
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaroslav Hanslík
The expression $this->betterReflectionM...getFileName() ?? false; of type string|false adds false to the return on line 141 which is incompatible with the return type of the parent method ReflectionMethod::getFileName of type string. It seems like you forgot to handle an error condition.
Loading history...
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147 1
    public function getName()
148
    {
149 1
        return $this->betterReflectionMethod->getName();
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155 1
    public function getNamespaceName()
156
    {
157 1
        return $this->betterReflectionMethod->getNamespaceName();
158
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163 1
    public function getNumberOfParameters()
164
    {
165 1
        return $this->betterReflectionMethod->getNumberOfParameters();
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171 1
    public function getNumberOfRequiredParameters()
172
    {
173 1
        return $this->betterReflectionMethod->getNumberOfRequiredParameters();
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179 1
    public function getParameters()
180
    {
181 1
        $parameters = $this->betterReflectionMethod->getParameters();
182
183 1
        $wrappedParameters = [];
184 1
        foreach ($parameters as $key => $parameter) {
185 1
            $wrappedParameters[$key] = new ReflectionParameter($parameter);
186
        }
187
188 1
        return $wrappedParameters;
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     */
194 1
    public function getReturnType()
195
    {
196 1
        return ReflectionNamedType::fromReturnTypeOrNull($this->betterReflectionMethod->getReturnType());
197
    }
198
199
    /**
200
     * {@inheritDoc}
201
     */
202 1
    public function getShortName()
203
    {
204 1
        return $this->betterReflectionMethod->getShortName();
205
    }
206
207
    /**
208
     * {@inheritDoc}
209
     */
210 1
    public function getStartLine()
211
    {
212 1
        return $this->betterReflectionMethod->getStartLine();
213
    }
214
215
    /**
216
     * {@inheritDoc}
217
     */
218 1
    public function getStaticVariables()
219
    {
220 1
        throw new NotImplemented('Not implemented');
221
    }
222
223
    /**
224
     * {@inheritDoc}
225
     */
226 1
    public function returnsReference()
227
    {
228 1
        return $this->betterReflectionMethod->returnsReference();
229
    }
230
231
    /**
232
     * {@inheritDoc}
233
     */
234 1
    public function isGenerator()
235
    {
236 1
        return $this->betterReflectionMethod->isGenerator();
237
    }
238
239
    /**
240
     * {@inheritDoc}
241
     */
242 1
    public function isVariadic()
243
    {
244 1
        return $this->betterReflectionMethod->isVariadic();
245
    }
246
247
    /**
248
     * {@inheritDoc}
249
     */
250 10
    public function isPublic()
251
    {
252 10
        return $this->betterReflectionMethod->isPublic();
253
    }
254
255
    /**
256
     * {@inheritDoc}
257
     */
258 1
    public function isPrivate()
259
    {
260 1
        return $this->betterReflectionMethod->isPrivate();
261
    }
262
263
    /**
264
     * {@inheritDoc}
265
     */
266 1
    public function isProtected()
267
    {
268 1
        return $this->betterReflectionMethod->isProtected();
269
    }
270
271
    /**
272
     * {@inheritDoc}
273
     */
274 1
    public function isAbstract()
275
    {
276 1
        return $this->betterReflectionMethod->isAbstract();
277
    }
278
279
    /**
280
     * {@inheritDoc}
281
     */
282 1
    public function isFinal()
283
    {
284 1
        return $this->betterReflectionMethod->isFinal();
285
    }
286
287
    /**
288
     * {@inheritDoc}
289
     */
290 1
    public function isStatic()
291
    {
292 1
        return $this->betterReflectionMethod->isStatic();
293
    }
294
295
    /**
296
     * {@inheritDoc}
297
     */
298 1
    public function isConstructor()
299
    {
300 1
        return $this->betterReflectionMethod->isConstructor();
301
    }
302
303
    /**
304
     * {@inheritDoc}
305
     */
306 1
    public function isDestructor()
307
    {
308 1
        return $this->betterReflectionMethod->isDestructor();
309
    }
310
311
    /**
312
     * {@inheritDoc}
313
     */
314 4
    public function getClosure($object = null)
315
    {
316
        try {
317 4
            return $this->betterReflectionMethod->getClosure($object);
318 3
        } catch (NoObjectProvided | NotAnObject $e) {
319 2
            return null;
320 1
        } catch (Throwable $e) {
321 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
322
        }
323
    }
324
325
    /**
326
     * {@inheritDoc}
327
     */
328 1
    public function getModifiers()
329
    {
330 1
        return $this->betterReflectionMethod->getModifiers();
331
    }
332
333
    /**
334
     * {@inheritDoc}
335
     */
336 4
    public function invoke($object = null, $args = null)
337
    {
338 4
        if (! $this->isAccessible()) {
339 1
            throw new CoreReflectionException('Method not accessible');
340
        }
341
342
        try {
343 3
            return $this->betterReflectionMethod->invoke(...func_get_args());
344 2
        } catch (NoObjectProvided | NotAnObject $e) {
345 1
            return null;
346 1
        } catch (Throwable $e) {
347 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
348
        }
349
    }
350
351
    /**
352
     * {@inheritDoc}
353
     */
354 5
    public function invokeArgs($object = null, array $args = [])
355
    {
356 5
        if (! $this->isAccessible()) {
357 1
            throw new CoreReflectionException('Method not accessible');
358
        }
359
360
        try {
361 4
            return $this->betterReflectionMethod->invokeArgs($object, $args);
362 3
        } catch (NoObjectProvided | NotAnObject $e) {
363 2
            return null;
364 1
        } catch (Throwable $e) {
365 1
            throw new CoreReflectionException($e->getMessage(), 0, $e);
366
        }
367
    }
368
369
    /**
370
     * {@inheritDoc}
371
     */
372 1
    public function getDeclaringClass()
373
    {
374 1
        return new ReflectionClass($this->betterReflectionMethod->getImplementingClass());
375
    }
376
377
    /**
378
     * {@inheritDoc}
379
     */
380 1
    public function getPrototype()
381
    {
382 1
        return new self($this->betterReflectionMethod->getPrototype());
383
    }
384
385
    /**
386
     * {@inheritDoc}
387
     */
388
    public function setAccessible($accessible)
389
    {
390
        $this->accessible = true;
391
    }
392
393 9
    private function isAccessible() : bool
394
    {
395 9
        return $this->accessible || $this->isPublic();
396
    }
397
}
398