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

ReflectionMethod   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 67
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setObject() 0 5 1
A getDeclaringClass() 0 12 2
A getAnnotations() 0 11 2
A getAnnotation() 0 11 3
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