Completed
Push — cs ( 9c9fc7 )
by Akihito
01:43
created

ReflectionMethod::getAnnotation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
final class ReflectionMethod extends \ReflectionMethod implements Reader
8
{
9
    /**
10
     * @var WeavedInterface
11
     */
12
    private $object;
13
14
    /**
15
     * @var string
16
     */
17
    private $method;
18
19
    /**
20
     * Set dependencies
21
     */
22 1
    public function setObject(WeavedInterface $object, \ReflectionMethod $method)
23
    {
24 1
        $this->object = $object;
25 1
        $this->method = $method->name;
26 1
    }
27
28
    public function getDeclaringClass() : ReflectionClass
29
    {
30
        $parencClass = (new \ReflectionClass($this->object))->getParentClass();
31
        if (! $parencClass instanceof \ReflectionClass) {
32
            throw new \LogicException; // @codeCoverageIgnore
33
        }
34
        $originalClass = $parencClass->name;
35
        $class = new ReflectionClass($originalClass);
36
        $class->setObject($this->object);
37
38
        return $class;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getAnnotations() : array
45
    {
46
        /** @var AbstractWeave $object */
47
        $object = $this->object;
48
        $annotations = unserialize($object->methodAnnotations);
49
        if (array_key_exists($this->method, $annotations)) {
50
            return $annotations[$this->method];
51
        }
52
53
        return [];
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getAnnotation(string $annotationName)
60
    {
61
        $annotations = $this->getAnnotations();
62
        foreach ($annotations as $annotation) {
63
            if ($annotation instanceof $annotationName) {
64
                return $annotation;
65
            }
66
        }
67
    }
68
}
69