Complex classes like ApollonValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApollonValidation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class ApollonValidation extends Validation |
||
8 | { |
||
9 | /** |
||
10 | * zip |
||
11 | * 郵便番号チェック 1カラム |
||
12 | * |
||
13 | * @access public |
||
14 | * @author hagiwara |
||
15 | * @param string $check |
||
16 | * @return boolean |
||
17 | */ |
||
18 | public static function zip($check) |
||
19 | { |
||
20 | $regex = '/^[0-9]{3}-?[0-9]{4}$/'; |
||
21 | return self::_check($check, $regex); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * zip1 |
||
26 | * 郵便番号チェック 上3桁 |
||
27 | * |
||
28 | * @access public |
||
29 | * @author hagiwara |
||
30 | * @param string $check |
||
31 | * @return boolean |
||
32 | */ |
||
33 | public static function zip1($check) |
||
34 | { |
||
35 | $regex = '/^[0-9]{3}$/'; |
||
36 | return self::_check($check, $regex); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * zip2 |
||
41 | * 郵便番号チェック 下4桁 |
||
42 | * |
||
43 | * @access public |
||
44 | * @author hagiwara |
||
45 | * @param string $check |
||
46 | * @return boolean |
||
47 | */ |
||
48 | public static function zip2($check) |
||
53 | |||
54 | /** |
||
55 | * 半角英字チェック |
||
56 | * |
||
57 | * @access public |
||
58 | * @author sakuragawa |
||
59 | * @param string $check |
||
60 | * @return boolean |
||
61 | */ |
||
62 | public static function alpha($check) |
||
67 | |||
68 | /** |
||
69 | * numeric |
||
70 | * 数値チェック |
||
71 | * integerなどの上限チェックを同時に行う |
||
72 | * |
||
73 | * @access public |
||
74 | * @author hagiwara |
||
75 | * @param string $check |
||
76 | * @param integer $limit |
||
77 | * @return boolean |
||
78 | */ |
||
79 | public static function numeric($check, $limit = 2147483647) |
||
92 | |||
93 | /** |
||
94 | * alphaNumericJp |
||
95 | * 半角英数チェック |
||
96 | * CoreのalphaNumericは日本語を通過させてしまうため、上書き |
||
97 | * @access public |
||
98 | * @author ito |
||
99 | * @param string $check |
||
100 | * @return boolean |
||
101 | */ |
||
102 | public static function alphaNumericJp($check) |
||
103 | { |
||
104 | $regex = '/^[a-zA-Z0-9]+$/u'; |
||
105 | return (bool) self::_check($check, $regex); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * alphaNumericSymbols |
||
110 | * 半角英数記号チェック |
||
111 | * 参考URL:http://defindit.com/ascii.html |
||
112 | * @access public |
||
113 | * @author ito |
||
114 | * @param string $check |
||
115 | * @return boolean |
||
116 | */ |
||
117 | public static function alphaNumericSymbols($check) |
||
131 | |||
132 | /** |
||
133 | * naturalNumber |
||
134 | * 数値チェック |
||
135 | * integerなどの上限チェックを同時に行う |
||
136 | * |
||
137 | * @access public |
||
138 | * @author hagiwara |
||
139 | * @param string $check |
||
140 | * @param boolean $allowZero |
||
141 | * @param integer $limit |
||
142 | * @return boolean |
||
143 | */ |
||
144 | public static function naturalNumber($check, $allowZero = false, $limit = 2147483647) |
||
160 | |||
161 | /** |
||
162 | * hiraganaOnly |
||
163 | * 全角ひらがな以外が含まれていればエラーとするバリデーションチェック |
||
164 | * 全角ダッシュ「ー」のみ必要と考えられるので追加 |
||
165 | * Japanese HIRAGANA Validation |
||
166 | * @param string $check |
||
167 | * @return boolean |
||
168 | * https://github.com/ichikaway/cakeplus |
||
169 | */ |
||
170 | public static function hiraganaOnly($check) |
||
175 | |||
176 | /** |
||
177 | * hiraganaSpaceOnly |
||
178 | * 全角ひらがな以外に全角スペースもOKとするバリデーション |
||
179 | * |
||
180 | * @param string $check |
||
181 | * @return boolean |
||
182 | */ |
||
183 | public static function hiraganaSpaceOnly($check) |
||
188 | |||
189 | /** |
||
190 | * hiraganaAllSpaceOnly |
||
191 | * 全角ひらがな以外に全半角スペースもOKとするバリデーション |
||
192 | * |
||
193 | * @param string $check |
||
194 | * @return boolean |
||
195 | */ |
||
196 | public static function hiraganaAllSpaceOnly($check) |
||
201 | |||
202 | /** |
||
203 | * katakanaOnly |
||
204 | * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック |
||
205 | * Japanese KATAKANA Validation |
||
206 | * |
||
207 | * @param string $check |
||
208 | * @return boolean |
||
209 | * https://github.com/ichikaway/cakeplus |
||
210 | */ |
||
211 | public static function katakanaOnly($check) |
||
218 | |||
219 | /** |
||
220 | * katakanaSpaceOnly |
||
221 | * 全角カタナカ以外に全角スペースもOKとするバリデーション |
||
222 | * |
||
223 | * @param string $check |
||
224 | * @return boolean |
||
225 | */ |
||
226 | public static function katakanaSpaceOnly($check) |
||
231 | |||
232 | /** |
||
233 | * katakanaAllSpaceOnly |
||
234 | * 全角カタナカ以外に全半角スペースもOKとするバリデーション |
||
235 | * |
||
236 | * @param string $check |
||
237 | * @return boolean |
||
238 | */ |
||
239 | public static function katakanaAllSpaceOnly($check) |
||
244 | |||
245 | /** |
||
246 | * zenkakuOnly |
||
247 | * マルチバイト文字以外が含まれていればエラーとするバリデーションチェック |
||
248 | * Japanese ZENKAKU Validation |
||
249 | * |
||
250 | * @param string $check |
||
251 | * @return boolean |
||
252 | * https://github.com/ichikaway/cakeplus |
||
253 | */ |
||
254 | public static function zenkakuOnly($check) |
||
259 | |||
260 | /** |
||
261 | * spaceOnly |
||
262 | * 全角、半角スペースのみであればエラーとするバリデーションチェック |
||
263 | * Japanese Space only validation |
||
264 | * |
||
265 | * @param string $check |
||
266 | * @return boolean |
||
267 | * https://github.com/ichikaway/cakeplus |
||
268 | */ |
||
269 | public static function spaceOnly($check) |
||
274 | |||
275 | /** |
||
276 | * hankakukatakanaOnly |
||
277 | * 半角カタカナ以外が含まれていればエラーとするバリデーションチェック |
||
278 | * Japanese HANKAKU KATAKANA Validation |
||
279 | * http://ash.jp/code/unitbl1.htm |
||
280 | * @param string $check |
||
281 | * @return boolean |
||
282 | */ |
||
283 | public static function hankakukatakanaOnly($check) |
||
288 | |||
289 | /** |
||
290 | * hankakukatakanaSpaceOnly |
||
291 | * 半角カタカナ以外にも半角スペースもOKとするバリデーション |
||
292 | * Japanese HANKAKU KATAKANA SPACE Validation |
||
293 | * http://ash.jp/code/unitbl1.htm |
||
294 | * @param string $check |
||
295 | * @return boolean |
||
296 | */ |
||
297 | public static function hankakukatakanaSpaceOnly($check) |
||
302 | |||
303 | /** |
||
304 | * phone |
||
305 | * |
||
306 | * @access public |
||
307 | * @author hayasaki |
||
308 | * @param string $check |
||
309 | * @return boolean |
||
310 | */ |
||
311 | public static function phone($check) |
||
316 | |||
317 | /** |
||
318 | * phone1 |
||
319 | * 市外局番範囲は2~5桁 |
||
320 | * |
||
321 | * @access public |
||
322 | * @author hayasaki |
||
323 | * @param string $check |
||
324 | * @return boolean |
||
325 | */ |
||
326 | public static function phone1($check) |
||
331 | |||
332 | /** |
||
333 | * phone2 |
||
334 | * 範囲は2~4桁 |
||
335 | * |
||
336 | * @access public |
||
337 | * @author hayasaki |
||
338 | * @param string $check |
||
339 | * @return boolean |
||
340 | */ |
||
341 | public static function phone2($check) |
||
346 | |||
347 | /** |
||
348 | * phone3 |
||
349 | * 範囲は4桁固定 |
||
350 | * |
||
351 | * @access public |
||
352 | * @author hayasaki |
||
353 | * @param string $check |
||
354 | * @return boolean |
||
355 | */ |
||
356 | public static function phone3($check) |
||
361 | |||
362 | /** |
||
363 | * emailNonRfc |
||
364 | * メールアドレスチェック(RFC非準拠) |
||
365 | * |
||
366 | * @access public |
||
367 | * @author fantasista21jp |
||
368 | * @param string $check |
||
369 | * @return boolean |
||
370 | */ |
||
371 | public static function emailNonRfc($check) |
||
376 | |||
377 | /** |
||
378 | * datetimeComparison |
||
379 | * 日時比較チェック |
||
380 | * |
||
381 | * @access public |
||
382 | * @author fantasista21jp |
||
383 | * @param $check1 |
||
384 | * @param $operator |
||
385 | * @param $check2 |
||
386 | * @param $context |
||
387 | * @return boolean |
||
388 | */ |
||
389 | public static function datetimeComparison($check1, $operator, $check2, $context) |
||
440 | } |
||
441 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.