Completed
Push — master ( 0da769...adcb36 )
by Jacques
15:29 queued 38s
created

Valitron::addrules()   D

Complexity

Conditions 15
Paths 1

Size

Total Lines 126
Code Lines 64

Duplication

Lines 78
Ratio 61.9 %

Importance

Changes 0
Metric Value
dl 78
loc 126
rs 4.9121
c 0
b 0
f 0
cc 15
eloc 64
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
    public static function addrules()
16
    {
17
        \Valitron\Validator::addRule('street_address', 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...
18
            return !preg_match('/^(Private Bag X\d+|P\.?O\.?\s?Box\s\d+).*$/i', $value);
19
        }, 'must be a physical street address');
20
21
        /**
22
         * Validate the possibility of a mobile number being a valid Namibian Mobile Number.
23
         */
24 View Code Duplication
        \Valitron\Validator::addRule('na_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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
            $msisdn = str_replace(array(' ', '-', '(', ')'), '', $value);
26
            $country = 'NA';
27
28
            $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
29
            try {
30
                $phoneNumberProto = $phoneUtil->parse($msisdn, $country);
31
            } catch (\libphonenumber\NumberParseException $e) {
32
                return false;
33
            }
34
35
            if (!$phoneUtil->isValidNumber($phoneNumberProto)) {
36
                return false;
37
            }
38
39
            if (
40
                !in_array(
41
                    $phoneUtil->getNumberType($phoneNumberProto),
42
                    [
43
                        1,
44
                        2,
45
                    ]
46
                )
47
            ) {
48
                return false;
49
            }
50
51
            if (
52
                !is_null($country)
53
            ) {
54
                if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) {
55
                    return true;
56
                }
57
58
                return false;
59
            }
60
61
            return true;
62
        }, 'must be a valid Namibian Mobile Number');
63
64
        /**
65
         * Validate the possibility of the South African Identity Number beng a valid South
66
         * African identity number.
67
         */
68
        \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...
69
            if (!ctype_digit($value)) {
70
                return false;
71
            }
72
73
            $match = preg_match("!^(\d{2})(\d{2})(\d{2})\d\d{6}$!", $value, $matches);
74
            if (!$match) {
75
                return false;
76
            }
77
            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...
78
79
            /**
80
             * Check citizenship of the users id (0 = .za, 1 = permanent resident)
81
             */
82
            if (!in_array($value{10}, array(0, 1))) {
83
                return false;
84
            }
85
86
            /**
87
             * Seen 8 or 9 here.
88
             */
89
            if (!in_array($value{11}, [8, 9])) {
90
                return false;
91
            }
92
93
            $idvalid = \PayBreak\Luhn\Luhn::validateNumber($value);
94
95
            return ($idvalid);
96
        }, 'must be a valid South African Identity Number');
97
98
        /**
99
         * Validate the possibility of a mobile number being a valid South African Mobile Number.
100
         */
101 View Code Duplication
        \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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
            $msisdn = str_replace(array(' ', '-', '(', ')'), '', $value);
103
            $country = 'ZA';
104
105
            $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
106
            try {
107
                $phoneNumberProto = $phoneUtil->parse($msisdn, $country);
108
            } catch (\libphonenumber\NumberParseException $e) {
109
                return false;
110
            }
111
112
            if (!$phoneUtil->isValidNumber($phoneNumberProto)) {
113
                return false;
114
            }
115
116
            if (
117
                !in_array(
118
                    $phoneUtil->getNumberType($phoneNumberProto),
119
                    [
120
                        1,
121
                        2,
122
                    ]
123
                )
124
            ) {
125
                return false;
126
            }
127
128
            if (
129
                !is_null($country)
130
            ) {
131
                if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) {
132
                    return true;
133
                }
134
135
                return false;
136
            }
137
138
            return true;
139
        }, 'must be a valid South African Mobile Number');
140
    }
141
}
142