Test Failed
Pull Request — master (#34)
by Anatoly
02:07
created

AbstractReference::getAnnotation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 33
rs 9.6
cc 4
nc 4
nop 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Annotation\OpenApi;
13
14
/**
15
 * Import classes
16
 */
17
use Doctrine\Common\Annotations\SimpleAnnotationReader;
18
use ReflectionClass;
19
use ReflectionMethod;
20
use ReflectionProperty;
21
use Sunrise\Http\Router\OpenApi\ComponentObjectInterface;
22
23
/**
24
 * Import functions
25
 */
26
use function hash;
27
28
/**
29
 * AbstractReference
30
 *
31
 * @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#reference-object
32
 */
33
abstract class AbstractReference
34
{
35
36
    /**
37
     * @var array
38
     */
39
    private static $cache = [];
40
41
    /**
42
     * @Required
43
     *
44
     * @var string
45
     */
46
    public $class;
47
48
    /**
49
     * @var string
50
     */
51
    public $method;
52
53
    /**
54
     * @var string
55
     */
56
    public $property;
57
58
    /**
59
     * @var null|ComponentObjectInterface
60
     */
61
    private $target;
62
63
    /**
64
     * @return string
65
     */
66
    abstract protected function getAnnotationName() : string;
67
68
    /**
69
     * @param SimpleAnnotationReader $annotationReader
70
     *
71
     * @return null|ComponentObjectInterface
72
     */
73
    public function getAnnotation(SimpleAnnotationReader $annotationReader) : ?ComponentObjectInterface
74
    {
75
        $key = hash(
76
            'md5',
77
            $this->class .
78
            $this->method .
79
            $this->property .
80
            $this->getAnnotationName()
81
        );
82
83
        $this->target =& self::$cache[$key];
84
85
        if (isset(self::$cache[$key])) {
86
            return self::$cache[$key];
87
        }
88
89
        if (isset($this->method)) {
90
            return $this->target = $annotationReader->getMethodAnnotation(
91
                new ReflectionMethod($this->class, $this->method),
92
                $this->getAnnotationName()
93
            );
94
        }
95
96
        if (isset($this->property)) {
97
            return $this->target = $annotationReader->getPropertyAnnotation(
98
                new ReflectionProperty($this->class, $this->property),
99
                $this->getAnnotationName()
100
            );
101
        }
102
103
        return $this->target = $annotationReader->getClassAnnotation(
104
            new ReflectionClass($this->class),
105
            $this->getAnnotationName()
106
        );
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function toArray() : array
113
    {
114
        if (isset($this->target)) {
115
            return ['$ref' => sprintf(
116
                '#/components/%s/%s',
117
                $this->target->getComponentName(),
0 ignored issues
show
Bug introduced by
The method getComponentName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
                $this->target->/** @scrutinizer ignore-call */ 
118
                               getComponentName(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
                $this->target->getReferenceName()
119
            )];
120
        }
121
122
        return ['$ref' => 'undefined'];
123
    }
124
}
125