Completed
Push — master ( 242b91...2e8ed4 )
by Roberto
04:42 queued 02:28
created

ProcessNumber::clearString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace NFePHP\EFDReinf\Common;
4
5
class ProcessNumber
6
{
7
    /**
8
     * Calculate check digit Algoritm Module 97 Base 10 (ISO 7064)
9
     * Anexo VIII da Resolução CNJ no 65, de 16 de dezembro de 2008.
10
     * @param string $input
11
     * @return string
12
     */
13
    public static function checkDigitJud($input)
14
    {
15
        $n = substr($input, 0, 7);
16
        $dd = substr($input, 7, 2);
0 ignored issues
show
Unused Code introduced by
$dd is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
17
        $a = substr($input, 9, 4);
18
        $jtr = substr($input, 13, 3);
19
        $o = substr($input, 16, 4);
20
        $r1 = $n % 97;
21
        $v2 = str_pad($r1, 2, '0', STR_PAD_LEFT)
22
            . str_pad($a, 4, '0', STR_PAD_LEFT)
23
            . str_pad($jtr, 3, '0', STR_PAD_LEFT);
24
        $r2 = $v2 % 97;
25
        $v3 = str_pad($r2, 2, '0', STR_PAD_LEFT)
26
            . str_pad($o, 4, '0', STR_PAD_LEFT)
27
            . "00";
28
        $r3 = $v3 % 97;
29
        return str_pad((98 - $r3), 2, '0', STR_PAD_LEFT);
30
    }
31
    
32
    /**
33
     * Check if judicial process number have a valid check digit
34
     * @param string $input
35
     * @return boolean
36
     */
37
    public static function isValidProcJudNumber($input)
38
    {
39
        $num = self::clearString($input, 20);
40
        $dd = (string) substr($num, 7, 2);
41
        if ($dd !== self::checkDigitJud($num)) {
42
            return false;
43
        }
44
        return true;
45
    }
46
47
    /**
48
     * Calculate check digit Algoritm Module 11
49
     * @param string $input
50
     * @return string
51
     */
52
    public static function checkDigitAdm($input)
53
    {
54
        $n = substr($input, 0, 5);
55
        $x = substr($input, 5, 6);
56
        $y = substr($input, 11, 4);
57
        $dd = substr($input, -2);
0 ignored issues
show
Unused Code introduced by
$dd is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
        $value = $n.$x.$y;
59
        $a = str_split($value);
60
        $soma = 0;
61
        $i = 16;
62
        foreach ($a as $k) {
63
            $soma += $k * $i;
64
            $i--;
65
        }
66
        $m1 = 11 - ($soma % 11);
67
        if ($m1 > 9) {
68
            $m1 -= 10;
69
        }
70
        $a[] = $m1;
71
        $i = 17;
72
        $soma = 0;
73
        foreach ($a as $k) {
74
            $soma += ($k * $i);
75
            $i--;
76
        }
77
        $m2 = 11 - ($soma % 11);
78
        if ($m2 > 9) {
79
            $m2 -= 10;
80
        }
81
        return $m1.$m2;
82
    }
83
    
84
    /**
85
     * Check if judicial process number have a valid check digit
86
     * @param string $input
87
     * @return boolean
88
     */
89
    public static function isValidProcAdmNumber($input)
90
    {
91
        $num = self::clearString($input, 17);
92
        $dd = substr($num, -2);
93
        if ($dd !== self::checkDigitAdm($num)) {
94
            return false;
95
        }
96
        return true;
97
    }
98
    
99
    
100
    /**
101
     * Clear input number
102
     * @param string $input
103
     * @return string
104
     * @param int $lenght
105
     * @throws \InvalidArgumentException
106
     */
107
    protected static function clearString($input, $lenght)
108
    {
109
        $input = str_replace(['-','/','.'], '', $input);
110
        $input = substr($input, 0, $lenght-1);
111
        $input = str_pad($input, $lenght, '0', STR_PAD_RIGHT);
112
        if (!preg_match('/^[0-9]+$/', $input)) {
113
            throw new \InvalidArgumentException("O numero do processo tem estrutura incorreta.");
114
        }
115
        return (string) $input;
116
    }
117
}
118