Passed
Push — feature/0.7.0 ( 0c7d59...ae5b22 )
by Ryuichi
78:01 queued 33:04
created

AnnotationReader::readClass()   D

Complexity

Conditions 10
Paths 3

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 3
nop 0
dl 0
loc 43
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace WebStream\Annotation\Reader;
3
4
use WebStream\Annotation\Base\IAnnotatable;
5
use WebStream\Annotation\Base\Annotation;
6
use WebStream\Annotation\Base\IClass;
7
use WebStream\Annotation\Base\IMethod;
8
use WebStream\Annotation\Base\IMethods;
9
use WebStream\Annotation\Base\IProperty;
10
use WebStream\Annotation\Base\IRead;
11
use WebStream\Annotation\Reader\Extend\ExtendReader;
12
use WebStream\Container\Container;
13
use WebStream\DI\Injector;
14
use WebStream\Exception\Delegate\ExceptionDelegator;
15
use WebStream\Exception\Extend\AnnotationException;
16
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\AnnotationReader 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\AnnotationException as DoctrineAnnotationException;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\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...
18
19
/**
20
 * AnnotationReader
21
 * @author Ryuichi TANAKA.
22
 * @since 2014/05/10
23
 * @version 0.4
24
 */
25
class AnnotationReader
26
{
27
    use Injector;
28
29
    /**
30
     * @var \ReflectionClass リフレクションクラスオブジェクト
31
     */
32
    // private $refClass;
33
34
    /**
35
     * @var IAnnotatable インスタンス
36
     */
37
    private $instance;
38
39
    /**
40
     * @var Logger ロガー
41
     */
42
    // private $logger;
43
44
    /**
45
     * @var Container コンテナ
46
     */
47
    // private $container;
48
49
    /**
50
     * @var array<string> 読み込み可能アノテーション情報
51
     */
52
    private $readableMap;
53
54
    /**
55
     * @var array<ExtendReader> 拡張アノテーションリーダー
56
     */
57
    private $extendReaderMap;
58
59
    /**
60
     * @var array<string> アノテーション情報リスト
61
     */
62
    private $annotationInfoList;
63
64
    /**
65
     * @var array<string> アノテーション情報リスト(拡張リーダー処理済み)
66
     */
67
    private $annotationInfoExtendList;
68
69
    /**
70
     * @var callable 読み込み時の例外
71
     */
72
    private $exception;
73
74
    /**
75
     * @var string 読み込み対象アノテーションクラスパス
76
     */
77
    // private $annotationClasspath;
78
79
    /**
80
     * @var string アクションメソッド
81
     */
82
    private $actionMethod;
83
84
    /**
85
     * constructor
86
     * @param IAnnotatable ターゲットインスタンス
87
     * @param Container 依存コンテナ
88
     */
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...
89
    public function __construct(IAnnotatable $instance)
90
    {
91
        $this->initialize();
92
        $this->instance = $instance;
93
    }
94
95
    /**
96
     * 初期化処理
97
     */
98
    private function initialize()
99
    {
100
        $this->readableMap = [];
101
        $this->extendReaderMap = [];
102
        $this->annotationInfoList = [];
103
        $this->annotationInfoExtendList = [];
104
    }
105
106
    /**
107
     * アノテーション情報リストを返却する
108
     * @param array<mixed> アノテーション情報リスト
109
     */
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...
110
    public function getAnnotationInfoList(): array
111
    {
112
        if (empty($this->extendReaderMap)) {
113
            return $this->annotationInfoList;
114
        }
115
116
        if (!empty($this->annotationInfoExtendList)) {
117
            return $this->annotationInfoExtendList;
118
        }
119
120
        foreach ($this->annotationInfoList as $key => $annotationInfo) {
121
            $readerClasspath = $this->extendReaderMap[$key];
122
            $reader = new $readerClasspath();
123
            $reader->read($annotationInfo);
124
            $this->annotationInfoExtendList[$key] = $reader->getAnnotationInfo();
125
        }
126
127
        return $this->annotationInfoExtendList;
128
    }
129
130
    /**
131
     * 発生した例外を返却する
132
     * @param ExceptionDelegator 発生した例外
133
     */
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...
134
    public function getException()
135
    {
136
        return $this->exception;
137
    }
138
139
    /**
140
     * アクションメソッドを設定する
141
     * @param string アクションメソッド
142
     */
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...
143
    public function setActionMethod(string $actionMethod)
144
    {
145
        $this->actionMethod = $actionMethod;
146
    }
147
148
    /**
149
     * 読み込み可能アノテーション情報を設定する
150
     * @param string アノテーションクラスパス
151
     * @param Container アノテーションクラス依存コンテナ
152
     */
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...
153
    public function readable(string $classpath, Container $container = null)
154
    {
155
        $this->readableMap[$classpath] = $container;
156
    }
157
158
     /**
159
      * 拡張アノテーションリーダーを設定する
160
      * @param string アノテーションクラスパス
161
      * @param string 拡張アノテーションリーダークラスパス
162
      */
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...
163
    public function useExtendReader(string $annotationClasspath, string $readerClasspath)
164
    {
165
        $this->extendReaderMap[$annotationClasspath] = $readerClasspath;
166
    }
167
168
    /**
169
     * アノテーション情報を読み込む
170
     */
171
    public function read()
172
    {
173
        try {
174
            $this->readClass();
175
            $this->readMethod();
176
            $this->readProperty();
177
        } catch (DoctrineAnnotationException $e) {
178
            $this->initialize();
179
            throw new AnnotationException($e);
180
        }
181
    }
182
183
    /**
184
     * クラス情報を読み込む
185
     */
186
    public function readClass()
187
    {
188
        $reader = new DoctrineAnnotationReader();
189
        $refClass = new \ReflectionClass($this->instance);
190
191
        while ($refClass !== false) {
192
            $annotations = $reader->getClassAnnotations($refClass);
193
194
            if (!empty($annotations)) {
195
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
196
                    $annotation = $annotations[$i];
197
198
                    if (!$annotation instanceof IClass) {
199
                        continue;
200
                    }
201
202
                    $key = get_class($annotation);
203
                    if (!array_key_exists($key, $this->readableMap)) {
204
                        continue;
205
                    }
206
207
                    $container = $this->readableMap[$key];
208
209
                    try {
210
                        $annotation->onClassInject($this->instance, $refClass, $container);
0 ignored issues
show
Bug introduced by
$container of type string is incompatible with the type WebStream\Container\Container expected by parameter $container of WebStream\Annotation\Base\IClass::onClassInject(). ( Ignorable by Annotation )

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

210
                        $annotation->onClassInject($this->instance, $refClass, /** @scrutinizer ignore-type */ $container);
Loading history...
211
                    } catch (\Exception $e) {
212
                        if ($this->exception === null) {
213
                            $this->exception = new ExceptionDelegator($this->instance, $e);
214
                        }
215
                        continue;
216
                    }
217
218
                    // IReadを実装している場合、任意のデータを返却する
219
                    if ($annotation instanceof IRead) {
220
                        if (!array_key_exists($key, $this->annotationInfoList)) {
221
                            $this->annotationInfoList[$key] = [];
222
                        }
223
                        $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo();
224
                    }
225
                }
226
            }
227
228
            $refClass = $refClass->getParentClass();
229
        }
230
    }
231
232
    /**
233
     * メソッド情報を読み込む
234
     */
235
    public function readMethod()
236
    {
237
        $reader = new DoctrineAnnotationReader();
238
        $refClass = new \ReflectionClass($this->instance);
239
240
        while ($refClass !== false) {
241
            foreach ($refClass->getMethods() as $refMethod) {
242
                if ($refClass->getName() !== $refMethod->class) {
243
                    continue;
244
                }
245
246
                $annotations = $reader->getMethodAnnotations($refMethod);
247
                if (empty($annotations)) {
248
                    continue;
249
                }
250
251
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
252
                    $annotation = $annotations[$i];
253
254
                    if (!$annotation instanceof IMethod && !$annotation instanceof IMethods) {
255
                        continue;
256
                    }
257
258
                    // IMethodを実装している場合、アクションメソッドのアノテーション以外は読み込まない
259
                    // PHPのメソッドは大文字小文字を区別しないため、そのまま比較するとルーティング解決結果と実際のメソッド名が合わないケースがある
260
                    // PHPの仕様に合わせてメソッド名の文字列比較は小文字に変換してから行う
261
                    if ($annotation instanceof IMethod && strtolower($this->actionMethod) !== strtolower($refMethod->name)) {
262
                        continue;
263
                    }
264
265
                    // 読み込み可能なアノテーション以外は読み込まない
266
                    $key = get_class($annotation);
267
                    if (!array_key_exists($key, $this->readableMap)) {
268
                        continue;
269
                    }
270
271
                    $container = $this->readableMap[$key];
272
273
                    try {
274
                        $annotation->onMethodInject($this->instance, $refMethod, $container);
0 ignored issues
show
Bug introduced by
$container of type string is incompatible with the type WebStream\Container\Container expected by parameter $container of WebStream\Annotation\Bas...thods::onMethodInject(). ( Ignorable by Annotation )

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

274
                        $annotation->onMethodInject($this->instance, $refMethod, /** @scrutinizer ignore-type */ $container);
Loading history...
Bug introduced by
$container of type string is incompatible with the type WebStream\Container\Container expected by parameter $container of WebStream\Annotation\Bas...ethod::onMethodInject(). ( Ignorable by Annotation )

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

274
                        $annotation->onMethodInject($this->instance, $refMethod, /** @scrutinizer ignore-type */ $container);
Loading history...
275
                    } catch (\Exception $e) {
276
                        if ($this->exception === null) {
277
                            $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod);
278
                        }
279
                        continue;
280
                    }
281
282
                    // IReadを実装している場合、任意のデータを返却する
283
                    if ($annotation instanceof IRead) {
284
                        if (!array_key_exists($key, $this->annotationInfoList)) {
285
                            $this->annotationInfoList[$key] = [];
286
                        }
287
                        $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo();
288
                    }
289
                }
290
            }
291
292
            $refClass = $refClass->getParentClass();
293
        }
294
295
        // 拡張リーダー処理結果をクリアする
296
        $this->annotationInfoExtendList = [];
297
    }
298
299
    /**
300
     * プロパティ情報を読み込む
301
     */
302
    private function readProperty()
303
    {
304
        $reader = new DoctrineAnnotationReader();
305
        $refClass = $this->refClass;
0 ignored issues
show
Bug Best Practice introduced by
The property refClass does not exist on WebStream\Annotation\Reader\AnnotationReader. Since you implemented __get, consider adding a @property annotation.
Loading history...
306
307
        while ($refClass !== false) {
308
            foreach ($refClass->getProperties() as $refProperty) {
309
                if ($refClass->getName() !== $refProperty->class) {
310
                    continue;
311
                }
312
313
                $annotations = $reader->getPropertyAnnotations($refProperty);
314
315
                // アノテーション定義がなければ次へ
316
                if (empty($annotations)) {
317
                    continue;
318
                }
319
320
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
321
                    $annotation = $annotations[$i];
322
                    // $annotation->inject('logger', $this->container->logger);
323
324
                    if (!$annotation instanceof IProperty) {
325
                        continue;
326
                    }
327
328
                    $key = get_class($annotation);
329
                    if (!array_key_exists($key, $this->readableMap)) {
330
                        continue;
331
                    }
332
333
                    $container = $this->readableMap[$key];
334
335
                    try {
336
                        $annotation->onPropertyInject($this->instance, $refProperty, $container);
0 ignored issues
show
Bug introduced by
$container of type string is incompatible with the type WebStream\Container\Container expected by parameter $container of WebStream\Annotation\Bas...rty::onPropertyInject(). ( Ignorable by Annotation )

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

336
                        $annotation->onPropertyInject($this->instance, $refProperty, /** @scrutinizer ignore-type */ $container);
Loading history...
337
                    } catch (\Exception $e) {
338
                        if ($this->exception === null) {
339
                            $this->exception = new ExceptionDelegator($this->instance, $e);
340
                        }
341
                        continue;
342
                    }
343
344
                    // IReadを実装している場合、任意のデータを返却する
345
                    if ($annotation instanceof IRead) {
346
                        if (!array_key_exists($key, $this->annotationInfoList)) {
347
                            $this->annotationInfoList[$key] = [];
348
                        }
349
                        $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

349
                        /** @scrutinizer ignore-call */ 
350
                        $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

349
                        /** @scrutinizer ignore-call */ 
350
                        $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...
350
                    }
351
                }
352
            }
353
354
            $refClass = $refClass->getParentClass();
355
        }
356
    }
357
}
358