Completed
Push — master ( f6da78...86bb06 )
by Francimar
03:45
created

Transportador::loadNode()   C

Complexity

Conditions 13
Paths 170

Size

Total Lines 73
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 15.0428

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 47
cts 61
cp 0.7705
rs 5.0346
c 0
b 0
f 0
cc 13
eloc 59
nc 170
nop 2
crap 15.0428

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\Common\Util;
31
use NFe\Entity\Destinatario;
32
33
/**
34
 * Dados da transportadora
35
 */
36
class Transportador extends Destinatario
37
{
38
39 23
    public function __construct($transportador = array())
40
    {
41 23
        parent::__construct($transportador);
42 23
    }
43
44 1
    public function toArray()
45
    {
46 1
        $transportador = parent::toArray();
47 1
        return $transportador;
48
    }
49
50 23
    public function fromArray($transportador = array())
51
    {
52 23
        if ($transportador instanceof Transportador) {
53 1
            $transportador = $transportador->toArray();
54 23
        } elseif (!is_array($transportador)) {
55 1
            return $this;
56
        }
57 23
        parent::fromArray($transportador);
58 23
        return $this;
59
    }
60
61 10
    public function getNode($name = null)
62
    {
63 10
        $dom = new \DOMDocument('1.0', 'UTF-8');
64 10
        $element = $dom->createElement(is_null($name)?'transporta':$name);
65 10
        if (!is_null($this->getCNPJ())) {
66 10
            Util::appendNode($element, 'CNPJ', $this->getCNPJ(true));
67 10
        } else {
68
            Util::appendNode($element, 'CPF', $this->getCPF(true));
69
        }
70 10
        if (!is_null($this->getCNPJ())) {
71 10
            Util::appendNode($element, 'xNome', $this->getRazaoSocial(true));
72 10
        } else {
73
            Util::appendNode($element, 'xNome', $this->getNome(true));
74
        }
75 10
        if (!is_null($this->getCNPJ())) {
76 10
            Util::appendNode($element, 'IE', $this->getIE(true));
77 10
        }
78 10
        if (!is_null($this->getEndereco())) {
79 8
            $endereco = $this->getEndereco();
80 8
            Util::appendNode($element, 'xEnder', $endereco->getDescricao(true));
81 8
            Util::appendNode($element, 'xMun', $endereco->getMunicipio()->getNome(true));
82 8
            Util::appendNode($element, 'UF', $endereco->getMunicipio()->getEstado()->getUF(true));
83 8
        }
84 10
        return $element;
85
    }
86
87 5
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 4320 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...
88
    {
89 5
        $name = is_null($name)?'transporta':$name;
90 5
        if ($element->tagName != $name) {
91
            $_fields = $element->getElementsByTagName($name);
92
            if ($_fields->length == 0) {
93
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
94
            }
95
            $element = $_fields->item(0);
96
        }
97 5
        $cnpj = null;
98 5
        $cpf = null;
99 5
        $_fields = $element->getElementsByTagName('CNPJ');
100 5
        if ($_fields->length == 0) {
101
            $_fields = $element->getElementsByTagName('CPF');
102
        }
103 5
        if ($_fields->length > 0) {
104 5
            if ($_fields->item(0)->tagName == 'CNPJ') {
105 5
                $cnpj = $_fields->item(0)->nodeValue;
106 5
            } else {
107
                $cpf = $_fields->item(0)->nodeValue;
108
            }
109 5
        } else {
110
            throw new \Exception('Tag "CNPJ ou CPF" do campo "CNPJ ou CPF" não encontrada', 404);
111
        }
112 5
        $this->setCNPJ($cnpj);
113 5
        $this->setCPF($cpf);
114 5
        $_fields = $element->getElementsByTagName('xNome');
115 5
        if ($_fields->length > 0) {
116 5
            $nome = $_fields->item(0)->nodeValue;
117 5
        } elseif (!is_null($this->getCNPJ())) {
118
            throw new \Exception('Tag "xNome" do campo "RazaoSocial" não encontrada', 404);
119
        } else {
120
            throw new \Exception('Tag "xNome" do campo "Nome" não encontrada', 404);
121
        }
122 5
        if (!is_null($this->getCNPJ())) {
123 5
            $this->setRazaoSocial($nome);
124 5
        } else {
125
            $this->setNome($nome);
126
        }
127 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...
128 5
        $this->setIE(
129 5
            Util::loadNode(
130 5
                $element,
131 5
                'IE',
132
                'Tag "IE" do campo "IE" não encontrada'
133 5
            )
134 5
        );
135 5
        $this->setIM(null);
136 5
        $_fields = $element->getElementsByTagName('xEnder');
137 5
        if ($_fields->length == 0) {
138 1
            $this->setEndereco(null);
139 1
            return $element;
140
        }
141 4
        $endereco = new \NFe\Entity\Endereco();
142 4
        $endereco->parseDescricao($_fields->item(0)->nodeValue);
143 4
        $_fields = $element->getElementsByTagName('xMun');
144 4
        if ($_fields->length > 0) {
145 4
            $nome_municipio = $_fields->item(0)->nodeValue;
146 4
        } else {
147
            throw new \Exception('Tag "xMun" do nome do município não encontrada', 404);
148
        }
149 4
        $endereco->getMunicipio()->setNome($nome_municipio);
150 4
        $_fields = $element->getElementsByTagName('UF');
151 4
        if ($_fields->length > 0) {
152 4
            $uf_estado = $_fields->item(0)->nodeValue;
153 4
        } else {
154
            throw new \Exception('Tag "UF" da UF do estado não encontrada', 404);
155
        }
156 4
        $endereco->getMunicipio()->getEstado()->setUF($uf_estado);
157 4
        $this->setEndereco($endereco);
158 4
        return $element;
159
    }
160
}
161