E110::postValidation()   B
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 51

Duplication

Lines 13
Ratio 25.49 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 13
loc 51
ccs 0
cts 32
cp 0
rs 7.8246
c 0
b 0
f 0
cc 8
nc 3
nop 0
crap 72

How to fix   Long Method   

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
namespace NFePHP\EFD\Elements\ICMSIPI;
4
5
use NFePHP\EFD\Common\Element;
6
use NFePHP\EFD\Common\ElementInterface;
7
use \stdClass;
8
9
class E110 extends Element implements ElementInterface
10
{
11
    const REG = 'E110';
12
    const LEVEL = 3;
13
    const PARENT = 'E100';
14
15
    protected $parameters = [
16
        'VL_TOT_DEBITOS' => [
17
            'type'     => 'numeric',
18
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
19
            'required' => true,
20
            'info'     => 'Valor total dos débitos por "Saídas e prestações com débito do imposto"',
21
            'format'   => '15v2'
22
        ],
23
        'VL_AJ_DEBITOS' => [
24
            'type'     => 'numeric',
25
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
26
            'required' => true,
27
            'info'     => 'Valor total dos ajustes a débito decorrentes do documento fiscal.',
28
            'format'   => '15v2'
29
        ],
30
        'VL_TOT_AJ_DEBITOS' => [
31
            'type'     => 'numeric',
32
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
33
            'required' => true,
34
            'info'     => 'Valor total de "Ajustes a débito"',
35
            'format'   => '15v2'
36
        ],
37
        'VL_ESTORNOS_CRED' => [
38
            'type'     => 'numeric',
39
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
40
            'required' => true,
41
            'info'     => 'Valor total de Ajustes “Estornos de créditos”',
42
            'format'   => '15v2'
43
        ],
44
        'VL_TOT_CREDITOS' => [
45
            'type'     => 'numeric',
46
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
47
            'required' => true,
48
            'info'     => 'Valor total dos créditos por "Entradas e aquisições com crédito do imposto"',
49
            'format'   => '15v2'
50
        ],
51
        'VL_AJ_CREDITOS' => [
52
            'type'     => 'numeric',
53
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
54
            'required' => true,
55
            'info'     => 'Valor total dos ajustes a crédito decorrentes do documento fiscal.',
56
            'format'   => '15v2'
57
        ],
58
        'VL_TOT_AJ_CREDITOS' => [
59
            'type'     => 'numeric',
60
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
61
            'required' => true,
62
            'info'     => 'Valor total de "Ajustes a crédito"',
63
            'format'   => '15v2'
64
        ],
65
        'VL_ESTORNOS_DEB' => [
66
            'type'     => 'numeric',
67
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
68
            'required' => true,
69
            'info'     => 'Valor total de Ajustes “Estornos de Débitos”',
70
            'format'   => '15v2'
71
        ],
72
        'VL_SLD_CREDOR_ANT' => [
73
            'type'     => 'numeric',
74
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
75
            'required' => true,
76
            'info'     => 'Valor total de "Saldo credor do período anterior"',
77
            'format'   => '15v2'
78
        ],
79
        'VL_SLD_APURADO' => [
80
            'type'     => 'numeric',
81
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
82
            'required' => true,
83
            'info'     => 'Valor do saldo devedor apurado',
84
            'format'   => '15v2'
85
        ],
86
        'VL_TOT_DED' => [
87
            'type'     => 'numeric',
88
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
89
            'required' => true,
90
            'info'     => 'Valor total de "Deduções"',
91
            'format'   => '15v2'
92
        ],
93
        'VL_ICMS_RECOLHER' => [
94
            'type'     => 'numeric',
95
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
96
            'required' => true,
97
            'info'     => 'Valor total de "ICMS a recolher (11-12)',
98
            'format'   => '15v2'
99
        ],
100
        'VL_SLD_CREDOR_TRANSPORTAR' => [
101
            'type'     => 'numeric',
102
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
103
            'required' => true,
104
            'info'     => 'Valor total de "Saldo credor a transportar para o período seguinte”',
105
            'format'   => '15v2'
106
        ],
107
        'DEB_ESP' => [
108
            'type'     => 'numeric',
109
            'regex'    => '^\d+(\.\d*)?|\.\d+$',
110
            'required' => true,
111
            'info'     => 'Valores recolhidos ou a recolher, extra apuração.',
112
            'format'   => '15v2'
113
        ]
114
    ];
115
116
    /**
117
     * Constructor
118
     * @param \stdClass $std
119
     */
120
    public function __construct(\stdClass $std)
121
    {
122
        parent::__construct(self::REG);
123
        $this->std = $this->standarize($std);
124
        $this->postValidation();
125
    }
126
127
    public function postValidation()
128
    {
129
        /*
130
         * Campo 13 (VL_ICMS_RECOLHER) Validação: o valor informado deve corresponder à diferença entre o
131
         * campo VL_SLD_APURADO e o campo VL_TOT_DED. Se o resultado dessa operação for negativo, informe
132
         * o valor zero neste campo, e o valor absoluto correspondente no campo VL_SLD_CREDOR_TRANSPORTAR.
133
         */
134
        $diferenca = $this->values->vl_sld_apurado - $this->values->vl_tot_ded;
135
        if ($diferenca < 0 && ($this->values->vl_icms_recolher != 0 || $this->values->vl_sld_credor_transportar == 0)) {
136
            throw new \InvalidArgumentException("[" . self::REG . "] O valor informado deve corresponder à diferença "
137
            ."entre o campo VL_SLD_APURADO e o campo VL_TOT_DED. Se o resultado dessa operação for negativo, informe "
138
            ."o valor zero neste campo, e o valor absoluto correspondente no campo VL_SLD_CREDOR_TRANSPORTAR.");
139
        }
140
141
        /*
142
         * Campo 14 (VL_SLD_CREDOR_TRANSPORTAR) Validação: o valor informado deve ser preenchido com base
143
         * na expressão: soma do total de débitos (VL_TOT_DEBITOS) com total de ajustes
144
         * (VL_AJ_DEBITOS +VL_TOT_AJ_DEBITOS) com total de estorno de crédito (VL_ESTORNOS_CRED) menos a
145
         * soma do total de créditos (VL_TOT_CREDITOS) com total de ajuste de créditos
146
         * (VL_,AJ_CREDITOS + VL_TOT_AJ_CREDITOS) com total de estorno de débito (VL_ESTORNOS_DEB)
147
         * com saldo credor do período anterior (VL_SLD_CREDOR_ANT). Se o valor da expressão for maior
148
         * ou igual a “0” (zero), então este valor deve ser informado neste campo e o campo 14
149
         * (VL_SLD_CREDOR_TRANSPORTAR) deve ser igual a “0” (zero). Se o valor da expressão for menor que
150
         * “0” (zero), então este campo deve ser preenchido com “0” (zero) e o valor absoluto da expressão deve
151
         * ser informado no campo VL_SLD_CREDOR_TRANSPORTAR, adicionado ao valor total das deduções (VL_TOT_DED)
152
         */
153
        $somatorio = $this->values->vl_tot_debitos
154
                    + $this->values->vl_aj_debitos
155
                    + $this->values->vl_tot_aj_debitos
156
                    + $this->values->vl_estornos_cred
157
                    - $this->values->vl_tot_creditos
158
                    - $this->values->vl_aj_creditos
159
                    - $this->values->vl_tot_aj_creditos
160
                    - $this->values->vl_estornos_deb
161
                    - $this->values->vl_sld_credor_ant
162
                    - $this->values->vl_tot_ded;
163
164 View Code Duplication
        if (($somatorio >= 0 && $this->values->vl_sld_credor_transportar != 0)
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...
165
        || ($somatorio < 0 && $this->values->vl_sld_credor_transportar == 0)) {
166
            throw new \InvalidArgumentException("[" . self::REG . "] O valor informado deve ser preenchido com base "
167
            ."na expressão: soma do total de débitos (VL_TOT_DEBITOS) com total de ajustes "
168
            ."(VL_AJ_DEBITOS +VL_TOT_AJ_DEBITOS) com total de estorno de crédito (VL_ESTORNOS_CRED) menos a "
169
            ."soma do total de créditos (VL_TOT_CREDITOS) com total de ajuste de créditos "
170
            ."(VL_,AJ_CREDITOS + VL_TOT_AJ_CREDITOS) com total de estorno de débito (VL_ESTORNOS_DEB) "
171
            ."com saldo credor do período anterior (VL_SLD_CREDOR_ANT). Se o valor da expressão for maior "
172
            ."ou igual a “0” (zero), então este valor deve ser informado neste campo e o campo 14 "
173
            ."(VL_SLD_CREDOR_TRANSPORTAR) deve ser igual a “0” (zero). Se o valor da expressão for menor que "
174
            ."“0” (zero), então este campo deve ser preenchido com “0” (zero) e o valor absoluto da expressão deve "
175
            ."ser informado no campo VL_SLD_CREDOR_TRANSPORTAR, adicionado ao valor total das deduções (VL_TOT_DED)");
176
        }
177
    }
178
}
179