Completed
Push — master ( 61245a...b8adf7 )
by Francimar
12:38
created

Endereco   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.89%

Importance

Changes 0
Metric Value
wmc 48
lcom 1
cbo 3
dl 0
loc 305
ccs 187
cts 193
cp 0.9689
rs 8.4864
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPais() 0 4 1
A setPais() 0 5 1
A getCEP() 0 7 2
A setCEP() 0 5 1
A getMunicipio() 0 4 1
A setMunicipio() 0 5 1
A getBairro() 0 7 2
A setBairro() 0 5 1
A getLogradouro() 0 7 2
A setLogradouro() 0 5 1
A getNumero() 0 7 2
A setNumero() 0 5 1
A getComplemento() 0 7 2
A setComplemento() 0 5 1
A getDescricao() 0 4 1
A parseDescricao() 0 11 2
A checkCodigos() 0 5 1
B toArray() 0 20 5
F fromArray() 0 44 12
A getNode() 0 20 3
B loadNode() 0 76 4

How to fix   Complexity   

Complex Class

Complex classes like Endereco often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Endereco, and based on these observations, apply Extract Interface, too.

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
33
/**
34
 * Informação de endereço que será informado nos clientes e no emitente
35
 */
36
class Endereco implements Node
37
{
38
39
    private $pais;
40
    private $cep;
41
    private $municipio;
42
    private $bairro;
43
    private $logradouro;
44
    private $numero;
45
    private $complemento;
46
47 93
    public function __construct($endereco = array())
48
    {
49 93
        $this->fromArray($endereco);
50 93
    }
51
52 36
    public function getPais()
53
    {
54 36
        return $this->pais;
55
    }
56
57 93
    public function setPais($pais)
58
    {
59 93
        $this->pais = $pais;
60 93
        return $this;
61
    }
62
63 36
    public function getCEP($normalize = false)
64
    {
65 36
        if (!$normalize) {
66 8
            return $this->cep;
67
        }
68 35
        return $this->cep;
69
    }
70
71 93
    public function setCEP($cep)
72
    {
73 93
        $this->cep = $cep;
74 93
        return $this;
75
    }
76
77 60
    public function getMunicipio()
78
    {
79 60
        return $this->municipio;
80
    }
81
82 93
    public function setMunicipio($municipio)
83
    {
84 93
        $this->municipio = $municipio;
85 93
        return $this;
86
    }
87
88 43
    public function getBairro($normalize = false)
89
    {
90 43
        if (!$normalize) {
91 15
            return $this->bairro;
92
        }
93 35
        return $this->bairro;
94
    }
95
96 93
    public function setBairro($bairro)
97
    {
98 93
        $this->bairro = $bairro;
99 93
        return $this;
100
    }
101
102 43
    public function getLogradouro($normalize = false)
103
    {
104 43
        if (!$normalize) {
105 15
            return $this->logradouro;
106
        }
107 35
        return $this->logradouro;
108
    }
109
110 93
    public function setLogradouro($logradouro)
111
    {
112 93
        $this->logradouro = $logradouro;
113 93
        return $this;
114
    }
115
116 43
    public function getNumero($normalize = false)
117
    {
118 43
        if (!$normalize) {
119 15
            return $this->numero;
120
        }
121 35
        return $this->numero;
122
    }
123
124 93
    public function setNumero($numero)
125
    {
126 93
        $this->numero = $numero;
127 93
        return $this;
128
    }
129
130 36
    public function getComplemento($normalize = false)
131
    {
132 36
        if (!$normalize) {
133 36
            return $this->complemento;
134
        }
135 2
        return $this->complemento;
136
    }
137
138 93
    public function setComplemento($complemento)
139
    {
140 93
        $this->complemento = $complemento;
141 93
        return $this;
142
    }
143
144
    /**
145
     * Obtém as informações básicas do endereço em uma linha de texto
146
     * @param  boolean $normalize informa se o valor deve ser normalizado para um XML
147
     * @return string             endereço com logradouro, número e bairro
148
     */
149 8
    public function getDescricao($normalize = false)
0 ignored issues
show
Unused Code introduced by
The parameter $normalize is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
    {
151 8
        return $this->getLogradouro().', '.$this->getNumero().' - '.$this->getBairro();
152
    }
153
154
    /**
155
     * Desmembra a descrição e salva as informações do endereço em seu respectivo campo
156
     * @param  string $descricao linha de endereço com diversas informações
157
     * @return Endereco retorna a própria instância
158
     */
159 4
    public function parseDescricao($descricao)
160
    {
161 4
        $pattern = '/(.*), (.*) - (.*)/';
162 4
        if (!preg_match($pattern, $descricao, $matches)) {
163
            throw new \Exception('Não foi possível desmembrar a linha de endereço', 500);
164
        }
165 4
        $this->setLogradouro($matches[1]);
166 4
        $this->setNumero($matches[2]);
167 4
        $this->setBairro($matches[3]);
168 4
        return $this;
169
    }
170
171 8
    public function toArray($recursive = false)
172
    {
173 8
        $endereco = array();
174 8
        if (!is_null($this->getPais()) && $recursive) {
175 1
            $endereco['pais'] = $this->getPais()->toArray($recursive);
176 1
        } else {
177 7
            $endereco['pais'] = $this->getPais();
178
        }
179 8
        $endereco['cep'] = $this->getCEP();
180 8
        if (!is_null($this->getMunicipio()) && $recursive) {
181 1
            $endereco['municipio'] = $this->getMunicipio()->toArray($recursive);
182 1
        } else {
183 7
            $endereco['municipio'] = $this->getMunicipio();
184
        }
185 8
        $endereco['bairro'] = $this->getBairro();
186 8
        $endereco['logradouro'] = $this->getLogradouro();
187 8
        $endereco['numero'] = $this->getNumero();
188 8
        $endereco['complemento'] = $this->getComplemento();
189 8
        return $endereco;
190
    }
191
192 93
    public function fromArray($endereco = array())
0 ignored issues
show
Complexity introduced by
This operation has 864 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...
193
    {
194 93
        if ($endereco instanceof Endereco) {
195 7
            $endereco = $endereco->toArray();
196 93
        } elseif (!is_array($endereco)) {
197 7
            return $this;
198
        }
199 93
        if (!isset($endereco['pais']) || is_null($endereco['pais'])) {
200 93
            $this->setPais(new Pais(array('codigo' => 1058, 'nome' => 'Brasil')));
201 93
        } else {
202 7
            $this->setPais($endereco['pais']);
203
        }
204 93
        if (isset($endereco['cep'])) {
205 7
            $this->setCEP($endereco['cep']);
206 7
        } else {
207 93
            $this->setCEP(null);
208
        }
209 93
        if (!isset($endereco['municipio']) || is_null($endereco['municipio'])) {
210 93
            $this->setMunicipio(new Municipio());
211 93
        } else {
212 7
            $this->setMunicipio($endereco['municipio']);
213
        }
214 93
        if (isset($endereco['bairro'])) {
215 7
            $this->setBairro($endereco['bairro']);
216 7
        } else {
217 93
            $this->setBairro(null);
218
        }
219 93
        if (isset($endereco['logradouro'])) {
220 7
            $this->setLogradouro($endereco['logradouro']);
221 7
        } else {
222 93
            $this->setLogradouro(null);
223
        }
224 93
        if (isset($endereco['numero'])) {
225 7
            $this->setNumero($endereco['numero']);
226 7
        } else {
227 93
            $this->setNumero(null);
228
        }
229 93
        if (isset($endereco['complemento'])) {
230 1
            $this->setComplemento($endereco['complemento']);
231 1
        } else {
232 93
            $this->setComplemento(null);
233
        }
234 93
        return $this;
235
    }
236
237 35
    public function checkCodigos()
238
    {
239 35
        $this->getMunicipio()->checkCodigos();
240 35
        $this->getMunicipio()->getEstado()->checkCodigos();
241 35
    }
242
243 35
    public function getNode($name = null)
244
    {
245 35
        $dom = new \DOMDocument('1.0', 'UTF-8');
246 35
        $this->checkCodigos();
247 35
        $element = $dom->createElement(is_null($name)?'enderEmit':$name);
248 35
        Util::appendNode($element, 'xLgr', $this->getLogradouro(true));
249 35
        Util::appendNode($element, 'nro', $this->getNumero(true));
250 35
        if (!is_null($this->getComplemento())) {
251 2
            Util::appendNode($element, 'xCpl', $this->getComplemento(true));
252 2
        }
253 35
        Util::appendNode($element, 'xBairro', $this->getBairro(true));
254 35
        Util::appendNode($element, 'cMun', $this->getMunicipio()->getCodigo(true));
255 35
        Util::appendNode($element, 'xMun', $this->getMunicipio()->getNome(true));
256 35
        Util::appendNode($element, 'UF', $this->getMunicipio()->getEstado()->getUF(true));
257 35
        Util::appendNode($element, 'CEP', $this->getCEP(true));
258 35
        Util::appendNode($element, 'cPais', $this->getPais()->getCodigo(true));
259 35
        Util::appendNode($element, 'xPais', $this->getPais()->getNome(true));
260
        // Util::appendNode($element, 'fone', $this->getTelefone(true));
261 35
        return $element;
262
    }
263
264 29
    public function loadNode($element, $name = null)
265
    {
266 29
        $name = is_null($name)?'enderEmit':$name;
267 29
        if ($element->nodeName != $name) {
268
            $_fields = $element->getElementsByTagName($name);
269
            if ($_fields->length == 0) {
270
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
271
            }
272
            $element = $_fields->item(0);
273
        }
274 29
        $this->setLogradouro(
275 29
            Util::loadNode(
276 29
                $element,
277 29
                'xLgr',
278
                'Tag "xLgr" do campo "Logradouro" não encontrada'
279 29
            )
280 29
        );
281 29
        $this->setNumero(
282 29
            Util::loadNode(
283 29
                $element,
284 29
                'nro',
285
                'Tag "nro" do campo "Numero" não encontrada'
286 29
            )
287 29
        );
288 29
        $this->setComplemento(Util::loadNode($element, 'xCpl'));
289 29
        $this->setBairro(
290 29
            Util::loadNode(
291 29
                $element,
292 29
                'xBairro',
293
                'Tag "xBairro" do campo "Bairro" não encontrada'
294 29
            )
295 29
        );
296 29
        $this->getMunicipio()->setCodigo(
297 29
            Util::loadNode(
298 29
                $element,
299 29
                'cMun',
300
                'Tag "cMun" do objeto "Municipio" não encontrada'
301 29
            )
302 29
        );
303 29
        $this->getMunicipio()->setNome(
304 29
            Util::loadNode(
305 29
                $element,
306 29
                'xMun',
307
                'Tag "xMun" do objeto "Municipio" não encontrada'
308 29
            )
309 29
        );
310 29
        $this->getMunicipio()->getEstado()->setUF(
311 29
            Util::loadNode(
312 29
                $element,
313 29
                'UF',
314
                'Tag "UF" do objeto "Estado" não encontrada'
315 29
            )
316 29
        );
317 29
        $this->setCEP(
318 29
            Util::loadNode(
319 29
                $element,
320 29
                'CEP',
321
                'Tag "CEP" do campo "CEP" não encontrada'
322 29
            )
323 29
        );
324 29
        $this->getPais()->setCodigo(
325 29
            Util::loadNode(
326 29
                $element,
327 29
                'cPais',
328
                'Tag "cPais" do objeto "Pais" não encontrada'
329 29
            )
330 29
        );
331 29
        $this->getPais()->setNome(
332 29
            Util::loadNode(
333 29
                $element,
334 29
                'xPais',
335
                'Tag "xPais" do objeto "Pais" não encontrada'
336 29
            )
337 29
        );
338 29
        return $element;
339
    }
340
}
341