RequestParameter   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 261
Duplicated Lines 13.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 4
dl 34
loc 261
rs 8.8
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
B __get() 0 15 6
B __set() 0 17 6
A reset() 0 11 1
A getUserInput() 0 4 1
A getContext() 0 4 1
A getMode() 0 4 1
A getCharacter() 0 4 1
A getUserInformation() 0 7 2
A setUserInput() 7 7 1
A setContext() 7 7 1
A setMode() 10 10 4
A setCharacter() 10 10 4
A setUserInformation() 0 5 1
A makeParameter() 0 10 3
A className() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * 送信パラメータ管理クラス
4
 * @author AIZAWA Hina <[email protected]>
5
 * @copyright 2015 by AIZAWA Hina <[email protected]>
6
 * @license https://github.com/fetus-hina/docomo-dialogue/blob/master/LICENSE MIT
7
 * @since 0.1.0
8
 */
9
10
namespace jp3cki\docomoDialogue;
11
12
/**
13
 * 送信パラメータ管理クラス
14
 *
15
 * @property string $utt            ユーザ入力テキスト
16
 * @property string $context        コンテキストID
17
 * @property string $mode           会話モード
18
 * @property int    $t              チャットキャラクター
19
 * @property UserInformation $user  ユーザ情報
20
 */
21
class RequestParameter
22
{
23
    /** デフォルトのキャラクター */
24
    const CHARACTER_DEFAULT = null;
25
    /** 関西弁のキャラクター */
26
    const CHARACTER_KANSAI  = 20;
27
    /** 赤ちゃんキャラクター */
28
    const CHARACTER_BABY    = 30;
29
30
    /** 対話モード */
31
    const MODE_DIALOG       = 'dialog';
32
    /** しりとりモード */
33
    const MODE_SRTR         = 'srtr';
34
35
    /** @internal @var array */
36
    private $parameters;
37
38
    /** @internal @var UserInformation */
39
    private $user_info = null;
40
41
    /**
42
     * コンストラクタ
43
     */
44
    public function __construct()
45
    {
46
        $this->reset();
47
    }
48
49
    /**
50
     * マジックメソッド __toString
51
     *
52
     * デバッグ等で表示する際に中身がわかるように JSON で返すだけで、
53
     * 表現そのものに意味はないし依存してはならない
54
     *
55
     * @return string
56
     */
57
    public function __toString()
58
    {
59
        return json_encode($this->makeParameter(), JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
60
    }
61
62
    /**
63
     * マジックメソッド __get
64
     *
65
     * @param   string  $key    プロパティ取得用のキー
66
     * @return  string          キーに対応する値
67
     */
68
    public function __get($key)
69
    {
70
        switch ($key) {
71
            case 'utt':
72
                return $this->getUserInput();
73
            case 'context':
74
                return $this->getContext();
75
            case 'mode':
76
                return $this->getMode();
77
            case 't':
78
                return $this->getCharacter();
79
            case 'user':
80
                return $this->getUserInformation();
81
        }
82
    }
83
84
    /**
85
     * マジックメソッド __set
86
     *
87
     * @param   string  $key    プロパティ設定用のキー
88
     * @param   mixed   $value  設定する値
89
     *
90
     * @throws  InvalidArgumentException    対応するキーが存在しない時
91
     * @throws  DomainError                 設定する値が異常な時
92
     */
93
    public function __set($key, $value)
94
    {
95
        switch ($key) {
96
            case 'utt':
97
                return $this->setUserInput($value);
98
            case 'context':
99
                return $this->setContext($value);
100
            case 'mode':
101
                return $this->setMode($value);
102
            case 't':
103
                return $this->setCharacter($value);
104
            case 'user':
105
                return $this->setUserInformation($value);
106
            default:
107
                throw new InvalidArgumentException("Unknown key {$key}");
108
        }
109
    }
110
111
    /**
112
     * パラメータを完全にリセットする
113
     *
114
     * return self
115
     */
116
    public function reset()
117
    {
118
        $this->parameters = [
119
            'utt'       => null,
120
            'context'   => null,
121
            'mode'      => self::MODE_DIALOG,
122
            't'         => self::CHARACTER_DEFAULT,
123
        ];
124
        $this->user_info = null;
125
        return $this;
126
    }
127
128
    /**
129
     * ユーザ入力テキストを取得
130
     *
131
     * @return string
132
     */
133
    public function getUserInput()
134
    {
135
        return $this->parameters['utt'];
136
    }
137
138
    /**
139
     * コンテキストIDを取得
140
     *
141
     * @return string
142
     */
143
    public function getContext()
144
    {
145
        return $this->parameters['context'];
146
    }
147
148
    /**
149
     * 対話モードを取得
150
     *
151
     * @return string
152
     */
153
    public function getMode()
154
    {
155
        return $this->parameters['mode'];
156
    }
157
158
    /**
159
     * キャラクタIDを取得
160
     *
161
     * @return int
162
     */
163
    public function getCharacter()
164
    {
165
        return $this->parameters['t'];
166
    }
167
168
    /**
169
     * ユーザ関連情報管理クラスを取得
170
     *
171
     * @return UserInformation
172
     */
173
    public function getUserInformation()
174
    {
175
        if (!$this->user_info) {
176
            $this->user_info = new UserInformation();
177
        }
178
        return $this->user_info;
179
    }
180
181
    /**
182
     * ユーザ入力テキストを設定
183
     *
184
     * @param   string  $value          テキスト
185
     * @return  self
186
     * @throws  DomainError
187
     */
188 View Code Duplication
    public function setUserInput($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        validators\Text::validate($value, 255, 'User input too long');
191
        $value = mb_substr($value, 0, 255, 'UTF-8');
192
        $this->parameters['utt'] = $value;
193
        return $this;
194
    }
195
196
    /**
197
     * コンテキストIDを設定
198
     *
199
     * @param   string  $value          コンテキストID
200
     * @return  self
201
     */
202 View Code Duplication
    public function setContext($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204
        validators\Text::validate($value, 255, 'Context id too long');
205
        $value = mb_substr($value, 0, 255, 'UTF-8');
206
        $this->parameters['context'] = $value;
207
        return $this;
208
    }
209
210
    /**
211
     * 対話モードを設定
212
     *
213
     * @param   string  $value          対話モード  MODE_DIALOG | MODE_SRTR
214
     * @return  self
215
     */
216 View Code Duplication
    public function setMode($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
        if ($value !== null &&
219
           $value !== self::MODE_DIALOG &&
220
           $value !== self::MODE_SRTR) {
221
            throw new DomainError('Invalid mode');
222
        }
223
        $this->parameters['mode'] = $value;
224
        return $this;
225
    }
226
227
    /**
228
     * キャラクタIDを設定
229
     *
230
     * @param   int     $value          キャラクタID    CHARACTER_DEFAULT | CHARACTER_KANSAI | CHARACTER_BABY
231
     * @return  self
232
     */
233 View Code Duplication
    public function setCharacter($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        if ($value !== self::CHARACTER_DEFAULT &&
236
           $value !== self::CHARACTER_KANSAI &&
237
           $value !== self::CHARACTER_BABY) {
238
            throw new DomainError('Invalid character');
239
        }
240
        $this->parameters['t'] = $value;
241
        return $this;
242
    }
243
244
    /**
245
     * ユーザ情報を設定
246
     *
247
     * @param   UserInformation $value      ユーザ情報
248
     * @return  self
249
     */
250
    public function setUserInformation(UserInformation $value)
251
    {
252
        $this->user_info = $value;
253
        return $this;
254
    }
255
256
    /**
257
     * 送信用のパラメータ配列を作成して取得する
258
     *
259
     * @return  array
260
     */
261
    public function makeParameter()
262
    {
263
        $ret = [];
264
        foreach ($this->parameters as $k => $v) {
265
            if ($v !== null) {
266
                $ret[$k] = $v;
267
            }
268
        }
269
        return array_merge($ret, $this->getUserInformation()->makeParameter());
270
    }
271
272
    /**
273
     * クラス名(FQCN)を取得
274
     *
275
     * return string
276
     */
277
    public static function className()
278
    {
279
        return get_called_class();
280
    }
281
}
282