Identification::is_valid()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 53

Duplication

Lines 14
Ratio 26.42 %

Importance

Changes 0
Metric Value
cc 12
nc 11
nop 2
dl 14
loc 53
rs 6.9666
c 0
b 0
f 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
 * Identification Validation
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the MIT license.
19
 *
20
 * @author    Jacques Marneweck <[email protected]>
21
 * @copyright 2015-2016 Jacques Marneweck.  All rights strictly reserved.
22
 * @license   MIT
23
 */
24
25
namespace Jacques\Validators;
26
27
use Carbon\Carbon;
28
29
class Identification
30
{
31
    /**
32
     * Checks that a identification number is theoretically valid.  It does not
33
     * validate that the identification number actually belongs to a human.
34
     *
35
     * id_type is either:
36
     *   - 1 - South African Identification Number
37
     *   - 2 - Passport
38
     *   - 3 - South African Asylum Document Number
39
     *
40
     * @param string  $id_document_number Document Number of the document
41
     * @param integer $id_type Identification Document Type
42
     *
43
     * @throws \InvalidArgumentException
44
     *
45
     * @return bool   true if is theoretically valid else false
46
     */
47
    public static function is_valid($id_document_number = null, $id_type = null)
48
    {
49
        if (is_null($id_document_number)) {
50
            throw new \InvalidArgumentException('Please enter a valid id document number.');
51
        }
52
53
        if (is_null($id_type)) {
54
            throw new \InvalidArgumentException('Please pass in a valid id type for the id document number you are testing.');
55
        }
56
57
        if (!is_numeric($id_type)) {
58
            throw new \InvalidArgumentException('Please pass in a numeric value for the id type wanting to be tested.');
59
        }
60
61
        $id_type = (int)$id_type;
62
63
        if ($id_type < 1 || $id_type > 3) {
64
            throw new \InvalidArgumentException('Please enter a numeric value for the id type.  1 == ZA ID / 2 == Passport / 3 == ZA Asylum');
65
        }
66
67
        if (1 == $id_type) {
68
            if (!ctype_digit($id_document_number)) {
69
                return false;
70
            }
71
72
            $idvalid = \PayBreak\Luhn\Luhn::validateNumber($id_document_number);
73
74
            return ($idvalid);
75
        }
76
77
        /**
78
         * Passport numbers are either like MA123456 or 12345678.
79
         */
80 View Code Duplication
        if (2 == $id_type) {
0 ignored issues
show
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...
81
            if (mb_strlen($id_document_number) < 8) {
82
                return false;
83
            }
84
85
            return true;
86
        }
87
88
        /**
89
         * South African Asylum Document Numbers
90
         */
91 View Code Duplication
        if (3 == $id_type) {
0 ignored issues
show
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...
92
            if (mb_strlen($id_document_number) < 8) {
93
                return false;
94
            }
95
96
            return true;
97
        }
98
        return false;
99
    }
100
}
101