setAnnotationWhiteList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Annotation;
26
27
use Brickoo\Component\Annotation\Definition\AnnotationDefinitionTargetFilter;
28
use Brickoo\Component\Common\Collection;
29
use ReflectionClass;
30
31
/**
32
 * AnnotationReflectionClassReader
33
 *
34
 * Implements an annotation reader based on the reflection API.
35
 * @author Celestino Diaz <[email protected]>
36
 */
37
class AnnotationReflectionClassReader {
38
39
    /** @var \Brickoo\Component\Annotation\AnnotationParser */
40
    private $annotationParser;
41
42
    /** @var \Brickoo\Component\Annotation\Definition\AnnotationDefinitionTargetFilter */
43
    private $annotationTargetFilter;
44
45
    /**
46
     * Class constructor.
47
     * @param \Brickoo\Component\Annotation\AnnotationParser $annotationParser
48
     */
49 1
    public function __construct(AnnotationParser $annotationParser) {
50 1
        $this->annotationParser = $annotationParser;
51 1
    }
52
53
    /**
54
     * Returns the read annotations.
55
     * @param \Brickoo\Component\Common\Collection $collection
56
     * @param \ReflectionClass $reflectionClass
57
     * @return \Brickoo\Component\Annotation\AnnotationReaderResult
58
     */
59 1
    public function getAnnotations(Collection $collection, ReflectionClass $reflectionClass) {
60 1
        $this->annotationTargetFilter = new AnnotationDefinitionTargetFilter(($collection));
61 1
        $readerResult = new AnnotationReaderResult($reflectionClass->getName());
62 1
        $this->addClassAnnotations($readerResult, $reflectionClass);
63 1
        $this->addMethodsAnnotations($readerResult, $reflectionClass);
64 1
        $this->addPropertiesAnnotations($readerResult, $reflectionClass);
65 1
        return $readerResult;
66
    }
67
68
    /**
69
     * Adds class annotations to the reader result.
70
     * @param \Brickoo\Component\Annotation\AnnotationReaderResult $result
71
     * @param \ReflectionClass $class
72
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
73
     */
74 1
    private function addClassAnnotations(AnnotationReaderResult $result, ReflectionClass $class) {
75 1
        $this->setAnnotationWhiteList(Annotation::TARGET_CLASS);
76 1
        $this->parseAnnotations($result, Annotation::TARGET_CLASS, $class->getName(), $class->getDocComment());
77 1
        return $this;
78
    }
79
80
    /**
81
     * Adds methods annotations to the reader result.
82
     * @param \Brickoo\Component\Annotation\AnnotationReaderResult $result
83
     * @param \ReflectionClass $class
84
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
85
     */
86 1
    private function addMethodsAnnotations(AnnotationReaderResult $result, ReflectionClass $class) {
87 1
        $this->setAnnotationWhiteList(Annotation::TARGET_METHOD);
88 1
        $this->parseAnnotationList($result, $class, Annotation::TARGET_METHOD);
89 1
        return $this;
90
    }
91
92
    /**
93
     * Adds properties annotations to the reader result.
94
     * @param \Brickoo\Component\Annotation\AnnotationReaderResult $result
95
     * @param \ReflectionClass $class
96
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
97
     */
98 1
    private function addPropertiesAnnotations(AnnotationReaderResult $result, ReflectionClass $class) {
99 1
        $this->setAnnotationWhiteList(Annotation::TARGET_PROPERTY);
100 1
        $this->parseAnnotationList($result, $class, Annotation::TARGET_PROPERTY);
101 1
        return $this;
102
    }
103
104
    /**
105
     * Set the annotation white list for an annotation type.
106
     * @param integer $targetType
107
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
108
     */
109 1
    private function setAnnotationWhiteList($targetType) {
110 1
        $this->annotationParser->setAnnotationWhitelist(
111 1
            $this->getAnnotationsNames(
112 1
                $this->annotationTargetFilter->filter($targetType)
113 1
            )
114 1
        );
115 1
        return $this;
116
    }
117
118
    /**
119
     * Parse the annotation list from class member of a type.
120
     * @param \Brickoo\Component\Annotation\AnnotationReaderResult $result
121
     * @param \ReflectionClass $reflectionClass
122
     * @param integer $targetType
123
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
124
     */
125 1
    private function parseAnnotationList(AnnotationReaderResult $result, ReflectionClass $reflectionClass, $targetType) {
126 1
        $reflectionMemberList = $this->getReflectionMemberList($reflectionClass, $targetType);
127 1
        foreach ($reflectionMemberList as $member) {
128 1
            $this->parseAnnotations(
129 1
                $result,
130 1
                $targetType,
131 1
                sprintf("%s::%s", $reflectionClass->getName(), $member->getName()),
132 1
                $member->getDocComment()
133 1
            );
134 1
        }
135 1
        return $this;
136
    }
137
138
    /**
139
     * Get the reflection member list.
140
     * @param ReflectionClass $reflectionClass
141
     * @param integer $targetType
142
     * @return array the reflection member list
143
     */
144 1
    private function getReflectionMemberList(ReflectionClass $reflectionClass, $targetType) {
145 1
        $reflectionMemberList = [];
146
147
        switch ($targetType) {
148 1
            case Annotation::TARGET_METHOD:
149 1
                $reflectionMemberList = $reflectionClass->getMethods();
150 1
                break;
151 1
            case Annotation::TARGET_PROPERTY:
152 1
                $reflectionMemberList = $reflectionClass->getProperties();
153 1
                break;
154
        }
155
156 1
        return $reflectionMemberList;
157
    }
158
159
    /**
160
     * Returns the annotations names.
161
     * @param \ArrayIterator $annotationsDefinitionsIterator
162
     * @return array the annotations names
163
     */
164 1
    private function getAnnotationsNames(\ArrayIterator $annotationsDefinitionsIterator) {
165 1
        $annotationsNames = [];
166 1
        foreach ($annotationsDefinitionsIterator as $annotationDefinition) {
167 1
            $annotationsNames[] = $annotationDefinition->getName();
168 1
        }
169 1
        return $annotationsNames;
170
    }
171
172
    /**
173
     * Parse the doc comments annotations.
174
     * @param AnnotationReaderResult $result
175
     * @param integer $target
176
     * @param string $targetLocation
177
     * @param string $docComment
178
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
179
     */
180 1
    private function parseAnnotations(AnnotationReaderResult $result, $target, $targetLocation, $docComment) {
181 1
        if ($annotations = $this->annotationParser->parse($target, $targetLocation, $docComment)) {
182 1
            $this->addResultAnnotations($result, $annotations);
183 1
        }
184 1
        return $this;
185
    }
186
187
    /**
188
     * Adds the annotations to the result collection.
189
     * @param \Brickoo\Component\Annotation\AnnotationReaderResult $result
190
     * @param Annotation[] $annotations
191
     * @return \Brickoo\Component\Annotation\AnnotationReflectionClassReader
192
     */
193 1
    private function addResultAnnotations(AnnotationReaderResult $result, array $annotations) {
194 1
        foreach ($annotations as $annotation) {
195 1
            $result->addAnnotation($annotation);
196 1
        }
197 1
        return $this;
198
    }
199
200
}
201