ReflectionMethod::setObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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