Completed
Push — master ( 21ca38...9d386c )
by Jacques
11:01
created

Valitron::addrules()   C

Complexity

Conditions 10
Paths 1

Size

Total Lines 79
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 5.5893
c 0
b 0
f 0
cc 10
eloc 38
nc 1
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Validations reworked for use with Valitron.
4
 *
5
 * @author Jacques Marneweck <[email protected]>
6
 * @copyright 2002-2018 Jacques Marneweck.  All rights strictly reserved.
7
 */
8
9
namespace Jacques\Validators;
10
11
use Carbon\Carbon;
12
13
class Valitron
14
{
15
    static public function addrules()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
16
    {
17
        /**
18
         * Validate the possibility of the South African Identity Number beng a valid South
19
         * African identity number.
20
         */
21
        \Valitron\Validator::addRule('za_identity_number', function($field, $value, array $params, array $fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
            if (!ctype_digit($value)) {
23
                return false;
24
            }
25
26
            $match = preg_match("!^(\d{2})(\d{2})(\d{2})\d\d{6}$!", $value, $matches);
27
            if (!$match) {
28
                return false;
29
            }
30
            list (, $year, $month, $day) = $matches;
0 ignored issues
show
Unused Code introduced by
The assignment to $year is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $month is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $day is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
31
32
            /**
33
             * Check citizenship of the users id (0 = .za, 1 = permanent resident)
34
             */
35
            if (!in_array($value{10}, array(0, 1))) {
36
                return false;
37
            }
38
39
            /**
40
             * Seen 8 or 9 here.
41
             */
42
            if (!in_array($value{11}, [8, 9])) {
43
                return false;
44
            }
45
46
            $idvalid = \PayBreak\Luhn\Luhn::validateNumber($value);
47
48
            return ($idvalid);
49
        }, 'must be a valid South African Identity Number');
50
51
        /**
52
         * Validate the possibility of a mobile number being a valid South African Mobile Number.
53
         */
54
        \Valitron\Validator::addRule('za_mobile_number', function($field, $value, array $params, array $fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
            $msisdn = str_replace(array(' ', '-', '(', ')'), '', $value);
56
            $country = 'ZA';
57
58
            $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
59
            try {
60
                $phoneNumberProto = $phoneUtil->parse($msisdn, $country);
61
            } catch (\libphonenumber\NumberParseException $e) {
62
                return false;
63
            }
64
65
            if (!$phoneUtil->isValidNumber($phoneNumberProto)) {
66
                return false;
67
            }
68
69
            if (
70
                !in_array(
71
                    $phoneUtil->getNumberType($phoneNumberProto),
72
                    [
73
                        1,
74
                        2,
75
                    ]
76
                )
77
            ) {
78
                return false;
79
            }
80
81
            if (
82
                !is_null($country)
83
            ) {
84
                if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) {
85
                    return true;
86
                }
87
88
                return false;
89
            }
90
91
            return true;
92
        }, 'must be a valid South African Mobile Number');
93
    }
94
}
95