Passed
Branch master (0e9587)
by Antonio Oertel
02:20
created

JudiciaryProcess::extractNumbers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
/**
6
 * Formats and validates the numbers of legal proceedings
7
 * related to Judiciary assessments.
8
 * This numbers are used in some SPED projects like EfdReinf and eSocial, from
9
 * Braziliam IRS (Receita Federal)
10
 *
11
 * @property string $sequentialNumber Identify sequential number of process by origin unit.
12
 * @property string $year Identifies the year the process is filed.
13
 * @property string $judiciary Identifies segment of the Judiciary.
14
 * @property string $court Identifies of court from Judiciary.
15
 * @property string $origin Unit of origin of the process.
16
 */
17
final class JudiciaryProcess extends AbstractDocument
18
{
19
    const LENGTH = 20;
20
21
    const LABEL = 'PROCESSO_JUDICIAL';
22
23
    const REGEX = '/^([\d]{7})([\d]{2})([\d]{4})([\d]{1})([\d]{2})([\d]{0,4})$/';
24
25
    const NUMBER_OF_DIGITS = 2;
26
27
    /**
28
     * Identify sequential number of process by origin unit.
29
     *
30
     * @var string
31
     */
32
    private $sequentialNumber;
33
34
    /**
35
     * Identifies the year the process is filed.
36
     *
37
     * @var string
38
     */
39
    private $year;
40
41
    /**
42
     * Identifies segment of the Judiciary.
43
     *
44
     * @var string
45
     */
46
    private $judiciary;
47
48
    /**
49
     * Identifies of court from Judiciary.
50
     *
51
     * @var string
52
     */
53
    private $court;
54
55
    /**
56
     * unit of origin of the process.
57
     *
58
     * @var string
59
     */
60
    private $origin;
61
62 22
    public function __construct($number)
63
    {
64 22
        $number = preg_replace('/\D/', '', $number);
65 22
        $this->extractNumbers($number);
66 22
        parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
67 8
    }
68
69 11
    public static function createFromString($number)
70
    {
71 11
        return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (tryCreateFromString() instead of createFromString()). Are you sure this is correct? If so, you might want to change this to $this->tryCreateFromString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
72
    }
73
74
    /**
75
     * Extract identification numbers.
76
     *
77
     * @param string $number
78
     */
79 22
    private function extractNumbers($number)
80
    {
81 22
        $number = str_pad($number, self::LENGTH, '0', STR_PAD_RIGHT);
82
83 22
        preg_match(self::REGEX, $number, $matches);
84 22
        $this->sequentialNumber = $matches[1];
85 22
        $this->year = $matches[3];
86 22
        $this->judiciary = $matches[4];
87 22
        $this->court = $matches[5];
88 22
        $this->origin = $matches[6];
89 22
    }
90
91 4
    public function format()
92
    {
93 4
        $number = str_pad($this->number, self::LENGTH, '0', STR_PAD_RIGHT);
94
95 4
        return preg_replace(self::REGEX, '$1-$2.$3.$4.$5.$6', "{$number}");
96
    }
97
98 22
    protected function extractCheckerDigit($number)
99
    {
100 22
        return substr($number, 7, 2);
101
    }
102
103 16
    protected function extractBaseNumber($number)
104
    {
105 16
        return "{$this->sequentialNumber}{$this->year}{$this->judiciary}{$this->court}{$this->origin}";
106
    }
107
108
    /**
109
     * Calculate check digit Algoritm Module 97 Base 10 (ISO 7064)
110
     * Anexo VIII da Resolução CNJ no 65, de 16 de dezembro de 2008.
111
     *
112
     * @see http://www.cnj.jus.br/busca-atos-adm?documento=2748
113
     * @see http://www.cnj.jus.br/images/stories/docs_cnj/resolucao/anexorescnj_65.pdf
114
     *
115
     * @param string $input
116
     *
117
     * @return string
118
     */
119 16
    public function calculateDigit($input)
120
    {
121 16
        $remainderNumber = intval($this->sequentialNumber) % 97;
122
123 16
        $judiciaryNumber = "{$remainderNumber}{$this->year}{$this->judiciary}{$this->court}";
124
125 16
        $remainderJudiciary = intval($judiciaryNumber) % 97;
126 16
        $originNumber = "{$remainderJudiciary}{$this->origin}00";
127
128 16
        $remainder = (float) $originNumber % 97;
129 16
        $digit = 98 - $remainder;
130
131 16
        return str_pad($digit, 2, '0', STR_PAD_LEFT);
132
    }
133
}
134