Completed
Pull Request — 2.x (#91)
by Akihito
02:24
created

ReflectionMethod::getDeclaringClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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