Completed
Push — 2.x ( 3dc104...879a72 )
by Akihito
01:18
created

ReflectionMethod::getCachedAnnotations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
9
use function array_key_exists;
10
use function assert;
11
use function class_exists;
12
use function is_object;
13
use function is_string;
14
use function property_exists;
15
use function unserialize;
16
17
final class ReflectionMethod extends \ReflectionMethod implements Reader
18
{
19
    /** @var ?WeavedInterface */
20
    private $object;
21
22
    /** @var string */
23
    private $method = '';
24
25
    /**
26 3
     * Set dependencies
27
     */
28 3
    public function setObject(WeavedInterface $object, \ReflectionMethod $method): void
29 3
    {
30 3
        $this->object = $object;
31
        $this->method = $method->name;
32
    }
33
34
    public function getDeclaringClass(): ReflectionClass
35 2
    {
36
        if (! is_object($this->object)) {
37 2
            assert(class_exists($this->class));
38 2
39
            return new ReflectionClass($this->class);
40
        }
41 2
42 2
        $parencClass = (new \ReflectionClass($this->object))->getParentClass();
43 2
        assert($parencClass instanceof \ReflectionClass);
44
        $originalClass = $parencClass->name;
45 2
        assert(class_exists($originalClass));
46
        $class = new ReflectionClass($originalClass);
47
        $class->setObject($this->object);
48
49
        return $class;
50
    }
51 2
52
    /**
53
     * {@inheritdoc}
54 2
     *
55 2
     * @psalm-suppress NoInterfaceProperties
56 2
     */
57 1
    public function getAnnotations(): array
58
    {
59
        $object = $this->object;
60 1
        if (is_object($object) && property_exists($object, 'methodAnnotations') && is_string($object->methodAnnotations)) {
61
            return $this->getCachedAnnotations($object->methodAnnotations);
62
        }
63
64
        assert(class_exists($this->class));
65
        /** @var list<object> $annotations */
0 ignored issues
show
Documentation introduced by
The doc-type list<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
66 2
        $annotations = (new AnnotationReader())->getMethodAnnotations(new \ReflectionMethod($this->class, $this->name));
67
68 2
        return $annotations;
69 2
    }
70 1
71 1
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getAnnotation(string $annotationName)
75 1
    {
76
        $annotations = $this->getAnnotations();
77
        foreach ($annotations as $annotation) {
78
            if ($annotation instanceof $annotationName) {
79
                return $annotation;
80
            }
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * @return list<object>
0 ignored issues
show
Documentation introduced by
The doc-type list<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
88
     */
89
    private function getCachedAnnotations(string $methodAnnotations): array
90
    {
91
        /** @var array<string, list<object>> $annotations */
92
        $annotations = unserialize((string) $methodAnnotations, ['allowed_classes' => true]);
93
        if (array_key_exists($this->method, $annotations)) {
94
            return $annotations[$this->method];
95
        }
96
97
        return [];
98
    }
99
}
100