Completed
Push — master ( 38feca...1f34d0 )
by Roberto
9s
created

Response::readEnviarLoteEvento()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 41
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 4.0003

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 41
ccs 35
cts 36
cp 0.9722
rs 8.5806
cc 4
eloc 34
nc 3
nop 1
crap 4.0003
1
<?php
2
3
namespace NFePHP\eFinanc;
4
5
/**
6
 * Classe auxiliar que le e trata todos os retornos do webservice
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHP\eFinanc
10
 * @copyright Copyright (c) 2016
11
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link      http://github.com/nfephp-org/nfephp for the canonical source repository
14
 */
15
16
use DOMDocument;
17
use NFePHP\Common\DateTime\DateTime;
18
19
class Response
20
{
21
    /**
22
     * Verifica o retorno e extrai os seus dados conforme o método usado
23
     * na consulta ao webservice ou no retorno de envio de lote
24
     *
25
     * @param string $method
26
     * @param string $xmlResp
27
     * @return array
28
     */
29 18
    public static function readReturn($method, $xmlResp = '')
30
    {
31 18
        if (trim($xmlResp) == '') {
32
            return [
33
                'bStat' => false,
34
                'message' => 'Não retornou nenhum dado'
35
            ];
36
        }
37 18
        libxml_use_internal_errors(true);
38 18
        $dom = new DOMDocument('1.0', 'utf-8');
39 18
        $dom->loadXML($xmlResp);
40 18
        $errors = libxml_get_errors();
41 18
        libxml_clear_errors();
42 18
        if (! empty($errors)) {
43
            return [
44
                'bStat' => false,
45
                'message' => $xmlResp
46
            ];
47
        }
48
        //foi retornado um xml continue
49 18
        $reason = self::checkForFault($dom);
50 18
        if ($reason != '') {
51
            return [
52
                'bStat' => false,
53
                'message' => $reason
54
            ];
55
        }
56
        //para cada $method tem um formato de retorno especifico
57
        switch ($method) {
58 18
            case 'ConsultarInformacoesCadastrais':
59 3
                return self::readConsultarInformacoesCadastrais($dom);
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61 15
            case 'ConsultarListaEFinanceira':
62 3
                return self::readConsultarListaEFinanceira($dom);
63
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64 12
            case 'ConsultarInformacoesMovimento':
65 3
                return self::readConsultarInformacoesMovimento($dom);
66
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
67 9
            case 'ConsultarInformacoesIntermediario':
68 3
                return self::readConsultarInformacoesIntermediario($dom);
69
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
70 6
            case 'ConsultarInformacoesPatrocinado':
71 3
                return self::readConsultarInformacoesPatrocinado($dom);
72
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
73 3
            case 'ReceberLoteEvento':
74 3
                return self::readEnviarLoteEvento($dom);
75
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
        }
77
        return array();
78
    }
79
    
80
    /**
81
     * Verifica se o retorno é relativo a um ERRO SOAP
82
     *
83
     * @param DOMDocument $dom
84
     * @return string
85
     */
86 18
    public static function checkForFault($dom)
87
    {
88 18
        $tagfault = $dom->getElementsByTagName('Fault')->item(0);
89 18
        if (empty($tagfault)) {
90 18
            return '';
91
        }
92
        $tagreason = $tagfault->getElementsByTagName('Reason')->item(0);
93
        if (! empty($tagreason)) {
94
            $reason = $tagreason->getElementsByTagName('Text')->item(0)->nodeValue;
95
            return $reason;
96
        }
97
        return 'Houve uma falha na comunicação.';
98
    }
99
    
100
    /**
101
     * Lê o retorno da Consulta Informacoes Cadastrais do Declarante
102
     *
103
     * @param DOMDocument $dom
104
     * @return array
105
     */
106 3
    public static function readConsultarInformacoesCadastrais($dom)
107
    {
108
        $aStatus = [
0 ignored issues
show
Unused Code introduced by
$aStatus 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...
109 3
            'cdRetorno' => '',
110 2
            'descRetorno' => '',
111 2
            'ocorrencias' => array()
112 2
        ];
113
        $aInfo = [
114 3
            'cnpj'=> '',
115 2
            'giin'=> '',
116 2
            'nome' => '',
117 2
            'endereco' => '',
118 2
            'municipio' => '',
119
            'uf' => ''
120 2
        ];
121
        $aResposta = [
122 3
            'bStat' => false,
123 2
            'cnpjEmpresaDeclarante' => '',
124 2
            'dataHoraProcessamento' => '',
125 2
            'status' => array(),
126 2
            'informacoesCadastrais' => array(),
127 2
            'numeroRecibo' => '',
128
            'idEvento' => ''
129 2
        ];
130 3
        $tag = $dom->getElementsByTagName('ConsultarInformacoesCadastraisResponse')->item(0);
131 3
        if (!isset($tag)) {
132
            return $aResposta;
133
        }
134
        //indica que houve resposta do webservice se for true
135
        //caso seja false algo estranho ocorreu
136 3
        $aResposta['bStat'] = true;
137
        //nó principal 1 - 1
138 3
        $node = $tag->getElementsByTagName('retornoConsultaInformacoesCadastrais')->item(0);
139
        //busca a data e hora do processamento
140 3
        $aResposta['dataHoraProcessamento'] = self::retDataHora($node);
141
        //busca dados declarante
142 3
        $aResposta['cnpjEmpresaDeclarante'] = self::retDeclarante($node)['cnpj'];
143
        //busca o status
144 3
        $aResposta['status'] = self::retStatus($node);
145 3
        $aResposta['numeroRecibo'] = self::retValue($node, 'numeroRecibo');
146 3
        $aResposta['idEvento'] = self::retValue($node, 'id');
147
        //informaçṍes cadastrais
148 3
        $info = $node->getElementsByTagName('informacoesCadastrais')->item(0);
149 3
        if (!empty($info)) {
150 3
            $aInfo['cnpj'] = self::retValue($info, 'cnpj');
151 3
            $aInfo['giin'] = self::retValue($info, 'giin');
152 3
            $aInfo['nome'] =  self::retValue($info, 'nome');
153 3
            $aInfo['endereco'] =  self::retValue($info, 'endereco');
154 3
            $aInfo['municipio'] =  self::retValue($info, 'municipio');
155 3
            $aInfo['uf'] =  self::retValue($info, 'uf');
156 2
        }
157 3
        $aResposta['informacoesCadastrais'] = $aInfo;
158 3
        return $aResposta;
159
    }
160
161
    /**
162
     * Lê o retorno da Consulta a Lista e-Financeira
163
     *
164
     * @param DOMDocument $dom
165
     * @return array
166
     */
167 3 View Code Duplication
    public static function readConsultarListaEFinanceira($dom)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
168
    {
169
        $aResposta = [
170 3
            'bStat' => false,
171 2
            'cnpjEmpresaDeclarante' => '',
172 2
            'dataHoraProcessamento' => '',
173 2
            'status' => array(),
174 2
            'informacoesEFinanceira' => array()
175 2
        ];
176 3
        $aInfos = array();
177
        $aInfo = [
178 3
            'dhInicial'=>'',
179 2
            'dhFinal'=>'',
180 2
            'situacaoEFinanceira'=>'',
181 2
            'numeroReciboAbertura'=>'',
182 2
            'idAbertura'=>'',
183 2
            'numeroReciboFechamento'=>'',
184
            'idFechamento'=>''
185 2
        ];
186 3
        $tag = $dom->getElementsByTagName('ConsultarListaEFinanceiraResponse')->item(0);
187 3
        if (!isset($tag)) {
188
            return $aResposta;
189
        }
190
        //indica que houve resposta do webservice se for true
191
        //caso seja false algo estranho ocorreu
192 3
        $aResposta['bStat'] = true;
193
        //nó principal 1 - 1
194 3
        $node = $tag->getElementsByTagName('retornoConsultaListaEFinanceira')->item(0);
195
        //busca a data e hora do processamento
196 3
        $aResposta['dataHoraProcessamento'] = self::retDataHora($node);
197
        //busca dados declarante
198 3
        $aResposta['cnpjEmpresaDeclarante'] = self::retDeclarante($node)['cnpj'];
199
        //busca o status
200 3
        $aResposta['status'] = self::retStatus($node);
201
        //informacoesEFinanceira
202 3
        $info = $node->getElementsByTagName('informacoesEFinanceira');
203 3
        if (isset($info)) {
204 3
            $n = 0;
205 3
            foreach ($info as $inform) {
206 3
                $node = $info->item($n);
207 3
                $aInfo['dhInicial'] = self::retValue($node, 'dhInicial');
208 3
                $aInfo['dhFinal'] = self::retValue($node, 'dhFinal');
209 3
                $aInfo['situacaoEFinanceira'] = self::retValue($node, 'situacaoEFinanceira', '0');
210 3
                $aInfo['numeroReciboAbertura'] = self::retValue($node, 'numeroReciboAbertura');
211 3
                $aInfo['idAbertura'] = self::retValue($node, 'idAbertura');
212 3
                $aInfo['numeroReciboFechamento'] = self::retValue($node, 'numeroReciboFechamento');
213 3
                $aInfo['idFechamento'] = self::retValue($node, 'idFechamento');
214 3
                $aInfos[] = $aInfo;
215 3
                $n++;
216 2
            }
217 2
        }
218 3
        $aResposta['informacoesEFinanceira'] = $aInfos;
219 3
        return $aResposta;
220
    }
221
    
222
    /**
223
     * Lê o retorno da Consulta Informacoes de Movimento
224
     *
225
     * @param DOMDocument $dom
226
     * @return array
227
     */
228 3 View Code Duplication
    public static function readConsultarInformacoesMovimento($dom)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
229
    {
230
        $aResposta = [
231 3
            'bStat' => false,
232 2
            'cnpjEmpresaDeclarante' => '',
233 2
            'dhProcessamento' => '',
234 2
            'status' => array(),
235 2
            'informacoesMovimento' => array()
236 2
        ];
237 3
        $aInfos = array();
238
        $aInfo = [
239 3
            'tipoMovimento'=>'',
240 2
            'tipoNI'=>'',
241 2
            'NI'=>'',
242 2
            'anoMesCaixa'=>'',
243 2
            'situacao'=>'',
244 2
            'numeroRecibo'=>'',
245
            'id'=>''
246 2
        ];
247 3
        $tag = $dom->getElementsByTagName('ConsultarInformacoesMovimentoResponse')->item(0);
248 3
        if (!isset($tag)) {
249 3
            return $aResposta;
250
        }
251
        //indica que houve resposta do webservice se for true
252
        //caso seja false algo estranho ocorreu
253
        $aResposta['bStat'] = true;
254
        //nó principal 1 - 1
255
        $node = $tag->getElementsByTagName('retornoConsultaInformacoesMovimento')->item(0);
256
        //busca a data e hora do processamento
257
        $aResposta['dataHoraProcessamento'] = self::retDataHora($node);
258
        //busca dados declarante
259
        $aResposta['cnpjEmpresaDeclarante'] = self::retDeclarante($node)['cnpj'];
260
        //busca o status
261
        $aResposta['status'] = self::retStatus($node);
262
        //informacoesEFinanceira
263
        $info = $node->getElementsByTagName('informacoesMovimento');
264
        if (isset($info)) {
265
            $n = 0;
266
            foreach ($info as $inform) {
267
                $node = $info->item($n);
268
                $aInfo['tipoMovimento'] = self::retValue($node, 'tipoMovimento');
269
                $aInfo['tipoNI'] = self::retValue($node, 'tipoNI');
270
                $aInfo['NI'] = self::retValue($node, 'NI');
271
                $aInfo['anoMesCaixa'] = self::retValue($node, 'anoMesCaixa');
272
                $aInfo['situacao'] = self::retValue($node, 'situacao');
273
                $aInfo['numeroRecibo'] = self::retValue($node, 'numeroRecibo');
274
                $aInfo['id'] = self::retValue($node, 'id');
275
                $aInfos[] = $aInfo;
276
                $n++;
277
            }
278
        }
279
        $aResposta['informacoesMovimento'] = $aInfos;
280
        return $aResposta;
281
    }
282
    
283
    /**
284
     * Lê o retorno da Consulta Informacoes do Intemedirário
285
     *
286
     * @param DOMDocument $dom
287
     * @return array
288
     */
289 3 View Code Duplication
    public static function readConsultarInformacoesIntermediario($dom)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
290
    {
291
        $aResposta = [
292 3
            'bStat' => false,
293 2
            'cnpjEmpresaDeclarante' => '',
294 2
            'dhProcessamento' => '',
295 2
            'status' => array(),
296 2
            'identificacaoIntermediario' => array()
297 2
        ];
298 3
        $aInfos = array();
299
        $aInfo = [
300 3
            'GIIN'=>'',
301 2
            'tpNI'=>'',
302 2
            'NIIntermediario'=>'',
303 2
            'numeroRecibo'=>'',
304
            'id'=>''
305 2
        ];
306 3
        $tag = $dom->getElementsByTagName('ConsultarInformacoesIntermediarioResponse')->item(0);
307 3
        if (!isset($tag)) {
308
            return $aResposta;
309
        }
310
        //indica que houve resposta do webservice se for true
311
        //caso seja false algo estranho ocorreu
312 3
        $aResposta['bStat'] = true;
313
        //nó principal 1 - 1
314 3
        $node = $tag->getElementsByTagName('retornoConsultaInformacoesIntermediario')->item(0);
315
        //busca a data e hora do processamento
316 3
        $aResposta['dataHoraProcessamento'] = self::retDataHora($node);
317
        //busca dados declarante
318 3
        $aResposta['cnpjEmpresaDeclarante'] = self::retDeclarante($node)['cnpj'];
319
        //busca o status
320 3
        $aResposta['status'] = self::retStatus($node);
321
        //informacoesEFinanceira
322 3
        $info = $node->getElementsByTagName('identificacaoIntermediario');
323 3
        if (isset($info)) {
324 3
            $n = 0;
325 3
            foreach ($info as $inform) {
326 3
                $node = $info->item($n);
327 3
                $aInfo['GIIN'] = self::retValue($node, 'GIIN');
328 3
                $aInfo['tpNI'] = self::retValue($node, 'tpNI');
329 3
                $aInfo['NIIntermediario'] = self::retValue($node, 'NIIntermediario');
330 3
                $aInfo['numeroRecibo'] = self::retValue($node, 'numeroRecibo');
331 3
                $aInfo['id'] = self::retValue($node, 'id');
332 3
                $aInfos[] = $aInfo;
333 3
                $n++;
334 2
            }
335 2
        }
336 3
         $aResposta['identificacaoIntermediario'] = $aInfos;
337 3
        return $aResposta;
338
    }
339
340
    /**
341
     * Lê o retorno da Consulta Informacoes do Patrocinado
342
     *
343
     * @param DOMDocument $dom
344
     * @return array
345
     */
346 3 View Code Duplication
    public static function readConsultarInformacoesPatrocinado($dom)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
347
    {
348
        $aResposta = [
349 3
            'bStat' => false,
350 2
            'cnpjEmpresaDeclarante' => '',
351 2
            'giinEmpresaDeclarante' => '',
352 2
            'dataHoraProcessamento' => '',
353 2
            'status' => array(),
354 2
            'identificacaoPatrocinado' => array()
355 2
        ];
356 3
        $aInfos = array();
357
        $aInfo = [
358 3
            'GIIN'=>'',
359 2
            'CNPJ'=>'',
360 2
            'numeroRecibo'=>'',
361
            'id'=>''
362 2
        ];
363 3
        $tag = $dom->getElementsByTagName('ConsultarInformacoesPatrocinadoResponse')->item(0);
364 3
        if (!isset($tag)) {
365
            return $aResposta;
366
        }
367
        //indica que houve resposta do webservice se for true
368
        //caso seja false algo estranho ocorreu
369 3
        $aResposta['bStat'] = true;
370
        //nó principal 1 - 1
371 3
        $node = $tag->getElementsByTagName('retornoConsultaInformacoesPatrocinado')->item(0);
372
        //busca a data e hora do processamento
373 3
        $aResposta['dataHoraProcessamento'] = self::retDataHora($node);
374
        //busca dados declarante
375 3
        $aResposta['cnpjEmpresaDeclarante'] = self::retDeclarante($node)['cnpj'];
376 3
        $aResposta['giinEmpresaDeclarante'] = self::retDeclarante($node)['giin'];
377
        //busca o status
378 3
        $aResposta['status'] = self::retStatus($node);
379
        //informacoesEFinanceira
380 3
        $info = $node->getElementsByTagName('identificacaoPatrocinado');
381 3
        if (isset($info)) {
382 3
            $n = 0;
383 3
            foreach ($info as $inform) {
384 3
                $node = $info->item($n);
385 3
                $aInfo['GIIN'] = self::retValue($node, 'GIIN');
386 3
                $aInfo['CNPJ'] = self::retValue($node, 'CNPJ');
387 3
                $aInfo['numeroRecibo'] = self::retValue($node, 'numeroRecibo');
388 3
                $aInfo['id'] = self::retValue($node, 'id');
389 3
                $aInfos[] = $aInfo;
390 3
                $n++;
391 2
            }
392 2
        }
393 3
         $aResposta['identificacaoPatrocinado'] = $aInfos;
394 3
        return $aResposta;
395
    }
396
    
397
    /**
398
     * Lê o retorno do envio de lote
399
     *
400
     * @param DOMDocument $dom
401
     * @return array
402
     */
403 3
    public static function readEnviarLoteEvento($dom)
404
    {
405
        $aResposta = [
406 3
            'bStat' => false,
407 2
            'IdTransmissor' => '',
408 2
            'status' => array(),
409 2
            'retornoEventos' => array()
410 2
        ];
411 3
        $aEventos = [];
412 3
        $tag = $dom->getElementsByTagName('retornoLoteEventos')->item(0);
413 3
        if (! isset($tag)) {
414
            return $aResposta;
415
        }
416 3
        $aResposta['bStat'] = true;
417
        //busca o status
418 3
        $aResposta['status'] = self::retStatus($tag);
0 ignored issues
show
Documentation introduced by
$tag is of type object<DOMNode>, but the function expects a object<NFePHP\eFinanc\DOMElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
419 3
        $tag = $dom->getElementsByTagName('retornoEventos')->item(0);
0 ignored issues
show
Unused Code introduced by
$tag 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...
420 3
        $eventos = $dom->getElementsByTagName('evento');
421 3
        $i = 0;
422 3
        if (!empty($eventos)) {
423 3
            foreach ($eventos as $evento) {
424 3
                $ret = $eventos->item($i)->getElementsByTagName('retornoEvento')->item(0);
425 3
                $recepcao = $ret->getElementsByTagName('dadosRecepcaoEvento')->item(0);
426 3
                $dadosReciboEntrega = $ret->getElementsByTagName('dadosReciboEntrega')->item(0);
427 3
                $aEvento['id'] = $ret->getAttribute('id');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aEvento was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aEvento = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
428 3
                $aEvento['cnpjDeclarante'] = self::retDeclarante($ret)['cnpj'];
0 ignored issues
show
Bug introduced by
The variable $aEvento does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
429 3
                $aEvento['dhProcessamento'] = self::retValue($recepcao, 'dhProcessamento');
430 3
                $aEvento['tipoEvento'] = self::retValue($recepcao, 'tipoEvento');
431 3
                $aEvento['idEvento'] = self::retValue($recepcao, 'idEvento');
432 3
                $aEvento['hash'] = self::retValue($recepcao, 'hash');
433 3
                $aEvento['nrRecibo'] = self::retValue($recepcao, 'nrRecibo');
434 3
                $aEvento['status'] = self::retStatus($ret);
435 3
                $aEvento['numeroRecibo'] = '';
436 3
                $aEvento['numeroRecibo'] = self::retValue($dadosReciboEntrega, 'numeroRecibo');
437 3
                $aEventos[] = $aEvento;
438 3
                $i++;
439 2
            }
440 2
        }
441 3
        $aResposta['retornoEventos'] = $aEventos;
442 3
        return $aResposta;
443
    }
444
    
445
    /**
446
     * Busca a data e hora do processamento
447
     *
448
     * @param DOMElement $node
449
     * @return strinf
450
     */
451 12
    protected static function retDataHora($node)
452
    {
453 12
        $data = '';
454 12
        $dtHora = self::retValue($node, 'dataHoraProcessamento');
455 12
        if (empty($dtHora)) {
456 3
            $dtHora = self::retValue($node, 'dhProcessamento');
457 2
        }
458 12
        if (! empty($dtHora)) {
459 12
            $data = date('d/m/Y H:i:s', DateTime::convertSefazTimeToTimestamp($dtHora));
460 8
        }
461 12
        return $data;
462
    }
463
464
    /**
465
     * Busca o cnpj do declarante
466
     *
467
     * @param DOMElement $node
468
     * @return string
469
     */
470 15
    protected static function retDeclarante($node)
471
    {
472
        //declarante 1 - 1
473 15
        $nodeDeclarante = $node->getElementsByTagName('identificacaoEmpresaDeclarante')->item(0);
474 15
        $cnpj = self::retValue($nodeDeclarante, 'cnpjEmpresaDeclarante');
475 15
        $giin = self::retValue($nodeDeclarante, 'GIIN');
476 15
        return array('cnpj'=>$cnpj, "giin"=>$giin);
477
    }
478
    
479
    /**
480
     * Busca e retorna o status em um array
481
     *
482
     * @param DOMElement $node
483
     * @return array
484
     */
485 15
    protected static function retStatus($node)
486
    {
487 15
        $aOcorrencias = array();
488
        $aOcorr = [
489 15
            'tipo' => '',
490 10
            'codigo' => '',
491 10
            'descricao' => '',
492
            'localizacaoErroAviso' => ''
493 10
        ];
494
        $aStatus = [
495 15
            'cdRetorno' => '',
496 10
            'descRetorno' => '',
497 10
            'ocorrencias' => array()
498 10
        ];
499
        //status 1 - 1
500 15
        $nodestatus = $node->getElementsByTagName('status')->item(0);
501 15
        $aStatus['descRetorno'] = self::retValue($nodestatus, 'descRetorno');
502 15
        $aStatus['cdRetorno'] = '1';
503 15
        if ($aStatus['descRetorno'] === 'SUCESSO') {
504 15
            $aStatus['cdRetorno'] = '0';
505 10
        }
506
        //status/dadosRegistroOcorrenciaEvento 0 -> N
507 15
        $nodedados = $node->getElementsByTagName('dadosRegistroOcorrenciaEvento');
508 15
        if ($nodedados->length == 0) {
509 15
            $nodedados = $node->getElementsByTagName('dadosRegistroOcorrenciaLote');
510 10
        }
511 15
        if (!empty($nodedados)) {
512 15
            $n = 0;
513 15
            foreach ($nodedados as $dados) {
514
                //dadosRegistroOcorrenciaEvento/ocorrencias 0 - N
515
                $nodeocor = $nodedados->item($n)->getElementsByTagName('ocorrencias');
516
                if (!empty($nodeocor)) {
517
                    $x = 0;
518
                    foreach ($nodeocor as $ocor) {
519
                        $dnode = $nodeocor->item($x);
520
                        $aOcorr['tipo'] = self::retValue($dnode, 'tipo');
521
                        $aOcorr['codigo'] = self::retValue($dnode, 'codigo');
522
                        $aOcorr['descricao'] = self::retValue($dnode, 'descricao');
523
                        $aOcorr['localizacaoErroAviso'] = self::retValue($dnode, 'localizacaoErroAviso');
524
                        $aOcorrencias[] = $aOcorr;
525
                        $x++;
526
                    }
527
                }
528
                $n++;
529 10
            }
530 10
        }
531 15
        $aStatus['ocorrencias'] = $aOcorrencias;
532 15
        return $aStatus;
533
    }
534
    
535
    /**
536
     * Extrai o valor de uma tag
537
     *
538
     * @param DOMElement $node
539
     * @param string $tag
540
     * @param string $expected
541
     * @return string
542
     */
543 15
    private static function retValue($node, $tag, $expected = '')
544
    {
545 15
        if (empty($node) || empty($tag)) {
546
            return '';
547
        }
548 15
        return !empty($node->getElementsByTagName($tag)->item(0)->nodeValue) ?
549 15
            $node->getElementsByTagName($tag)->item(0)->nodeValue :
550 15
            $expected;
551
    }
552
}
553