Annotation   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 2
dl 0
loc 139
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A collectPropertyAnnotations() 0 18 6
A collectMethodParameterAnnotations() 0 13 4
A getAllProperties() 0 4 1
A getProperty() 0 4 1
A hasProperty() 0 4 1
A getAllMethodsParameters() 0 4 1
A getMethodParameters() 0 4 1
A getMethodParameter() 0 4 1
A hasMethodParameter() 0 4 3
1
<?php
2
3
namespace Dgame\Annotation;
4
5
use Exception;
6
use ReflectionClass;
7
8
/**
9
 * Class Annotation
10
 * @package Dgame\Annotation
11
 */
12
final class Annotation
13
{
14
    /**
15
     * @var VariableAnnotation[]
16
     */
17
    private $properties = [];
18
    /**
19
     * @var VariableAnnotation[][]
20
     */
21
    private $parameters = [];
22
23
    /**
24
     * Annotation constructor.
25
     *
26
     * @param ReflectionClass $class
27
     *
28
     * @throws Exception
29
     */
30
    public function __construct(ReflectionClass $class)
31
    {
32
        $parser = new AnnotationParser();
33
34
        $this->collectPropertyAnnotations($parser, $class);
35
        $this->collectMethodParameterAnnotations($parser, $class);
36
    }
37
38
    /**
39
     * @param AnnotationParser $parser
40
     * @param ReflectionClass  $class
41
     *
42
     * @throws Exception
43
     */
44
    private function collectPropertyAnnotations(AnnotationParser $parser, ReflectionClass $class): void
45
    {
46
        $comment = $class->getDocComment();
47
        $comment = $comment === false ? '' : $comment;
48
        foreach ($parser->parsePropertyAnnotations($comment) as $annotation) {
49
            $this->properties[$annotation->getName()] = $annotation;
50
        }
51
52
        foreach ($class->getProperties() as $property) {
53
            $comment     = $property->getDocComment();
54
            $comment     = $comment === false ? '' : $comment;
55
            $annotations = $parser->parseVariableAnnotations($comment);
56
            $annotation  = array_pop($annotations);
57
            if ($annotation !== null) {
58
                $this->properties[$property->name] = $annotation;
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param AnnotationParser $parser
65
     * @param ReflectionClass  $class
66
     *
67
     * @throws Exception
68
     */
69
    private function collectMethodParameterAnnotations(AnnotationParser $parser, ReflectionClass $class): void
70
    {
71
        foreach ($class->getMethods() as $method) {
72
            $name    = $method->getName();
73
            $comment = $method->getDocComment();
74
            $comment = $comment === false ? '' : $comment;
75
76
            $this->parameters[$name] = [];
77
            foreach ($parser->parseParameterAnnotations($comment) as $annotation) {
78
                $this->parameters[$name][$annotation->getName()] = $annotation;
79
            }
80
        }
81
    }
82
83
    /**
84
     * @return VariableAnnotation[]
85
     */
86
    public function getAllProperties(): array
87
    {
88
        return $this->properties;
89
    }
90
91
    /**
92
     * @param string $property
93
     *
94
     * @return VariableAnnotation
95
     */
96
    public function getProperty(string $property): VariableAnnotation
97
    {
98
        return $this->properties[$property];
99
    }
100
101
    /**
102
     * @param string $property
103
     *
104
     * @return bool
105
     */
106
    public function hasProperty(string $property): bool
107
    {
108
        return array_key_exists($property, $this->properties);
109
    }
110
111
    /**
112
     * @return VariableAnnotation[][]
113
     */
114
    public function getAllMethodsParameters(): array
115
    {
116
        return $this->parameters;
117
    }
118
119
    /**
120
     * @param string $method
121
     *
122
     * @return VariableAnnotation[]
123
     */
124
    public function getMethodParameters(string $method): array
125
    {
126
        return $this->parameters[$method];
127
    }
128
129
    /**
130
     * @param string $method
131
     * @param string $param
132
     *
133
     * @return VariableAnnotation
134
     */
135
    public function getMethodParameter(string $method, string $param): VariableAnnotation
136
    {
137
        return $this->parameters[$method][$param];
138
    }
139
140
    /**
141
     * @param string      $method
142
     * @param string|null $param
143
     *
144
     * @return bool
145
     */
146
    public function hasMethodParameter(string $method, string $param = null): bool
147
    {
148
        return array_key_exists($method, $this->parameters) && ($param === null ? true : array_key_exists($param, $this->parameters[$method]));
149
    }
150
}
151