GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Validation   F
last analyzed

Complexity

Total Complexity 76

Size/Duplication

Total Lines 706
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 706
rs 2.32
c 0
b 0
f 0
wmc 76

39 Methods

Rating   Name   Duplication   Size   Complexity  
A isListed() 0 8 2
A isLess() 0 3 2
A isAlphaNumeric() 0 3 1
A isGreaterEqual() 0 3 2
A isFloat() 0 3 1
A isDecimal() 0 3 1
A isAlphaNumericSpaces() 0 3 1
C isPassword() 0 44 15
A isDimension() 0 15 2
A isNaturalNoZero() 0 3 2
A isRequired() 0 7 3
A isLessEqual() 0 3 2
A isAlpha() 0 3 1
A isAlphaUnderscoreDash() 0 3 1
A isMsisdn() 0 3 1
A isMaxLength() 0 7 2
A isBool() 0 3 1
A isExactLength() 0 7 2
A isMinLength() 0 11 4
A isIpv4() 0 3 1
A isAlphaDash() 0 3 1
A isInteger() 0 3 1
A isDomain() 0 5 3
A isUrl() 0 15 4
A isNumeric() 0 3 1
A isDate() 0 5 2
A isAlphaUnderscore() 0 3 1
A isIpv6() 0 3 1
A isMatch() 0 7 2
A isOptional() 0 3 1
A isGreater() 0 3 2
A isEmail() 0 3 1
A isNatural() 0 3 1
A isBase64() 0 3 1
A isValidIp() 0 15 3
A isAlphaSpaces() 0 3 1
A isRegexMatch() 0 3 1
A isMd5() 0 3 1
A isAscii() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Validation 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.

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 Validation, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Security\Filters;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class Validation
20
 *
21
 * @package O2System\Security\Filters
22
 */
23
class Validation
24
{
25
    /**
26
     * Validation::isOptional
27
     *
28
     * @return bool
29
     */
30
    public static function isOptional()
31
    {
32
        return true;
33
    }
34
35
    // ------------------------------------------------------------------------
36
37
    /**
38
     * Validation::isRequired
39
     *
40
     * @param $string
41
     *
42
     * @return bool
43
     */
44
    public static function isRequired($string = null)
45
    {
46
        if (empty($string) OR strlen($string) == 0) {
47
            return false;
48
        }
49
50
        return true;
51
    }
52
53
    // ------------------------------------------------------------------------
54
55
    /**
56
     * Validation::isMatch
57
     *
58
     * @param $string
59
     * @param $match
60
     *
61
     * @return bool
62
     */
63
    public static function isMatch($string, $match)
64
    {
65
        if ($string === $match) {
66
            return true;
67
        }
68
69
        return false;
70
    }
71
72
    // ------------------------------------------------------------------------
73
74
    /**
75
     * Validation::isRegexMatch
76
     *
77
     * Performs a Regular Expression match test.
78
     *
79
     * @param    string
80
     * @param    string    regex
0 ignored issues
show
Bug introduced by
The type O2System\Security\Filters\regex was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
     *
82
     * @return    bool
83
     */
84
    public static function isRegexMatch($string, $regex)
85
    {
86
        return (bool)preg_match($regex, $string);
87
    }
88
89
    // ------------------------------------------------------------------------
90
91
    /**
92
     * Validation::isFloat
93
     *
94
     * @param $string
95
     *
96
     * @return mixed
97
     */
98
    public static function isFloat($string)
99
    {
100
        return filter_var($string, FILTER_VALIDATE_FLOAT);
101
    }
102
103
    // ------------------------------------------------------------------------
104
105
    /**
106
     * Validation::isMaxLength
107
     *
108
     * Max Length
109
     *
110
     * @param    string
111
     * @param    string
112
     *
113
     * @return    bool
114
     */
115
    public static function isMaxLength($string, $length)
116
    {
117
        if ( ! is_numeric($length)) {
118
            return false;
119
        }
120
121
        return ($length >= mb_strlen($string));
122
    }
123
124
    // --------------------------------------------------------------------
125
126
    /**
127
     * Validation::isExactLength
128
     *
129
     * Exact Length
130
     *
131
     * @param    string
132
     * @param    string
133
     *
134
     * @return    bool
135
     */
136
    public static function isExactLength($string, $length)
137
    {
138
        if ( ! is_numeric($length)) {
139
            return false;
140
        }
141
142
        return (mb_strlen($string) === (int)$length);
143
    }
144
145
    // ------------------------------------------------------------------------
146
147
    /**
148
     * Validation::isDimension
149
     *
150
     * @param        $string
151
     * @param string $format
152
     *
153
     * @return bool
154
     */
155
    public static function isDimension($string, $format = 'W x H x L')
156
    {
157
        $string = strtolower($string);
158
        $string = preg_replace('/\s+/', '', $string);
159
        $x_string = explode('x', $string);
160
161
        $format = strtolower($format);
162
        $format = preg_replace('/\s+/', '', $format);
163
        $x_format = explode('x', $format);
164
165
        if (count($x_string) == count($x_format)) {
166
            return true;
167
        }
168
169
        return false;
170
    }
171
172
    // ------------------------------------------------------------------------
173
174
    /**
175
     * Validation::isIpv4
176
     *
177
     * @param $string
178
     *
179
     * @return mixed
180
     */
181
    public static function isIpv4($string)
182
    {
183
        return filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
184
    }
185
186
    // ------------------------------------------------------------------------
187
188
    /**
189
     * Validation::isIpv6
190
     *
191
     * @param $string
192
     *
193
     * @return mixed
194
     */
195
    public static function isIpv6($string)
196
    {
197
        return filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
198
    }
199
200
    // ------------------------------------------------------------------------
201
202
    /**
203
     * Validation::isUrl
204
     *
205
     * @param $string
206
     *
207
     * @return bool|mixed
208
     */
209
    public static function isUrl($string)
210
    {
211
        if (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $string, $matches)) {
212
            if (empty($matches[ 2 ])) {
213
                return false;
214
            } elseif ( ! in_array($matches[ 1 ], ['http', 'https'], true)) {
215
                return false;
216
            }
217
218
            $string = $matches[ 2 ];
219
        }
220
221
        $string = 'http://' . $string;
222
223
        return filter_var($string, FILTER_VALIDATE_URL);
224
    }
225
226
    // ------------------------------------------------------------------------
227
228
    /**
229
     * Validation::isEmail
230
     *
231
     * @param $string
232
     *
233
     * @return bool
234
     */
235
    public static function isEmail($string)
236
    {
237
        return (bool)filter_var($string, FILTER_VALIDATE_EMAIL);
238
    }
239
240
    // ------------------------------------------------------------------------
241
242
    /**
243
     * Validation::isDomain
244
     *
245
     * @param $string
246
     *
247
     * @return bool
248
     */
249
    public static function isDomain($string)
250
    {
251
        return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $string) //valid chars check
252
            && preg_match("/^.{1,253}$/", $string) //overall length check
253
            && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $string)); //length of each label
254
    }
255
256
    // ------------------------------------------------------------------------
257
258
    /**
259
     * Validation::isBool
260
     *
261
     * @param $string
262
     *
263
     * @return bool
264
     */
265
    public static function isBool($string)
266
    {
267
        return (bool)filter_var($string, FILTER_VALIDATE_BOOLEAN);
268
    }
269
270
    // --------------------------------------------------------------------
271
272
    /**
273
     * Validation::isAlpha
274
     *
275
     * @param    string $string
276
     *
277
     * @return    bool
278
     */
279
    public static function isAlpha($string)
280
    {
281
        return (bool)ctype_alpha($string);
282
    }
283
284
    // ------------------------------------------------------------------------
285
286
    /**
287
     * Validation::isAlphaSpaces
288
     *
289
     * @param    string $string
290
     *
291
     * @return    bool
292
     */
293
    public static function isAlphaSpaces($string)
294
    {
295
        return (bool)preg_match('/^[A-Z ]+$/i', $string);
296
    }
297
298
    // --------------------------------------------------------------------
299
300
    /**
301
     * Alpha-numeric
302
     *
303
     * @param    string $string
304
     *
305
     * @return    bool
306
     */
307
    public static function isAlphaNumeric($string)
308
    {
309
        return (bool)ctype_alnum((string)$string);
310
    }
311
312
    // --------------------------------------------------------------------
313
314
    /**
315
     * Validation::isAlphaNumericSpaces
316
     *
317
     * Alpha-numeric w/ spaces
318
     *
319
     * @param    string $string
320
     *
321
     * @return    bool
322
     */
323
    public static function isAlphaNumericSpaces($string)
324
    {
325
        return (bool)preg_match('/^[A-Z0-9 ]+$/i', $string);
326
    }
327
328
    /**
329
     * Validation::isAlphaDash
330
     *
331
     * Alpha-numeric with underscores and dashes
332
     *
333
     * @param    string
334
     *
335
     * @return    bool
336
     */
337
    public static function isAlphaDash($string)
338
    {
339
        return (bool)preg_match('/^[a-z0-9-]+$/i', $string);
340
    }
341
342
    // ------------------------------------------------------------------------
343
344
    /**
345
     * Validation::isAlphaUnderscore
346
     *
347
     * Alpha-numeric with underscores and dashes
348
     *
349
     * @param    string
350
     *
351
     * @return    bool
352
     */
353
    public static function isAlphaUnderscore($string)
354
    {
355
        return (bool)preg_match('/^[a-z0-9_]+$/i', $string);
356
    }
357
358
    // ------------------------------------------------------------------------
359
360
    /**
361
     * Validation::isAlphaUnderscoreDash
362
     *
363
     * Alpha-numeric with underscores and dashes
364
     *
365
     * @param    string
366
     *
367
     * @return    bool
368
     */
369
    public static function isAlphaUnderscoreDash($string)
370
    {
371
        return (bool)preg_match('/^[a-z0-9_-]+$/i', $string);
372
    }
373
374
    // ------------------------------------------------------------------------
375
376
    /**
377
     * Validation::isNumeric
378
     *
379
     * @param    string
380
     *
381
     * @return    bool
382
     */
383
    public static function isNumeric($str)
384
    {
385
        return (bool)preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
386
387
    }
388
389
    // ------------------------------------------------------------------------
390
391
    /**
392
     * Validation::isInteger
393
     *
394
     * @param    string
395
     *
396
     * @return    bool
397
     */
398
    public static function isInteger($str)
399
    {
400
        return (bool)preg_match('/^[\-+]?[0-9]+$/', $str);
401
    }
402
403
    // ------------------------------------------------------------------------
404
405
    /**
406
     * Validation::isDecimal
407
     *
408
     * Decimal number
409
     *
410
     * @param    string
411
     *
412
     * @return    bool
413
     */
414
    public static function isDecimal($string)
415
    {
416
        return (bool)preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $string);
417
    }
418
419
    // ------------------------------------------------------------------------
420
421
    /**
422
     * Validation::isGreater
423
     *
424
     * Greater than
425
     *
426
     * @param    string
427
     * @param    int
428
     *
429
     * @return    bool
430
     */
431
    public static function isGreater($string, $min)
432
    {
433
        return is_numeric($string) ? ($string > $min) : false;
434
    }
435
436
    // ------------------------------------------------------------------------
437
438
    /**
439
     * Validation::isGreaterEqual
440
     *
441
     * Equal to or Greater than
442
     *
443
     * @param    string
444
     * @param    int
445
     *
446
     * @return    bool
447
     */
448
    public static function isGreaterEqual($string, $min)
449
    {
450
        return (bool)is_numeric($string) ? ($string >= $min) : false;
451
    }
452
453
    // --------------------------------------------------------------------
454
455
    /**
456
     * Validation::isLess
457
     *
458
     * Less than
459
     *
460
     * @param    string
461
     * @param    int
462
     *
463
     * @return    bool
464
     */
465
    public static function isLess($string, $max)
466
    {
467
        return (bool)is_numeric($string) ? ($string < $max) : false;
468
    }
469
470
    // --------------------------------------------------------------------
471
472
    /**
473
     * Validation::isLessEqual
474
     *
475
     * Equal to or Less than
476
     *
477
     * @param    string
478
     * @param    int
479
     *
480
     * @return    bool
481
     */
482
    public static function isLessEqual($string, $max)
483
    {
484
        return (bool)is_numeric($string) ? ($string <= $max) : false;
485
    }
486
487
    // --------------------------------------------------------------------
488
489
    /**
490
     * Validation::isListed
491
     *
492
     * Value should be within an array of values
493
     *
494
     * @param    string
495
     * @param    string
496
     *
497
     * @return    bool
498
     */
499
    public static function isListed($string, $list)
500
    {
501
        if (is_string($list)) {
502
            $list = explode(',', $list);
503
            $list = array_map('trim', $list);
504
        }
505
506
        return (bool)in_array($string, $list, true);
507
    }
508
509
    // --------------------------------------------------------------------
510
511
    /**
512
     * Validation::isNatural
513
     *
514
     * Is a Natural number  (0,1,2,3, etc.)
515
     *
516
     * @param    string
517
     *
518
     * @return    bool
519
     */
520
    public static function isNatural($string)
521
    {
522
        return (bool)ctype_digit((string)$string);
523
    }
524
525
    // --------------------------------------------------------------------
526
527
    /**
528
     * Validation::isNaturalNoZero
529
     *
530
     * Is a Natural number, but not a zero  (1,2,3, etc.)
531
     *
532
     * @param    string
533
     *
534
     * @return    bool
535
     */
536
    public static function isNaturalNoZero($string)
537
    {
538
        return (bool)($string != 0 && ctype_digit((string)$string));
539
    }
540
541
    // --------------------------------------------------------------------
542
543
    /**
544
     * Validation::isMd5
545
     *
546
     * @param $string
547
     *
548
     * @return int
549
     */
550
    public static function isMd5($string)
551
    {
552
        return preg_match('/^[a-f0-9]{32}$/i', $string);
553
    }
554
555
    // --------------------------------------------------------------------
556
557
    /**
558
     * Validation::isMsisdn
559
     *
560
     * @param        $string
561
     * @param string $leading
562
     *
563
     * @return bool
564
     */
565
    public static function isMsisdn($string, $leading = '62')
566
    {
567
        return (bool)preg_match('/^(' . $leading . '[1-9]{1}[0-9]{1,2})[0-9]{6,8}$/', $string);
568
    }
569
570
    // --------------------------------------------------------------------
571
572
    /**
573
     * Validation::isDate
574
     *
575
     * @param        $string
576
     * @param string $format
577
     *
578
     * @return bool
579
     */
580
    public static function isDate($string, $format = 'Y-m-d')
581
    {
582
        $dateTime = \DateTime::createFromFormat($format, $string);
583
584
        return (bool)$dateTime !== false && ! array_sum($dateTime->getLastErrors());
585
    }
586
587
    // --------------------------------------------------------------------
588
589
    /**
590
     * Validation::isPassword
591
     *
592
     * @param string $string
593
     * @param int    $length
594
     * @param string $format
595
     *
596
     * @return bool
597
     */
598
    public static function isPassword($string, $length = 8, $format = 'uppercase, lowercase, number, special')
599
    {
600
        // Length
601
        if (self::isMinLength($string, $length) === false) {
602
            return false;
603
        }
604
605
        $format = strtolower($format);
606
        $format = explode(',', $format);
607
        $format = array_map('trim', $format);
608
        $valid = [];
609
610
        foreach ($format as $type) {
611
            switch ($type) {
612
                case 'uppercase':
613
                    if (preg_match_all('/[A-Z]/', $string, $uppercase)) {
614
                        $valid[ $type ] = count($uppercase[ 0 ]);
615
                    }
616
                    break;
617
                case 'lowercase':
618
                    if (preg_match_all('/[a-z]/', $string, $lowercase)) {
619
                        $valid[ $type ] = count($lowercase[ 0 ]);
620
                    }
621
                    break;
622
                case 'number':
623
                case 'numbers':
624
                    if (preg_match_all('/[0-9]/', $string, $numbers)) {
625
                        $valid[ $type ] = count($numbers[ 0 ]);
626
                    }
627
                    break;
628
                case 'special character':
629
                case 'special-character':
630
                case 'special':
631
                    // Special Characters
632
                    if (preg_match_all('/[!@#$%^&*()\-_=+{};:,<.>]/', $string, $special)) {
633
                        $valid[ $type ] = count($special[ 0 ]);
634
                    }
635
                    break;
636
            }
637
        }
638
639
        $diff = array_diff($format, array_keys($valid));
640
641
        return empty($diff) ? true : false;
642
    }
643
644
    // --------------------------------------------------------------------
645
646
    /**
647
     * Validation::isMinLength
648
     *
649
     * Minimum Length
650
     *
651
     * @param    string
652
     * @param    string
653
     *
654
     * @return    bool
655
     */
656
    public static function isMinLength($string, $length)
657
    {
658
        if (isset($string)) {
659
            if ( ! is_numeric($length) or $length == 0) {
660
                return false;
661
            }
662
663
            return ($length <= mb_strlen($string));
664
        }
665
666
        return false;
667
    }
668
669
    // --------------------------------------------------------------------
670
671
    /**
672
     * Validation::isAscii
673
     *
674
     * Chceks if a string is an v4 or v6 ip address.
675
     *
676
     * @param   string $string String to check
677
     * @param   string $which  ipv4 | ipv6
678
     *
679
     * @return  bool
680
     */
681
    public static function isValidIp($string, $which = null)
682
    {
683
        switch (strtolower($which)) {
684
            case 'ipv4':
685
                $which = FILTER_FLAG_IPV4;
686
                break;
687
            case 'ipv6':
688
                $which = FILTER_FLAG_IPV6;
689
                break;
690
            default:
691
                $which = null;
692
                break;
693
        }
694
695
        return (bool)filter_var($string, FILTER_VALIDATE_IP, $which);
696
    }
697
698
    // --------------------------------------------------------------------
699
700
    /**
701
     * Validation::isAscii
702
     *
703
     * Tests if a string is standard 7-bit ASCII or not.
704
     *
705
     * @param   string $string String to check
706
     *
707
     * @return  bool
708
     */
709
    public static function isAscii($string)
710
    {
711
        return (bool)(preg_match('/[^\x00-\x7F]/S', $string) === 0);
712
    }
713
714
    // --------------------------------------------------------------------
715
716
    /**
717
     * Validation::isBase64
718
     *
719
     * Tests a string for characters outside of the Base64 alphabet
720
     * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
721
     *
722
     * @param    string
723
     *
724
     * @return    bool
725
     */
726
    public function isBase64($string)
727
    {
728
        return (bool)(base64_encode(base64_decode($string)) === $string);
729
    }
730
}