Completed
Push — master ( 4c018e...ece8e1 )
by Randy
01:04
created

Annotation::getMethodParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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