Completed
Push — master ( 644cda...0eeac7 )
by satoru
12s
created

ApollonValidation   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 22
Bugs 0 Features 0
Metric Value
wmc 44
c 22
b 0
f 0
lcom 2
cbo 2
dl 0
loc 375
rs 8.3396

20 Methods

Rating   Name   Duplication   Size   Complexity  
A emailNonRfc() 0 5 1
A zip() 0 5 1
A zip1() 0 5 1
A zip2() 0 5 1
A alpha() 0 5 1
A numeric() 0 13 4
B naturalNumber() 0 16 6
A hiraganaOnly() 0 5 1
A hiraganaSpaceOnly() 0 5 1
A katakanaOnly() 0 7 1
A katakanaSpaceOnly() 0 5 1
A zenkakuOnly() 0 5 1
A spaceOnly() 0 5 1
A hankakukatakanaOnly() 0 5 1
A hankakukatakanaSpaceOnly() 0 5 1
A phone() 0 5 1
A phone1() 0 5 1
A phone2() 0 5 1
A phone3() 0 5 1
C datetimeComparison() 0 57 17

How to fix   Complexity   

Complex Class

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
2
namespace Apollon\Validation;
3
4
use Cake\Validation\Validation;
5
use Cake\Chronos\Chronos;
6
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)) {
0 ignored issues
show
Bug introduced by
It seems like $allowZero can also be of type array<string,null,{"providers":"null"}>; however, Cake\Validation\Validation::naturalNumber() does only seem to accept boolean, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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)
307
    {
308
        $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@' . self::$_pattern['hostname'] . '$/ui';
309
        return self::_check($check, $regex);
310
    }
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)) {
330
            return true;
331
        }
332
333
        if (is_array($date1)) {
334
            $date1 = static::_getDateString($date1);
335
        }
336
        if (is_array($date2)) {
337
            $date2 = static::_getDateString($date2);
338
        }
339
340
        $parseDate1 = Chronos::parse($date1);
341
        $parseDate2 = Chronos::parse($date2);
342
343
        $operator = str_replace([' ', "\t", "\n", "\r", "\0", "\x0B"], '', strtolower($operator));
344
        switch ($operator) {
345
            case 'isgreater':
346
            case '>':
347
                return $parseDate1->gt($parseDate2);
348
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
349
350
            case 'isless':
351
            case '<':
352
                return $parseDate1->lt($parseDate2);
353
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
354
355
            case 'greaterorequal':
356
            case '>=':
357
                return $parseDate1->gte($parseDate2);
358
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
359
360
            case 'lessorequal':
361
            case '<=':
362
                return $parseDate1->lte($parseDate2);
363
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
364
365
            case 'equalto':
366
            case '==':
367
                return $parseDate1->eq($parseDate2);
368
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
369
370
            case 'notequal':
371
            case '!=':
372
                return $parseDate1->ne($parseDate2);
373
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
374
375
            default:
376
                static::$errors[] = 'You must define the $operator parameter for Validation::datetimeComparison()';
377
        }
378
379
        return false;
380
    }
381
}
382