FilterExtendReader::read()   C
last analyzed

Complexity

Conditions 16
Paths 110

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 16.1004

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 71
ccs 38
cts 41
cp 0.9268
rs 5.4833
c 2
b 0
f 0
cc 16
nc 110
nop 1
crap 16.1004

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
3
namespace WebStream\Annotation\Reader\Extend;
4
5
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...
6
use WebStream\Annotation\Container\AnnotationListContainer;
7
8
/**
9
 * FilterExtendReader
10
 * @author Ryuichi Tanaka
11
 * @since 2017/01/08
12
 * @version 0.7
13
 */
14
class FilterExtendReader extends ExtendReader
15
{
16
    /**
17
     * @var array<Container> アノテーション情報リスト
18
     */
19
    private $annotationInfo;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 20
    public function getAnnotationInfo()
25
    {
26 20
        return $this->annotationInfo;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 20
    public function read(array $annotationInfoList)
33
    {
34 20
        $filterListContainer = new Container();
35 20
        $filterListContainer->initialize = new AnnotationListContainer();
36 20
        $filterListContainer->before = new AnnotationListContainer();
37 20
        $filterListContainer->after = new AnnotationListContainer();
38
39 20
        $exceptMethods = [];
40
        // アクションメソッドの@Filter(type="skip")をチェックする
41
        // 1メソッドに対して複数の@Filterが指定されてもエラーにはしない
42 20
        foreach ($annotationInfoList as $annotationInfo) {
43 20
            if ($annotationInfo['classpath'] . "#" . $annotationInfo['action'] === $annotationInfo['refMethod']->class . "#" . $annotationInfo['refMethod']->name) {
44 2
                if ($annotationInfo['annotation']->type === 'skip') {
45 2
                    $exceptMethods = $annotationInfo['annotation']->except;
46 2
                    if (!is_array($exceptMethods)) {
47 1
                        $exceptMethods = [$exceptMethods];
48
                    }
49
                }
50
            }
51
        }
52
53 20
        $isInitialized = false;
54 20
        foreach ($annotationInfoList as $annotationInfo) {
55 20
            $type = $annotationInfo['annotation']->type;
56 20
            $only = $annotationInfo['annotation']->only;
57 20
            $except = $annotationInfo['annotation']->except;
58 20
            $refMethod = $annotationInfo['refMethod'];
59 20
            $action = $annotationInfo['action'];
60
            // initializeはCoreControllerでのみ使用可能なため複数回指定されたら例外
61 20
            if ($type === "initialize") {
62
                if ($isInitialized) {
63
                    throw new AnnotationException("Can not multiple define @Filter(type=\"initialize\") at method.");
0 ignored issues
show
Bug introduced by
The type WebStream\Annotation\Rea...end\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...
64
                }
65
                $isInitialized = true;
66 20
            } elseif (array_key_exists($type, array_flip(["before", "after"]))) {
67
                // skip filterが有効なら適用しない
68
                // クラスに関係なくメソッド名が一致した場合すべて適用しない
69 20
                if (array_key_exists($refMethod->name, array_flip($exceptMethods))) {
70 2
                    continue;
71
                }
72
                // only
73 19
                if ($only !== null) {
74 9
                    $onlyList = $only;
75 9
                    if (!is_array($onlyList)) {
76 5
                        $onlyList = [$onlyList];
77
                    }
78
                    // アクションメソッド名がonlyListに含まれていれば実行対象とする
79 9
                    if (!array_key_exists($action, array_flip($onlyList))) {
80 8
                        continue;
81
                    }
82
                }
83
                // exceptは親クラス以上すべてのメソッドに対して適用するのでメソッド名のみ取得
84 17
                if ($except !== null) {
85 9
                    $exceptList = $except;
86 9
                    if (!is_array($exceptList)) {
87 5
                        $exceptList = [$exceptList];
88
                    }
89
                    // アクションメソッド名がexceptListに含まれていれば実行対象としない
90 9
                    if (array_key_exists($action, array_flip($exceptList))) {
91 7
                        continue;
92
                    }
93
                }
94
            } else {
95 2
                continue;
96
            }
97
            // 実行時に動的にインスタンスを渡すようにしないと、各メソッドでの実行結果が反映されないため
98
            // この時点でのクロージャへのインスタンス設定はせず、リストとして保持するに留める
99 16
            $filterListContainer->{$type}->push($refMethod);
100
        }
101
102 20
        $this->annotationInfo = $filterListContainer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filterListContainer of type WebStream\Container\Container is incompatible with the declared type WebStream\Container\Container[] of property $annotationInfo.

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...
103
    }
104
}
105