Completed
Push — master ( c523cc...f6da78 )
by Francimar
05:08
created

Transportador::loadNode()   D

Complexity

Conditions 14
Paths 202

Size

Total Lines 73
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 16.9142

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 46
cts 61
cp 0.7541
rs 4.9898
c 0
b 0
f 0
cc 14
eloc 60
nc 202
nop 2
crap 16.9142

How to fix   Long Method    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\Transporte;
29
30
use NFe\Entity\Destinatario;
31
32
/**
33
 * Dados da transportadora
34
 */
35
class Transportador extends Destinatario
36
{
37
38 23
    public function __construct($transportador = array())
39
    {
40 23
        parent::__construct($transportador);
41 23
    }
42
43 1
    public function toArray()
44
    {
45 1
        $transportador = parent::toArray();
46 1
        return $transportador;
47
    }
48
49 23
    public function fromArray($transportador = array())
50
    {
51 23
        if ($transportador instanceof Transportador) {
52 1
            $transportador = $transportador->toArray();
53 23
        } elseif (!is_array($transportador)) {
54 1
            return $this;
55
        }
56 23
        parent::fromArray($transportador);
57 23
        return $this;
58
    }
59
60 10
    public function getNode($name = null)
61
    {
62 10
        $dom = new \DOMDocument('1.0', 'UTF-8');
63 10
        $element = $dom->createElement(is_null($name)?'transporta':$name);
64 10
        if (!is_null($this->getCNPJ())) {
65 10
            $element->appendChild($dom->createElement('CNPJ', $this->getCNPJ(true)));
66 10
        } else {
67
            $element->appendChild($dom->createElement('CPF', $this->getCPF(true)));
68
        }
69 10
        if (!is_null($this->getCNPJ())) {
70 10
            $element->appendChild($dom->createElement('xNome', $this->getRazaoSocial(true)));
71 10
        } else {
72
            $element->appendChild($dom->createElement('xNome', $this->getNome(true)));
73
        }
74 10
        if (!is_null($this->getCNPJ())) {
75 10
            $element->appendChild($dom->createElement('IE', $this->getIE(true)));
76 10
        }
77 10
        if (!is_null($this->getEndereco())) {
78 8
            $endereco = $this->getEndereco();
79 8
            $element->appendChild($dom->createElement('xEnder', $endereco->getDescricao(true)));
80 8
            $element->appendChild($dom->createElement('xMun', $endereco->getMunicipio()->getNome(true)));
81 8
            $element->appendChild($dom->createElement('UF', $endereco->getMunicipio()->getEstado()->getUF(true)));
82 8
        }
83 10
        return $element;
84
    }
85
86 5
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 8640 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...
87
    {
88 5
        $name = is_null($name)?'transporta':$name;
89 5
        if ($element->tagName != $name) {
90
            $_fields = $element->getElementsByTagName($name);
91
            if ($_fields->length == 0) {
92
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
93
            }
94
            $element = $_fields->item(0);
95
        }
96 5
        $cnpj = null;
97 5
        $cpf = null;
98 5
        $_fields = $element->getElementsByTagName('CNPJ');
99 5
        if ($_fields->length == 0) {
100
            $_fields = $element->getElementsByTagName('CPF');
101
        }
102 5
        if ($_fields->length > 0) {
103 5
            if ($_fields->item(0)->tagName == 'CNPJ') {
104 5
                $cnpj = $_fields->item(0)->nodeValue;
105 5
            } else {
106
                $cpf = $_fields->item(0)->nodeValue;
107
            }
108 5
        } else {
109
            throw new \Exception('Tag "CNPJ ou CPF" do campo "CNPJ ou CPF" não encontrada', 404);
110
        }
111 5
        $this->setCNPJ($cnpj);
112 5
        $this->setCPF($cpf);
113 5
        $_fields = $element->getElementsByTagName('xNome');
114 5
        if ($_fields->length > 0) {
115 5
            $nome = $_fields->item(0)->nodeValue;
116 5
        } elseif (!is_null($this->getCNPJ())) {
117
            throw new \Exception('Tag "xNome" do campo "RazaoSocial" não encontrada', 404);
118
        } else {
119
            throw new \Exception('Tag "xNome" do campo "Nome" não encontrada', 404);
120
        }
121 5
        if (!is_null($this->getCNPJ())) {
122 5
            $this->setRazaoSocial($nome);
123 5
        } else {
124
            $this->setNome($nome);
125
        }
126 5
        $ie = null;
0 ignored issues
show
Unused Code introduced by
$ie 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...
Comprehensibility introduced by
Avoid variables with short names like $ie. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
127 5
        $_fields = $element->getElementsByTagName('IE');
128 5
        if ($_fields->length > 0) {
129 5
            $ie = $_fields->item(0)->nodeValue;
130 5
        } else {
131
            throw new \Exception('Tag "IE" do campo "IE" não encontrada', 404);
132
        }
133 5
        $this->setIE($ie);
134 5
        $this->setIM(null);
135 5
        $_fields = $element->getElementsByTagName('xEnder');
136 5
        if ($_fields->length == 0) {
137 1
            $this->setEndereco(null);
138 1
            return $element;
139
        }
140 4
        $endereco = new \NFe\Entity\Endereco();
141 4
        $endereco->parseDescricao($_fields->item(0)->nodeValue);
142 4
        $_fields = $element->getElementsByTagName('xMun');
143 4
        if ($_fields->length > 0) {
144 4
            $nome_municipio = $_fields->item(0)->nodeValue;
145 4
        } else {
146
            throw new \Exception('Tag "xMun" do nome do município não encontrada', 404);
147
        }
148 4
        $endereco->getMunicipio()->setNome($nome_municipio);
149 4
        $_fields = $element->getElementsByTagName('UF');
150 4
        if ($_fields->length > 0) {
151 4
            $uf_estado = $_fields->item(0)->nodeValue;
152 4
        } else {
153
            throw new \Exception('Tag "UF" da UF do estado não encontrada', 404);
154
        }
155 4
        $endereco->getMunicipio()->getEstado()->setUF($uf_estado);
156 4
        $this->setEndereco($endereco);
157 4
        return $element;
158
    }
159
}
160