Test Failed
Push — master ( f8eb03...8feb3a )
by Francimar
06:03
created

Transporte::toArray()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11.0077

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 24
cts 25
cp 0.96
rs 7.3166
c 0
b 0
f 0
cc 11
nc 32
nop 1
crap 11.0077

How to fix   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
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Entity;
29
30
use NFe\Common\Node;
31
use NFe\Common\Util;
32
use NFe\Entity\Transporte\Veiculo;
33
use NFe\Entity\Transporte\Tributo;
34
use NFe\Entity\Transporte\Transportador;
35
36
/**
37
 * Dados dos transportes da NF-e
38
 */
39
class Transporte implements Node
0 ignored issues
show
Complexity introduced by
This class has a complexity of 75 which exceeds the configured maximum of 50.

The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.

Some resources for further reading:

You can also find more detailed suggestions for refactoring in the “Code” section of your repository.

Loading history...
40
{
41
42
    /**
43
     * Modalidade do frete
44
     * 0- Contratação do Frete por conta do Remetente
45
     * (CIF);
46
     * 1- Contratação do Frete por conta do destinatário/remetente
47
     * (FOB);
48
     * 2- Contratação do Frete por conta de terceiros;
49
     * 3- Transporte
50
     * próprio por conta do remetente;
51
     * 4- Transporte próprio por conta do
52
     * destinatário;
53
     * 9- Sem Ocorrência de transporte.
54
     */
55
    const FRETE_REMETENTE = 'remetente';
56
    const FRETE_DESTINATARIO = 'destinatario';
57
    const FRETE_TERCEIROS = 'terceiros';
58
    const FRETE_PROPRIOREMETENTE = 'proprio_remetente';
59
    const FRETE_PROPRIODESTINATARIO = 'proprio_destinatario';
60
    const FRETE_NENHUM = 'nenhum';
61
62
    private $frete;
63
    private $transportador;
64
    private $retencao;
65
    private $veiculo;
66
    private $reboque;
67
    private $vagao;
68
    private $balsa;
69
    private $volumes;
70
71 46
    public function __construct($transporte = [])
72
    {
73 46
        $this->fromArray($transporte);
74 46
    }
75
76
    /**
77
     * Modalidade do frete
78
     * 0- Contratação do Frete por conta do Remetente
79
     * (CIF);
80
     * 1- Contratação do Frete por conta do destinatário/remetente
81
     * (FOB);
82
     * 2- Contratação do Frete por conta de terceiros;
83
     * 3- Transporte
84
     * próprio por conta do remetente;
85
     * 4- Transporte próprio por conta do
86
     * destinatário;
87
     * 9- Sem Ocorrência de transporte.
88
     * @param boolean $normalize informa se o frete deve estar no formato do XML
89
     * @return mixed frete da Transporte
90
     */
91 41
    public function getFrete($normalize = false)
92
    {
93 41
        if (!$normalize) {
94 41
            return $this->frete;
95
        }
96 40
        switch ($this->frete) {
97 40
            case self::FRETE_REMETENTE:
98 2
                return '0';
99 38
            case self::FRETE_DESTINATARIO:
100 2
                return '1';
101 37
            case self::FRETE_TERCEIROS:
102 2
                return '2';
103 35
            case self::FRETE_PROPRIOREMETENTE:
104 1
                return '3';
105 34
            case self::FRETE_PROPRIODESTINATARIO:
106
                return '4';
107 34
            case self::FRETE_NENHUM:
108 34
                return '9';
109
        }
110
        return $this->frete;
111
    }
112
    
113
    /**
114
     * Altera o valor do Frete para o informado no parâmetro
115
     * @param mixed $frete novo valor para Frete
116
     * @return Transporte A própria instância da classe
117
     */
118 46
    public function setFrete($frete)
119
    {
120
        switch ($frete) {
121 46
            case '0':
122 1
                $frete = self::FRETE_REMETENTE;
123 1
                break;
124 46
            case '1':
125 1
                $frete = self::FRETE_DESTINATARIO;
126 1
                break;
127 46
            case '2':
128 1
                $frete = self::FRETE_TERCEIROS;
129 1
                break;
130 46
            case '3':
131 1
                $frete = self::FRETE_PROPRIOREMETENTE;
132 1
                break;
133 46
            case '4':
134
                $frete = self::FRETE_PROPRIODESTINATARIO;
135
                break;
136 46
            case '9':
137 28
                $frete = self::FRETE_NENHUM;
138 28
                break;
139
        }
140 46
        $this->frete = $frete;
141 46
        return $this;
142
    }
143
144
    /**
145
     * Dados da transportadora
146
     */
147 14
    public function getTransportador()
148
    {
149 14
        return $this->transportador;
150
    }
151
152 46
    public function setTransportador($transportador)
153
    {
154 46
        $this->transportador = $transportador;
155 46
        return $this;
156
    }
157
158
    /**
159
     * Dados da retenção  ICMS do Transporte
160
     */
161 14
    public function getRetencao()
162
    {
163 14
        return $this->retencao;
164
    }
165
166 46
    public function setRetencao($retencao)
167
    {
168 46
        $this->retencao = $retencao;
169 46
        return $this;
170
    }
171
172
    /**
173
     * Dados do veículo
174
     */
175 14
    public function getVeiculo()
176
    {
177 14
        return $this->veiculo;
178
    }
179
180 46
    public function setVeiculo($veiculo)
181
    {
182 46
        $this->veiculo = $veiculo;
183 46
        return $this;
184
    }
185
186
    /**
187
     * Dados do reboque/Dolly (v2.0)
188
     */
189 14
    public function getReboque()
190
    {
191 14
        return $this->reboque;
192
    }
193
194 46
    public function setReboque($reboque)
195
    {
196 46
        $this->reboque = $reboque;
197 46
        return $this;
198
    }
199
200
    /**
201
     * Identificação do vagão (v2.0)
202
     */
203 14
    public function getVagao($normalize = false)
204
    {
205 14
        if (!$normalize) {
206 14
            return $this->vagao;
207
        }
208 6
        return $this->vagao;
209
    }
210
211 46
    public function setVagao($vagao)
212
    {
213 46
        $this->vagao = $vagao;
214 46
        return $this;
215
    }
216
217
    /**
218
     * Identificação da balsa (v2.0)
219
     */
220 14
    public function getBalsa($normalize = false)
221
    {
222 14
        if (!$normalize) {
223 14
            return $this->balsa;
224
        }
225 6
        return $this->balsa;
226
    }
227
228 46
    public function setBalsa($balsa)
229
    {
230 46
        $this->balsa = $balsa;
231 46
        return $this;
232
    }
233
234
    /**
235
     * Dados dos volumes
236
     */
237 14
    public function getVolumes()
238
    {
239 14
        return $this->volumes;
240
    }
241
242 46
    public function setVolumes($volumes)
243
    {
244 46
        $this->volumes = $volumes;
245 46
        return $this;
246
    }
247
248 3
    public function addVolume($volume)
249
    {
250 3
        $this->volumes[] = $volume;
251 3
        return $this;
252
    }
253
254 11
    public function toArray($recursive = false)
0 ignored issues
show
Complexity introduced by
This operation has 243 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
255
    {
256 11
        $transporte = [];
257 11
        $transporte['frete'] = $this->getFrete();
258 11
        if (!is_null($this->getTransportador()) && $recursive) {
259 1
            $transporte['transportador'] = $this->getTransportador()->toArray($recursive);
260
        } else {
261 10
            $transporte['transportador'] = $this->getTransportador();
262
        }
263 11
        if (!is_null($this->getRetencao()) && $recursive) {
264 1
            $transporte['retencao'] = $this->getRetencao()->toArray($recursive);
265
        } else {
266 10
            $transporte['retencao'] = $this->getRetencao();
267
        }
268 11
        if (!is_null($this->getVeiculo()) && $recursive) {
269 1
            $transporte['veiculo'] = $this->getVeiculo()->toArray($recursive);
270
        } else {
271 10
            $transporte['veiculo'] = $this->getVeiculo();
272
        }
273 11
        if (!is_null($this->getReboque()) && $recursive) {
274 1
            $transporte['reboque'] = $this->getReboque()->toArray($recursive);
275
        } else {
276 10
            $transporte['reboque'] = $this->getReboque();
277
        }
278 11
        $transporte['vagao'] = $this->getVagao();
279 11
        $transporte['balsa'] = $this->getBalsa();
280 11
        if ($recursive) {
281 2
            $volumes = [];
282 2
            $_volumes = $this->getVolumes();
283 2
            foreach ($_volumes as $_volume) {
284
                $volumes[] = $_volume->toArray($recursive);
285
            }
286 2
            $transporte['volumes'] = $volumes;
287
        } else {
288 9
            $transporte['volumes'] = $this->getVolumes();
289
        }
290 11
        return $transporte;
291
    }
292
293 46
    public function fromArray($transporte = [])
0 ignored issues
show
Complexity introduced by
This operation has 30000 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
294
    {
295 46
        if ($transporte instanceof Transporte) {
296 9
            $transporte = $transporte->toArray();
297 46
        } elseif (!is_array($transporte)) {
298 3
            return $this;
299
        }
300 46
        if (!isset($transporte['frete'])) {
301 46
            $this->setFrete(self::FRETE_NENHUM);
302
        } else {
303 9
            $this->setFrete($transporte['frete']);
304
        }
305 46
        $this->setTransportador(
306 46
            new Transportador(
307 46
                isset($transporte['transportador']) ? $transporte['transportador'] : []
308
            )
309
        );
310 46
        $this->setRetencao(new Tributo(isset($transporte['retencao']) ? $transporte['retencao'] : []));
311 46
        $this->setVeiculo(new Veiculo(isset($transporte['veiculo']) ? $transporte['veiculo'] : []));
312 46
        $this->setReboque(new Veiculo(isset($transporte['reboque']) ? $transporte['reboque'] : []));
313 46
        if (isset($transporte['vagao'])) {
314 3
            $this->setVagao($transporte['vagao']);
315
        } else {
316 46
            $this->setVagao(null);
317
        }
318 46
        if (isset($transporte['balsa'])) {
319 3
            $this->setBalsa($transporte['balsa']);
320
        } else {
321 46
            $this->setBalsa(null);
322
        }
323 46
        if (!isset($transporte['volumes'])) {
324 46
            $this->setVolumes([]);
325
        } else {
326 9
            $this->setVolumes($transporte['volumes']);
327
        }
328 46
        return $this;
329
    }
330
331 40
    public function getNode($name = null)
0 ignored issues
show
Complexity introduced by
This operation has 1920 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
332
    {
333 40
        $dom = new \DOMDocument('1.0', 'UTF-8');
334 40
        $element = $dom->createElement(is_null($name)?'transp':$name);
335 40
        Util::appendNode($element, 'modFrete', $this->getFrete(true));
336 40
        if ($this->getFrete() == self::FRETE_NENHUM) {
337 34
            return $element;
338
        }
339 6
        if (!is_null($this->getTransportador())) {
340 6
            $transportador = $this->getTransportador()->getNode();
341 6
            $transportador = $dom->importNode($transportador, true);
342 6
            $element->appendChild($transportador);
343
        }
344 6
        if (!is_null($this->getRetencao())) {
345 6
            $retencao = $this->getRetencao()->getNode();
346 6
            $retencao = $dom->importNode($retencao, true);
347 6
            $element->appendChild($retencao);
348
        }
349 6
        if (!is_null($this->getVeiculo())) {
350 6
            $veiculo = $this->getVeiculo()->getNode('veicTransp');
351 6
            $veiculo = $dom->importNode($veiculo, true);
352 6
            $element->appendChild($veiculo);
353
        }
354 6
        if (!is_null($this->getReboque())) {
355 6
            $reboque = $this->getReboque()->getNode('reboque');
356 6
            $reboque = $dom->importNode($reboque, true);
357 6
            $element->appendChild($reboque);
358
        }
359 6
        if (!is_null($this->getVagao())) {
360 6
            Util::appendNode($element, 'vagao', $this->getVagao(true));
361
        }
362 6
        if (!is_null($this->getBalsa())) {
363 6
            Util::appendNode($element, 'balsa', $this->getBalsa(true));
364
        }
365 6
        if (!is_null($this->getVolumes())) {
366 6
            $_volumes = $this->getVolumes();
367 6
            foreach ($_volumes as $_volume) {
368 6
                $volume = $_volume->getNode();
369 6
                $volume = $dom->importNode($volume, true);
370 6
                $element->appendChild($volume);
371
            }
372
        }
373 6
        return $element;
374
    }
375
376 33
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 480 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
377
    {
378 33
        $name = is_null($name)?'transp':$name;
379 33
        if ($element->nodeName != $name) {
380 2
            $_fields = $element->getElementsByTagName($name);
381 2
            if ($_fields->length == 0) {
382 1
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
383
            }
384 1
            $element = $_fields->item(0);
385
        }
386 32
        $this->setFrete(
387 32
            Util::loadNode(
388 32
                $element,
389 32
                'modFrete',
390 32
                'Tag "modFrete" do campo "Frete" não encontrada'
391
            )
392
        );
393 31
        $_fields = $element->getElementsByTagName('transporta');
394 31
        $transportador = null;
395 31
        if ($_fields->length > 0) {
396 3
            $transportador = new Transportador();
397 3
            $transportador->loadNode($_fields->item(0), 'transporta');
398
        }
399 31
        $this->setTransportador($transportador);
400 31
        $_fields = $element->getElementsByTagName('retTransp');
401 31
        $retencao = null;
402 31
        if ($_fields->length > 0) {
403 3
            $retencao = new Tributo();
404 3
            $retencao->loadNode($_fields->item(0), 'retTransp');
405
        }
406 31
        $this->setRetencao($retencao);
407 31
        $_fields = $element->getElementsByTagName('veicTransp');
408 31
        $veiculo = null;
409 31
        if ($_fields->length > 0) {
410 3
            $veiculo = new Veiculo();
411 3
            $veiculo->loadNode($_fields->item(0), 'veicTransp');
412
        }
413 31
        $this->setVeiculo($veiculo);
414 31
        $_fields = $element->getElementsByTagName('reboque');
415 31
        $reboque = null;
416 31
        if ($_fields->length > 0) {
417 3
            $reboque = new Veiculo();
418 3
            $reboque->loadNode($_fields->item(0), 'reboque');
419
        }
420 31
        $this->setReboque($reboque);
421 31
        $this->setVagao(Util::loadNode($element, 'vagao'));
422 31
        $this->setBalsa(Util::loadNode($element, 'balsa'));
423 31
        $volumes = [];
424 31
        $_fields = $element->getElementsByTagName('vol');
425 31
        foreach ($_fields as $_item) {
426 3
            $volume = new Volume();
427 3
            $volume->loadNode($_item, 'vol');
428 3
            $volumes[] = $volume;
429
        }
430 31
        $this->setVolumes($volumes);
431 31
        return $element;
432
    }
433
}
434