Conditions | 10 |
Paths | 1 |
Total Lines | 79 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
15 | static public function addrules() |
||
|
|||
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) { |
||
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; |
||
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) { |
||
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 |