ReflectionMethod   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 15
Bugs 4 Features 0
Metric Value
eloc 16
c 15
b 4
f 0
dl 0
loc 65
ccs 23
cts 23
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotation() 0 10 3
A setObject() 0 3 1
A getDeclaringClass() 0 11 2
A getAnnotations() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use Ray\ServiceLocator\ServiceLocator;
8
9
use function assert;
10
use function class_exists;
11
use function is_object;
12
13
final class ReflectionMethod extends \ReflectionMethod implements Reader
14
{
15
    /** @var ?WeavedInterface */
16
    private $object;
17
18
    /**
19
     * Set dependencies
20
     */
21
    public function setObject(WeavedInterface $object): void
22
    {
23
        $this->object = $object;
24
    }
25
26 3
    /**
27
     * @return ReflectionClass<object>
28 3
     *
29 3
     * @psalm-external-mutation-free
30 3
     * @psalm-suppress MethodSignatureMismatch
31
     */
32
    public function getDeclaringClass(): ReflectionClass
33
    {
34
        if (! is_object($this->object)) {
35 2
            return new ReflectionClass($this->class);
36
        }
37 2
38 2
        $parencClass = (new \ReflectionClass($this->object))->getParentClass();
39
        assert($parencClass instanceof \ReflectionClass);
40
        $originalClass = $parencClass->name;
41 2
42 2
        return new ReflectionClass($originalClass);
43 2
    }
44
45 2
    /**
46
     * {@inheritDoc}
47
     *
48
     * @psalm-suppress NoInterfaceProperties
49
     */
50
    public function getAnnotations(): array
51 2
    {
52
        assert(class_exists($this->class));
53
        /** @var list<object> $annotations */
54 2
        $annotations = ServiceLocator::getReader()->getMethodAnnotations(new \ReflectionMethod($this->class, $this->name));
55 2
56 2
        return $annotations;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $annotations returns the type Ray\Aop\list which is incompatible with the type-hinted return array.
Loading history...
57 1
    }
58
59
    /**
60 1
     * @param class-string<T> $annotationName
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
61
     *
62
     * @return T|null
63
     *
64
     * @template T of object
65
     *
66 2
     * @psalm-suppress MoreSpecificImplementedParamType
67
     */
68 2
    public function getAnnotation(string $annotationName)
69 2
    {
70 1
        $annotations = $this->getAnnotations();
71 1
        foreach ($annotations as $annotation) {
72
            if ($annotation instanceof $annotationName) {
73
                return $annotation;
74
            }
75 1
        }
76
77
        return null;
78
    }
79
}
80