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 |
||
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() |
||
48 | |||
49 | /** |
||
50 | * マジックメソッド __toString |
||
51 | * |
||
52 | * デバッグ等で表示する際に中身がわかるように JSON で返すだけで、 |
||
53 | * 表現そのものに意味はないし依存してはならない |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | public function __toString() |
||
61 | |||
62 | /** |
||
63 | * マジックメソッド __get |
||
64 | * |
||
65 | * @param string $key プロパティ取得用のキー |
||
66 | * @return string キーに対応する値 |
||
67 | */ |
||
68 | public function __get($key) |
||
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) |
||
110 | |||
111 | /** |
||
112 | * パラメータを完全にリセットする |
||
113 | * |
||
114 | * return self |
||
115 | */ |
||
116 | public function reset() |
||
127 | |||
128 | /** |
||
129 | * ユーザ入力テキストを取得 |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getUserInput() |
||
137 | |||
138 | /** |
||
139 | * コンテキストIDを取得 |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getContext() |
||
147 | |||
148 | /** |
||
149 | * 対話モードを取得 |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getMode() |
||
157 | |||
158 | /** |
||
159 | * キャラクタIDを取得 |
||
160 | * |
||
161 | * @return int |
||
162 | */ |
||
163 | public function getCharacter() |
||
167 | |||
168 | /** |
||
169 | * ユーザ関連情報管理クラスを取得 |
||
170 | * |
||
171 | * @return UserInformation |
||
172 | */ |
||
173 | public function getUserInformation() |
||
180 | |||
181 | /** |
||
182 | * ユーザ入力テキストを設定 |
||
183 | * |
||
184 | * @param string $value テキスト |
||
185 | * @return self |
||
186 | * @throws DomainError |
||
187 | */ |
||
188 | View Code Duplication | public function setUserInput($value) |
|
195 | |||
196 | /** |
||
197 | * コンテキストIDを設定 |
||
198 | * |
||
199 | * @param string $value コンテキストID |
||
200 | * @return self |
||
201 | */ |
||
202 | View Code Duplication | public function setContext($value) |
|
209 | |||
210 | /** |
||
211 | * 対話モードを設定 |
||
212 | * |
||
213 | * @param string $value 対話モード MODE_DIALOG | MODE_SRTR |
||
214 | * @return self |
||
215 | */ |
||
216 | View Code Duplication | public function setMode($value) |
|
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) |
|
243 | |||
244 | /** |
||
245 | * ユーザ情報を設定 |
||
246 | * |
||
247 | * @param UserInformation $value ユーザ情報 |
||
248 | * @return self |
||
249 | */ |
||
250 | public function setUserInformation(UserInformation $value) |
||
255 | |||
256 | /** |
||
257 | * 送信用のパラメータ配列を作成して取得する |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | public function makeParameter() |
||
271 | |||
272 | /** |
||
273 | * クラス名(FQCN)を取得 |
||
274 | * |
||
275 | * return string |
||
276 | */ |
||
277 | public static function className() |
||
281 | } |
||
282 |
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.