ServiceSpecification::init()   F
last analyzed

Complexity

Conditions 19
Paths 972

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 31
nc 972
nop 0
dl 0
loc 54
rs 0.3888
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
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-8
6
 * Time: 下午3:43.
7
 */
8
9
namespace MiotApi\Contract\Specification;
10
11
/**
12
 * 服务是一个独立的有意义的功能组,描述一个服务,需要说清楚:
13
 * 是什么服务?
14
 * 有什么方法可以操作?
15
 * 有什么事件可能会发生?
16
 * 有哪些属性?
17
 *
18
 * Class ServiceSpecification
19
 */
20
class ServiceSpecification extends Specification
21
{
22
    /**
23
     * 必选方法列表.
24
     *
25
     * @var
26
     */
27
    protected $requiredActions;
28
29
    /**
30
     * 可选方法列表.
31
     *
32
     * @var
33
     */
34
    protected $optionalActions;
35
36
    /**
37
     * 必选事件列表.
38
     *
39
     * @var
40
     */
41
    protected $requiredEvents;
42
43
    /**
44
     * 可选事件列表.
45
     *
46
     * @var
47
     */
48
    protected $optionalEvents;
49
50
    /**
51
     * 必选属性列表.
52
     *
53
     * @var
54
     */
55
    protected $requiredProperties;
56
57
    /**
58
     * 可选属性列表.
59
     *
60
     * @var
61
     */
62
    protected $optionalProperties;
63
64
    /**
65
     * @throws \MiotApi\Exception\SpecificationErrorException
66
     */
67
    public function init()
68
    {
69
        parent::init();
70
71
        if ($this->has('required-actions')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on MiotApi\Contract\Specifi...on\ServiceSpecification. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

71
        if ($this->/** @scrutinizer ignore-call */ has('required-actions')) {
Loading history...
72
            $requiredActions = $this->__get('required-actions');
73
            if (!empty($requiredActions)) {
74
                foreach ($requiredActions as $index => $action) {
75
                    $this->requiredActions[] = new ActionSpecification($action);
76
                }
77
            }
78
        }
79
80
        if ($this->has('optional-actions')) {
81
            $optionalActions = $this->__get('optional-actions');
82
            if (!empty($optionalActions)) {
83
                foreach ($optionalActions as $index => $action) {
84
                    $this->optionalActions[] = new ActionSpecification($action);
85
                }
86
            }
87
        }
88
89
        if ($this->has('required-events')) {
90
            $requiredEvents = $this->__get('required-events');
91
            if (!empty($requiredEvents)) {
92
                foreach ($requiredEvents as $index => $event) {
93
                    $this->requiredEvents[] = new EventSpecification($event);
94
                }
95
            }
96
        }
97
98
        if ($this->has('optional-events')) {
99
            $optionalEvents = $this->__get('optional-events');
100
            if (!empty($optionalEvents)) {
101
                foreach ($optionalEvents as $index => $event) {
102
                    $this->optionalEvents[] = new EventSpecification($event);
103
                }
104
            }
105
        }
106
107
        if ($this->has('required-properties')) {
108
            $requiredProperties = $this->__get('required-properties');
109
            if (!empty($requiredProperties)) {
110
                foreach ($requiredProperties as $index => $property) {
111
                    $this->requiredProperties[] = new PropertySpecification($property);
112
                }
113
            }
114
        }
115
116
        if ($this->has('optional-properties')) {
117
            $optionalProperties = $this->__get('optional-properties');
118
            if (!empty($optionalProperties)) {
119
                foreach ($optionalProperties as $index => $property) {
120
                    $this->optionalProperties[] = new PropertySpecification($property);
121
                }
122
            }
123
        }
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    public function getRequiredActions()
130
    {
131
        return $this->requiredActions;
132
    }
133
134
    /**
135
     * @return mixed
136
     */
137
    public function getOptionalActions()
138
    {
139
        return $this->optionalActions;
140
    }
141
142
    /**
143
     * @return mixed
144
     */
145
    public function getRequiredEvents()
146
    {
147
        return $this->requiredEvents;
148
    }
149
150
    /**
151
     * @return mixed
152
     */
153
    public function getOptionalEvents()
154
    {
155
        return $this->optionalEvents;
156
    }
157
158
    /**
159
     * @return mixed
160
     */
161
    public function getRequiredProperties()
162
    {
163
        return $this->requiredProperties;
164
    }
165
166
    /**
167
     * @return mixed
168
     */
169
    public function getOptionalProperties()
170
    {
171
        return $this->optionalProperties;
172
    }
173
}
174