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 |
||
| 6 | class ApollonValidation extends Validation |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * zip |
||
| 10 | * 郵便番号チェック 1カラム |
||
| 11 | * |
||
| 12 | * @access public |
||
| 13 | * @author hagiwara |
||
| 14 | * @param string $check |
||
| 15 | * @return boolean |
||
| 16 | */ |
||
| 17 | public static function zip($check) |
||
| 18 | { |
||
| 19 | $regex = '/^[0-9]{3}-?[0-9]{4}$/'; |
||
| 20 | return self::_check($check, $regex); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * zip1 |
||
| 25 | * 郵便番号チェック 上3桁 |
||
| 26 | * |
||
| 27 | * @access public |
||
| 28 | * @author hagiwara |
||
| 29 | * @param string $check |
||
| 30 | * @return boolean |
||
| 31 | */ |
||
| 32 | public static function zip1($check) |
||
| 33 | { |
||
| 34 | $regex = '/^[0-9]{3}$/'; |
||
| 35 | return self::_check($check, $regex); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * zip2 |
||
| 40 | * 郵便番号チェック 下4桁 |
||
| 41 | * |
||
| 42 | * @access public |
||
| 43 | * @author hagiwara |
||
| 44 | * @param string $check |
||
| 45 | * @return boolean |
||
| 46 | */ |
||
| 47 | public static function zip2($check) |
||
| 48 | { |
||
| 49 | $regex = '/^[0-9]{4}$/'; |
||
| 50 | return self::_check($check, $regex); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * 半角英字チェック |
||
| 55 | * |
||
| 56 | * @access public |
||
| 57 | * @author sakuragawa |
||
| 58 | * @param string $check |
||
| 59 | * @return boolean |
||
| 60 | */ |
||
| 61 | public static function alpha($check) |
||
| 62 | { |
||
| 63 | $regex = '/^[a-zA-Z]+$/u'; |
||
| 64 | return self::_check($check, $regex); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * numeric |
||
| 69 | * 数値チェック |
||
| 70 | * integerなどの上限チェックを同時に行う |
||
| 71 | * |
||
| 72 | * @access public |
||
| 73 | * @author hagiwara |
||
| 74 | * @param string $check |
||
| 75 | * @param integer $limit |
||
| 76 | * @return boolean |
||
| 77 | */ |
||
| 78 | public static function numeric($check, $limit = 2147483647) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * naturalNumber |
||
| 94 | * 数値チェック |
||
| 95 | * integerなどの上限チェックを同時に行う |
||
| 96 | * |
||
| 97 | * @access public |
||
| 98 | * @author hagiwara |
||
| 99 | * @param string $check |
||
| 100 | * @param boolean $allowZero |
||
| 101 | * @param integer $limit |
||
| 102 | * @return boolean |
||
| 103 | */ |
||
| 104 | public static function naturalNumber($check, $allowZero = false, $limit = 2147483647) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * hiraganaOnly |
||
| 123 | * 全角ひらがな以外が含まれていればエラーとするバリデーションチェック |
||
| 124 | * 全角ダッシュ「ー」のみ必要と考えられるので追加 |
||
| 125 | * Japanese HIRAGANA Validation |
||
| 126 | * @param string $check |
||
| 127 | * @return boolean |
||
| 128 | * https://github.com/ichikaway/cakeplus |
||
| 129 | */ |
||
| 130 | public static function hiraganaOnly($check) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * hiraganaSpaceOnly |
||
| 138 | * 全角ひらがな以外にスペースもOKとするバリデーション |
||
| 139 | * |
||
| 140 | * @param string $check |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | public static function hiraganaSpaceOnly($check) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * katakanaOnly |
||
| 151 | * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック |
||
| 152 | * Japanese KATAKANA Validation |
||
| 153 | * |
||
| 154 | * @param string $check |
||
| 155 | * @return boolean |
||
| 156 | * https://github.com/ichikaway/cakeplus |
||
| 157 | */ |
||
| 158 | public static function katakanaOnly($check) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * katakanaSpaceOnly |
||
| 168 | * 全角カタナカ以外にスペースもOKとするバリデーション |
||
| 169 | * |
||
| 170 | * @param string $check |
||
| 171 | * @return boolean |
||
| 172 | */ |
||
| 173 | public static function katakanaSpaceOnly($check) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * zenkakuOnly |
||
| 181 | * マルチバイト文字以外が含まれていればエラーとするバリデーションチェック |
||
| 182 | * Japanese ZENKAKU Validation |
||
| 183 | * |
||
| 184 | * @param string $check |
||
| 185 | * @return boolean |
||
| 186 | * https://github.com/ichikaway/cakeplus |
||
| 187 | */ |
||
| 188 | public static function zenkakuOnly($check) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * spaceOnly |
||
| 196 | * 全角、半角スペースのみであればエラーとするバリデーションチェック |
||
| 197 | * Japanese Space only validation |
||
| 198 | * |
||
| 199 | * @param string $check |
||
| 200 | * @return boolean |
||
| 201 | * https://github.com/ichikaway/cakeplus |
||
| 202 | */ |
||
| 203 | public static function spaceOnly($check) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * hankakukatakanaOnly |
||
| 211 | * 半角カタカナ以外が含まれていればエラーとするバリデーションチェック |
||
| 212 | * Japanese HANKAKU KATAKANA Validation |
||
| 213 | * http://ash.jp/code/unitbl1.htm |
||
| 214 | * @param string $check |
||
| 215 | * @return boolean |
||
| 216 | */ |
||
| 217 | public static function hankakukatakanaOnly($check) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * hankakukatakanaSpaceOnly |
||
| 225 | * 半角カタカナ以外にも半角スペースもOKとするバリデーション |
||
| 226 | * Japanese HANKAKU KATAKANA SPACE Validation |
||
| 227 | * http://ash.jp/code/unitbl1.htm |
||
| 228 | * @param string $check |
||
| 229 | * @return boolean |
||
| 230 | */ |
||
| 231 | public static function hankakukatakanaSpaceOnly($check) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * phone |
||
| 239 | * |
||
| 240 | * @access public |
||
| 241 | * @author hayasaki |
||
| 242 | * @param string $check |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | public static function phone($check) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * phone1 |
||
| 253 | * 市外局番範囲は2~5桁 |
||
| 254 | * |
||
| 255 | * @access public |
||
| 256 | * @author hayasaki |
||
| 257 | * @param string $check |
||
| 258 | * @return boolean |
||
| 259 | */ |
||
| 260 | public static function phone1($check) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * phone2 |
||
| 268 | * 範囲は2~4桁 |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * @author hayasaki |
||
| 272 | * @param string $check |
||
| 273 | * @return boolean |
||
| 274 | */ |
||
| 275 | public static function phone2($check) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * phone3 |
||
| 283 | * 範囲は4桁固定 |
||
| 284 | * |
||
| 285 | * @access public |
||
| 286 | * @author hayasaki |
||
| 287 | * @param string $check |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | public static function phone3($check) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * emailNonRfc |
||
| 298 | * メールアドレスチェック(RFC非準拠) |
||
| 299 | * |
||
| 300 | * @access public |
||
| 301 | * @author fantasista21jp |
||
| 302 | * @param string $check |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public static function emailNonRfc($check) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * datetimeComparison |
||
| 313 | * 日時比較チェック |
||
| 314 | * |
||
| 315 | * @access public |
||
| 316 | * @author fantasista21jp |
||
| 317 | * @param $check1 |
||
| 318 | * @param $operator |
||
| 319 | * @param $check2 |
||
| 320 | * @param $context |
||
| 321 | * @return boolean |
||
| 322 | */ |
||
| 323 | public static function datetimeComparison($check1, $operator, $check2, $context) |
||
| 386 | } |
||
| 387 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.