Passed
Push — master ( fb814c...01670e )
by Ryuichi
36:14 queued 34:17
created

AnnotationReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WebStream\Annotation\Reader;
4
5
use WebStream\Annotation\Base\IAnnotatable;
6
use WebStream\Annotation\Base\IClass;
7
use WebStream\Annotation\Base\IExtension;
8
use WebStream\Annotation\Base\IMethod;
9
use WebStream\Annotation\Base\IMethods;
10
use WebStream\Annotation\Base\IProperty;
11
use WebStream\Annotation\Base\IRead;
12
use WebStream\Annotation\Reader\Extend\ExtendReader;
13
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use WebStream\Exception\Delegate\ExceptionDelegator;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Delegate\ExceptionDelegator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use WebStream\Exception\Extend\AnnotationException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\AnnotationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
18
use Doctrine\Common\Annotations\AnnotationException as DoctrineAnnotationException;
19
20
/**
21
 * AnnotationReader
22
 * @author Ryuichi TANAKA.
23
 * @since 2014/05/10
24
 * @version 0.4
25
 */
26
class AnnotationReader
27
{
28
    use Injector;
29
30
    /**
31
     * @var IAnnotatable インスタンス
32
     */
33
    private $instance;
34
35
    /**
36
     * @var array<string> 読み込み可能アノテーション情報
37
     */
38
    private $readableMap;
39
40
    /**
41
     * @var array<ExtendReader> 拡張アノテーションリーダー
42
     */
43
    private $extendReaderMap;
44
45
    /**
46
     * @var array<string> アノテーション情報リスト
47
     */
48
    private $annotationInfoList;
49
50
    /**
51
     * @var array<string> アノテーション情報リスト(拡張リーダー処理済み)
52
     */
53
    private $annotationInfoExtendList;
0 ignored issues
show
introduced by
The private property $annotationInfoExtendList is not used, and could be removed.
Loading history...
54
55
    /**
56
     * @var ExceptionDelegator 読み込み時の例外
57
     */
58
    private $exception;
59
60
    /**
61
     * @var string アクションメソッド
62
     */
63
    private $actionMethod;
64
65
    /**
66
     * @var Container デフォルト依存コンテナ
67
     */
68
    private $defaultContainer;
69
70
    /**
71
     * constructor
72
     * @param IAnnotatable ターゲットインスタンス
0 ignored issues
show
Documentation Bug introduced by
The doc comment ターゲットインスタンス at position 0 could not be parsed: Unknown type name 'ターゲットインスタンス' at position 0 in ターゲットインスタンス.
Loading history...
73
     */
74 146
    public function __construct(IAnnotatable $instance)
75
    {
76 146
        $this->initialize();
77 146
        $this->instance = $instance;
78
    }
79
80
    /**
81
     * 初期化処理
82
     */
83 146
    private function initialize()
84
    {
85 146
        $this->readableMap = [];
86 146
        $this->extendReaderMap = [];
87 146
        $this->annotationInfoList = [];
88 146
        $this->defaultContainer = new Container(false);
89
    }
90
91
    /**
92
     * アノテーション情報リストを返却する
93
     * @return array<mixed> アノテーション情報リスト
94
     */
95 48
    public function getAnnotationInfoList(): array
96
    {
97 48
        if (empty($this->extendReaderMap)) {
98 24
            return $this->annotationInfoList;
99
        }
100
101 24
        foreach ($this->annotationInfoList as $key => $annotationInfo) {
102 24
            if (!array_key_exists($key, $this->extendReaderMap)) {
103
                continue;
104
            }
105 24
            $readerClasspath = $this->extendReaderMap[$key];
106 24
            $refClass = new \ReflectionClass($readerClasspath);
107 24
            $reader = $refClass->newInstance();
108 24
            $reader->read($annotationInfo);
109 24
            $this->annotationInfoList[$key] = $reader->getAnnotationInfo();
110
        }
111
112 24
        return $this->annotationInfoList;
113
    }
114
115
    /**
116
     * 発生した例外を返却する
117
     * @return ExceptionDelegator 発生した例外
118
     */
119 98
    public function getException(): ?ExceptionDelegator
120
    {
121 98
        return $this->exception;
122
    }
123
124
    /**
125
     * アクションメソッドを設定する
126
     * @param string アクションメソッド
0 ignored issues
show
Documentation Bug introduced by
The doc comment アクションメソッド at position 0 could not be parsed: Unknown type name 'アクションメソッド' at position 0 in アクションメソッド.
Loading history...
127
     */
128 145
    public function setActionMethod(string $actionMethod)
129
    {
130 145
        $this->actionMethod = $actionMethod;
131
    }
132
133
    /**
134
     * 読み込み可能アノテーション情報を設定する
135
     * @param string アノテーションクラスパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment アノテーションクラスパス at position 0 could not be parsed: Unknown type name 'アノテーションクラスパス' at position 0 in アノテーションクラスパス.
Loading history...
136
     * @param Container アノテーションクラス依存コンテナ
137
     */
138 140
    public function readable(string $classpath, Container $container = null)
139
    {
140 140
        $this->readableMap[$classpath] = $container;
141
    }
142
143
     /**
144
      * 拡張アノテーションリーダーを設定する
145
      * @param string アノテーションクラスパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment アノテーションクラスパス at position 0 could not be parsed: Unknown type name 'アノテーションクラスパス' at position 0 in アノテーションクラスパス.
Loading history...
146
      * @param string 拡張アノテーションリーダークラスパス
147
      */
148 24
    public function useExtendReader(string $annotationClasspath, string $readerClasspath)
149
    {
150 24
        $this->extendReaderMap[$annotationClasspath] = $readerClasspath;
151
    }
152
153
    /**
154
     * アノテーション情報を読み込む
155
     */
156
    public function read()
157
    {
158
        try {
159
            $this->readClass();
160
            $this->readMethod();
161
            $this->readProperty();
162
        } catch (DoctrineAnnotationException $e) {
163
            $this->initialize();
164
            throw new AnnotationException($e);
165
        }
166
    }
167
168
    /**
169
     * クラス情報を読み込む
170
     */
171 2
    public function readClass()
172
    {
173 2
        $reader = new DoctrineAnnotationReader();
174 2
        $refClass = new \ReflectionClass($this->instance);
175
176 2
        while ($refClass !== false) {
177 2
            $annotations = $reader->getClassAnnotations($refClass);
178
179 2
            if (!empty($annotations)) {
180 2
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
181 2
                    $annotation = $annotations[$i];
182
183 2
                    if (!$annotation instanceof IClass) {
184
                        continue;
185
                    }
186
187 2
                    $key = get_class($annotation);
188 2
                    $container = null;
189 2
                    if (!array_key_exists($key, $this->readableMap)) {
190
                        if ($annotation instanceof IExtension) {
191
                            $container = $this->defaultContainer;
192
                        } else {
193
                            continue;
194
                        }
195
                    } else {
196 2
                        $container = $this->readableMap[$key];
197
                    }
198
199
                    try {
200 2
                        $annotation->onClassInject($this->instance, $refClass, $container);
201 1
                    } catch (\Exception $e) {
202 1
                        if ($this->exception === null) {
203 1
                            $this->exception = new ExceptionDelegator($this->instance, $e);
204
                        }
205 1
                        continue;
206
                    }
207
208
                    // IReadを実装している場合、任意のデータを返却する
209 1
                    if ($annotation instanceof IRead) {
210 1
                        if (!array_key_exists($key, $this->annotationInfoList)) {
211 1
                            $this->annotationInfoList[$key] = [];
212
                        }
213 1
                        $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo();
214
                    }
215
                }
216
            }
217
218 2
            $refClass = $refClass->getParentClass();
219
        }
220
    }
221
222
    /**
223
     * メソッド情報を読み込む
224
     */
225 144
    public function readMethod()
226
    {
227 144
        $reader = new DoctrineAnnotationReader();
228 144
        $refClass = new \ReflectionClass($this->instance);
229
230 144
        while ($refClass !== false) {
231 144
            foreach ($refClass->getMethods() as $refMethod) {
232 144
                if ($refClass->getName() !== $refMethod->class) {
233
                    continue;
234
                }
235
236 144
                $annotations = $reader->getMethodAnnotations($refMethod);
237 144
                if (empty($annotations)) {
238 25
                    continue;
239
                }
240
241 144
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
242 144
                    $annotation = $annotations[$i];
243
244 144
                    if (!$annotation instanceof IMethod && !$annotation instanceof IMethods) {
245
                        continue;
246
                    }
247
248
                    // IMethodを実装している場合、アクションメソッドのアノテーション以外は読み込まない
249
                    // PHPのメソッドは大文字小文字を区別しないため、そのまま比較するとルーティング解決結果と実際のメソッド名が合わないケースがある
250
                    // PHPの仕様に合わせてメソッド名の文字列比較は小文字に変換してから行う
251 144
                    if ($annotation instanceof IMethod && strtolower($this->actionMethod) !== strtolower($refMethod->name)) {
252 105
                        continue;
253
                    }
254
255
                    // 読み込み可能なアノテーション以外は読み込まない
256 144
                    $key = get_class($annotation);
257 144
                    $container = null;
258 144
                    if (!array_key_exists($key, $this->readableMap)) {
259 6
                        if ($annotation instanceof IExtension) {
260 1
                            $container = $this->defaultContainer;
261
                        } else {
262 5
                            continue;
263
                        }
264
                    } else {
265 138
                        $container = $this->readableMap[$key];
266
                    }
267
268
                    try {
269 139
                        $annotation->onMethodInject($this->instance, $refMethod, $container);
270 54
                    } catch (\Exception $e) {
271 54
                        if ($this->exception === null) {
272 54
                            $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod);
273
                        }
274 54
                        continue;
275
                    }
276
277
                    // IReadを実装している場合、任意のデータを返却する
278 85
                    if ($annotation instanceof IRead) {
279 41
                        if (!array_key_exists($key, $this->annotationInfoList)) {
280 41
                            $this->annotationInfoList[$key] = [];
281
                        }
282 41
                        $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo();
283
                    }
284
                }
285
            }
286
287 144
            $refClass = $refClass->getParentClass();
288
        }
289
    }
290
291
    /**
292
     * プロパティ情報を読み込む
293
     */
294
    private function readProperty()
295
    {
296
        $reader = new DoctrineAnnotationReader();
297
        $refClass = $this->refClass;
298
299
        while ($refClass !== false) {
300
            foreach ($refClass->getProperties() as $refProperty) {
301
                if ($refClass->getName() !== $refProperty->class) {
302
                    continue;
303
                }
304
305
                $annotations = $reader->getPropertyAnnotations($refProperty);
306
307
                // アノテーション定義がなければ次へ
308
                if (empty($annotations)) {
309
                    continue;
310
                }
311
312
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
313
                    $annotation = $annotations[$i];
314
315
                    if (!$annotation instanceof IProperty) {
316
                        continue;
317
                    }
318
319
                    $key = get_class($annotation);
320
                    $container = null;
321
                    if (!array_key_exists($key, $this->readableMap)) {
322
                        if ($annotation instanceof IExtension) {
323
                            $container = $this->defaultContainer;
324
                        } else {
325
                            continue;
326
                        }
327
                    } else {
328
                        $container = $this->readableMap[$key];
329
                    }
330
331
                    try {
332
                        $annotation->onPropertyInject($this->instance, $refProperty, $container);
333
                    } catch (\Exception $e) {
334
                        if ($this->exception === null) {
335
                            $this->exception = new ExceptionDelegator($this->instance, $e);
336
                        }
337
                        continue;
338
                    }
339
340
                    // IReadを実装している場合、任意のデータを返却する
341
                    if ($annotation instanceof IRead) {
342
                        if (!array_key_exists($key, $this->annotationInfoList)) {
343
                            $this->annotationInfoList[$key] = [];
344
                        }
345
                        $this->annotationInfoList[$key][] = $annotation->onInjected();
0 ignored issues
show
Bug introduced by
The method onInjected() does not exist on WebStream\Annotation\Base\IRead. ( Ignorable by Annotation )

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

345
                        /** @scrutinizer ignore-call */ 
346
                        $this->annotationInfoList[$key][] = $annotation->onInjected();

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...
Bug introduced by
The method onInjected() does not exist on WebStream\Annotation\Base\IExtension. ( Ignorable by Annotation )

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

345
                        /** @scrutinizer ignore-call */ 
346
                        $this->annotationInfoList[$key][] = $annotation->onInjected();

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...
Bug introduced by
The method onInjected() does not exist on WebStream\Annotation\Base\IProperty. ( Ignorable by Annotation )

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

345
                        /** @scrutinizer ignore-call */ 
346
                        $this->annotationInfoList[$key][] = $annotation->onInjected();

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...
346
                    }
347
                }
348
            }
349
350
            $refClass = $refClass->getParentClass();
351
        }
352
    }
353
}
354