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) |
||
| 49 | { |
||
| 50 | $regex = '/^[0-9]{4}$/'; |
||
| 51 | return self::_check($check, $regex); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * 半角英字チェック |
||
| 56 | * |
||
| 57 | * @access public |
||
| 58 | * @author sakuragawa |
||
| 59 | * @param string $check |
||
| 60 | * @return boolean |
||
| 61 | */ |
||
| 62 | public static function alpha($check) |
||
| 63 | { |
||
| 64 | $regex = '/^[a-zA-Z]+$/u'; |
||
| 65 | return self::_check($check, $regex); |
||
| 66 | } |
||
| 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) |
||
| 80 | { |
||
| 81 | //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす |
||
| 82 | if (is_array($limit) && isset($limit['providers'])) { |
||
| 83 | $limit = 2147483647; |
||
| 84 | } |
||
| 85 | |||
| 86 | //coreのチェックを先に行う |
||
| 87 | if (!parent::numeric($check)) { |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | return abs($check) <= $limit; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * naturalNumber |
||
| 95 | * 数値チェック |
||
| 96 | * integerなどの上限チェックを同時に行う |
||
| 97 | * |
||
| 98 | * @access public |
||
| 99 | * @author hagiwara |
||
| 100 | * @param string $check |
||
| 101 | * @param boolean $allowZero |
||
| 102 | * @param integer $limit |
||
| 103 | * @return boolean |
||
| 104 | */ |
||
| 105 | public static function naturalNumber($check, $allowZero = false, $limit = 2147483647) |
||
| 106 | { |
||
| 107 | //providersが間違いなく$contextの内容と考えられるので初期値を入力しなおす |
||
| 108 | if (is_array($allowZero) && isset($allowZero['providers'])) { |
||
| 109 | $allowZero = false; |
||
| 110 | } |
||
| 111 | if (is_array($limit) && isset($limit['providers'])) { |
||
| 112 | $limit = 2147483647; |
||
| 113 | } |
||
| 114 | |||
| 115 | //coreのチェックを先に行う |
||
| 116 | if (!parent::naturalNumber($check, $allowZero)) { |
||
|
|
|||
| 117 | return false; |
||
| 118 | } |
||
| 119 | return abs($check) <= $limit; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * hiraganaOnly |
||
| 124 | * 全角ひらがな以外が含まれていればエラーとするバリデーションチェック |
||
| 125 | * 全角ダッシュ「ー」のみ必要と考えられるので追加 |
||
| 126 | * Japanese HIRAGANA Validation |
||
| 127 | * @param string $check |
||
| 128 | * @return boolean |
||
| 129 | * https://github.com/ichikaway/cakeplus |
||
| 130 | */ |
||
| 131 | public static function hiraganaOnly($check) |
||
| 132 | { |
||
| 133 | $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc))*$/'; |
||
| 134 | return self::_check($check, $regex); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * hiraganaSpaceOnly |
||
| 139 | * 全角ひらがな以外にスペースもOKとするバリデーション |
||
| 140 | * |
||
| 141 | * @param string $check |
||
| 142 | * @return boolean |
||
| 143 | */ |
||
| 144 | public static function hiraganaSpaceOnly($check) |
||
| 145 | { |
||
| 146 | $regex = '/^(\xe3(\x81[\x81-\xbf]|\x82[\x80-\x93]|\x83\xbc)| )*$/'; |
||
| 147 | return self::_check($check, $regex); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * katakanaOnly |
||
| 152 | * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック |
||
| 153 | * Japanese KATAKANA Validation |
||
| 154 | * |
||
| 155 | * @param string $check |
||
| 156 | * @return boolean |
||
| 157 | * https://github.com/ichikaway/cakeplus |
||
| 158 | */ |
||
| 159 | public static function katakanaOnly($check) |
||
| 160 | { |
||
| 161 | //\xe3\x82\x9b 濁点゛ |
||
| 162 | //\xe3\x82\x9c 半濁点゜ |
||
| 163 | $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c))*$/'; |
||
| 164 | return self::_check($check, $regex); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * katakanaSpaceOnly |
||
| 169 | * 全角カタナカ以外にスペースもOKとするバリデーション |
||
| 170 | * |
||
| 171 | * @param string $check |
||
| 172 | * @return boolean |
||
| 173 | */ |
||
| 174 | public static function katakanaSpaceOnly($check) |
||
| 175 | { |
||
| 176 | $regex = '/^(\xe3(\x82[\xa1-\xbf]|\x83[\x80-\xb6]|\x83\xbc|\x82\x9b|\x82\x9c)| )*$/'; |
||
| 177 | return self::_check($check, $regex); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * zenkakuOnly |
||
| 182 | * マルチバイト文字以外が含まれていればエラーとするバリデーションチェック |
||
| 183 | * Japanese ZENKAKU Validation |
||
| 184 | * |
||
| 185 | * @param string $check |
||
| 186 | * @return boolean |
||
| 187 | * https://github.com/ichikaway/cakeplus |
||
| 188 | */ |
||
| 189 | public static function zenkakuOnly($check) |
||
| 190 | { |
||
| 191 | $regex = '/(?:\xEF\xBD[\xA1-\xBF]|\xEF\xBE[\x80-\x9F])|[\x20-\x7E]/'; |
||
| 192 | return !self::_check($check, $regex); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * spaceOnly |
||
| 197 | * 全角、半角スペースのみであればエラーとするバリデーションチェック |
||
| 198 | * Japanese Space only validation |
||
| 199 | * |
||
| 200 | * @param string $check |
||
| 201 | * @return boolean |
||
| 202 | * https://github.com/ichikaway/cakeplus |
||
| 203 | */ |
||
| 204 | public static function spaceOnly($check) |
||
| 205 | { |
||
| 206 | $regex = '/^(\s| )+$/'; |
||
| 207 | return !self::_check($check, $regex); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * hankakukatakanaOnly |
||
| 212 | * 半角カタカナ以外が含まれていればエラーとするバリデーションチェック |
||
| 213 | * Japanese HANKAKU KATAKANA Validation |
||
| 214 | * http://ash.jp/code/unitbl1.htm |
||
| 215 | * @param string $check |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | public static function hankakukatakanaOnly($check) |
||
| 219 | { |
||
| 220 | $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F])*$/'; |
||
| 221 | return self::_check($check, $regex); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * hankakukatakanaSpaceOnly |
||
| 226 | * 半角カタカナ以外にも半角スペースもOKとするバリデーション |
||
| 227 | * Japanese HANKAKU KATAKANA SPACE Validation |
||
| 228 | * http://ash.jp/code/unitbl1.htm |
||
| 229 | * @param string $check |
||
| 230 | * @return boolean |
||
| 231 | */ |
||
| 232 | public static function hankakukatakanaSpaceOnly($check) |
||
| 233 | { |
||
| 234 | $regex = '/^(?:\xEF\xBD[\xA6-\xBF]|\xEF\xBE[\x80-\x9F]|\x20)*$/'; |
||
| 235 | return self::_check($check, $regex); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * phone |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * @author hayasaki |
||
| 243 | * @param string $check |
||
| 244 | * @return boolean |
||
| 245 | */ |
||
| 246 | public static function phone($check) |
||
| 247 | { |
||
| 248 | $regex = '/^[0-9]{2,5}-?[0-9]{1,4}-?[0-9]{4}$/'; |
||
| 249 | return self::_check($check, $regex); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * phone1 |
||
| 254 | * 市外局番範囲は2~5桁 |
||
| 255 | * |
||
| 256 | * @access public |
||
| 257 | * @author hayasaki |
||
| 258 | * @param string $check |
||
| 259 | * @return boolean |
||
| 260 | */ |
||
| 261 | public static function phone1($check) |
||
| 262 | { |
||
| 263 | $regex = '/^[0-9]{2,5}$/'; |
||
| 264 | return self::_check($check, $regex); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * phone2 |
||
| 269 | * 範囲は2~4桁 |
||
| 270 | * |
||
| 271 | * @access public |
||
| 272 | * @author hayasaki |
||
| 273 | * @param string $check |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | public static function phone2($check) |
||
| 277 | { |
||
| 278 | $regex = '/^[0-9]{2,4}$/'; |
||
| 279 | return self::_check($check, $regex); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * phone3 |
||
| 284 | * 範囲は4桁固定 |
||
| 285 | * |
||
| 286 | * @access public |
||
| 287 | * @author hayasaki |
||
| 288 | * @param string $check |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | public static function phone3($check) |
||
| 292 | { |
||
| 293 | $regex = '/^[0-9]{4}$/'; |
||
| 294 | return self::_check($check, $regex); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * emailNonRfc |
||
| 299 | * メールアドレスチェック(RFC非準拠) |
||
| 300 | * |
||
| 301 | * @access public |
||
| 302 | * @author fantasista21jp |
||
| 303 | * @param string $check |
||
| 304 | * @return boolean |
||
| 305 | */ |
||
| 306 | public static function emailNonRfc($check) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * datetimeComparison |
||
| 314 | * 日時比較チェック |
||
| 315 | * |
||
| 316 | * @access public |
||
| 317 | * @author fantasista21jp |
||
| 318 | * @param $check1 |
||
| 319 | * @param $operator |
||
| 320 | * @param $check2 |
||
| 321 | * @param $context |
||
| 322 | * @return boolean |
||
| 323 | */ |
||
| 324 | public static function datetimeComparison($check1, $operator, $check2, $context) |
||
| 325 | { |
||
| 326 | $date1 = $check1; |
||
| 327 | $date2 = $context['data'][$check2]; |
||
| 328 | |||
| 329 | if (empty($date1) || empty($date2)) { |
||
| 381 | } |
||
| 382 |
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.