Passed
Push — feature/0.7.0 ( f53acb...b4ac8e )
by Ryuichi
44:06
created

AnnotationReader::readMethod()   C

Complexity

Conditions 15
Paths 13

Size

Total Lines 58
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 30
nc 13
nop 0
dl 0
loc 58
rs 6.4661
c 0
b 0
f 0

How to fix   Long Method    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;
0 ignored issues
show
introduced by
The private property $annotationInfoExtendList is not used, and could be removed.
Loading history...
68
69
    /**
70
     * @var callable 読み込み時の例外
71
     */
72
    private $exception;
73
74
    /**
75
     * @var string アクションメソッド
76
     */
77
    private $actionMethod;
78
79
    /**
80
     * constructor
81
     * @param IAnnotatable ターゲットインスタンス
82
     * @param Container 依存コンテナ
83
     */
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...
84
    public function __construct(IAnnotatable $instance)
85
    {
86
        $this->initialize();
87
        $this->instance = $instance;
88
    }
89
90
    /**
91
     * 初期化処理
92
     */
93
    private function initialize()
94
    {
95
        $this->readableMap = [];
96
        $this->extendReaderMap = [];
97
        $this->annotationInfoList = [];
98
    }
99
100
    /**
101
     * アノテーション情報リストを返却する
102
     * @param array<mixed> アノテーション情報リスト
103
     */
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...
104
    public function getAnnotationInfoList(): array
105
    {
106
        if (empty($this->extendReaderMap)) {
107
            return $this->annotationInfoList;
108
        }
109
110
        foreach ($this->annotationInfoList as $key => $annotationInfo) {
111
            if (!array_key_exists($key, $this->extendReaderMap)) {
112
                continue;
113
            }
114
            $readerClasspath = $this->extendReaderMap[$key];
115
            $refClass = new \ReflectionClass($readerClasspath);
116
            $reader = $refClass->newInstance();
117
            $reader->read($annotationInfo);
118
            $this->annotationInfoList[$key] = $reader->getAnnotationInfo();
119
        }
120
121
        return $this->annotationInfoList;
122
    }
123
124
    /**
125
     * 発生した例外を返却する
126
     * @param ExceptionDelegator 発生した例外
127
     */
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...
128
    public function getException()
129
    {
130
        return $this->exception;
131
    }
132
133
    /**
134
     * アクションメソッドを設定する
135
     * @param string アクションメソッド
136
     */
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...
137
    public function setActionMethod(string $actionMethod)
138
    {
139
        $this->actionMethod = $actionMethod;
140
    }
141
142
    /**
143
     * 読み込み可能アノテーション情報を設定する
144
     * @param string アノテーションクラスパス
145
     * @param Container アノテーションクラス依存コンテナ
146
     */
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...
147
    public function readable(string $classpath, Container $container = null)
148
    {
149
        $this->readableMap[$classpath] = $container;
150
    }
151
152
     /**
153
      * 拡張アノテーションリーダーを設定する
154
      * @param string アノテーションクラスパス
155
      * @param string 拡張アノテーションリーダークラスパス
156
      */
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...
157
    public function useExtendReader(string $annotationClasspath, string $readerClasspath)
158
    {
159
        $this->extendReaderMap[$annotationClasspath] = $readerClasspath;
160
    }
161
162
    /**
163
     * アノテーション情報を読み込む
164
     */
165
    public function read()
166
    {
167
        try {
168
            $this->readClass();
169
            $this->readMethod();
170
            $this->readProperty();
171
        } catch (DoctrineAnnotationException $e) {
172
            $this->initialize();
173
            throw new AnnotationException($e);
174
        }
175
    }
176
177
    /**
178
     * クラス情報を読み込む
179
     */
180
    public function readClass()
181
    {
182
        $reader = new DoctrineAnnotationReader();
183
        $refClass = new \ReflectionClass($this->instance);
184
185
        while ($refClass !== false) {
186
            $annotations = $reader->getClassAnnotations($refClass);
187
188
            if (!empty($annotations)) {
189
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
190
                    $annotation = $annotations[$i];
191
192
                    if (!$annotation instanceof IClass) {
193
                        continue;
194
                    }
195
196
                    $key = get_class($annotation);
197
                    if (!array_key_exists($key, $this->readableMap)) {
198
                        continue;
199
                    }
200
201
                    $container = $this->readableMap[$key];
202
203
                    try {
204
                        $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

204
                        $annotation->onClassInject($this->instance, $refClass, /** @scrutinizer ignore-type */ $container);
Loading history...
205
                    } catch (\Exception $e) {
206
                        if ($this->exception === null) {
207
                            $this->exception = new ExceptionDelegator($this->instance, $e);
0 ignored issues
show
Documentation Bug introduced by
It seems like new WebStream\Exception\...or($this->instance, $e) of type WebStream\Exception\Delegate\ExceptionDelegator is incompatible with the declared type callable of property $exception.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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

268
                        $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...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

268
                        $annotation->onMethodInject($this->instance, $refMethod, /** @scrutinizer ignore-type */ $container);
Loading history...
269
                    } catch (\Exception $e) {
270
                        if ($this->exception === null) {
271
                            $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod);
0 ignored issues
show
Documentation Bug introduced by
It seems like new WebStream\Exception\...e, $this->actionMethod) of type WebStream\Exception\Delegate\ExceptionDelegator is incompatible with the declared type callable of property $exception.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
272
                        }
273
                        continue;
274
                    }
275
276
                    // IReadを実装している場合、任意のデータを返却する
277
                    if ($annotation instanceof IRead) {
278
                        if (!array_key_exists($key, $this->annotationInfoList)) {
279
                            $this->annotationInfoList[$key] = [];
280
                        }
281
                        $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo();
282
                    }
283
                }
284
            }
285
286
            $refClass = $refClass->getParentClass();
287
        }
288
    }
289
290
    /**
291
     * プロパティ情報を読み込む
292
     */
293
    private function readProperty()
294
    {
295
        $reader = new DoctrineAnnotationReader();
296
        $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...
297
298
        while ($refClass !== false) {
299
            foreach ($refClass->getProperties() as $refProperty) {
300
                if ($refClass->getName() !== $refProperty->class) {
301
                    continue;
302
                }
303
304
                $annotations = $reader->getPropertyAnnotations($refProperty);
305
306
                // アノテーション定義がなければ次へ
307
                if (empty($annotations)) {
308
                    continue;
309
                }
310
311
                for ($i = 0, $count = count($annotations); $i < $count; $i++) {
312
                    $annotation = $annotations[$i];
313
                    // $annotation->inject('logger', $this->container->logger);
314
315
                    if (!$annotation instanceof IProperty) {
316
                        continue;
317
                    }
318
319
                    $key = get_class($annotation);
320
                    if (!array_key_exists($key, $this->readableMap)) {
321
                        continue;
322
                    }
323
324
                    $container = $this->readableMap[$key];
325
326
                    try {
327
                        $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

327
                        $annotation->onPropertyInject($this->instance, $refProperty, /** @scrutinizer ignore-type */ $container);
Loading history...
328
                    } catch (\Exception $e) {
329
                        if ($this->exception === null) {
330
                            $this->exception = new ExceptionDelegator($this->instance, $e);
0 ignored issues
show
Documentation Bug introduced by
It seems like new WebStream\Exception\...or($this->instance, $e) of type WebStream\Exception\Delegate\ExceptionDelegator is incompatible with the declared type callable of property $exception.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
331
                        }
332
                        continue;
333
                    }
334
335
                    // IReadを実装している場合、任意のデータを返却する
336
                    if ($annotation instanceof IRead) {
337
                        if (!array_key_exists($key, $this->annotationInfoList)) {
338
                            $this->annotationInfoList[$key] = [];
339
                        }
340
                        $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

340
                        /** @scrutinizer ignore-call */ 
341
                        $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

340
                        /** @scrutinizer ignore-call */ 
341
                        $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...
341
                    }
342
                }
343
            }
344
345
            $refClass = $refClass->getParentClass();
346
        }
347
    }
348
}
349