Conditions | 4 |
Paths | 5 |
Total Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | public static function adjust($xml, Contingency $contingency) |
||
19 | { |
||
20 | if ($contingency->type == '') { |
||
21 | return $xml; |
||
22 | } |
||
23 | $xml = Signer::removeSignature($xml); |
||
24 | |||
25 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
26 | $dom->preserveWhiteSpace = false; |
||
27 | $dom->formatOutput = false; |
||
28 | $dom->loadXML($xml); |
||
29 | |||
30 | $ide = $dom->getElementsByTagName('ide')->item(0); |
||
31 | $cUF = $ide->getElementsByTagName('cUF')->item(0)->nodeValue; |
||
32 | $cNF = $ide->getElementsByTagName('cCT')->item(0)->nodeValue; |
||
33 | $nNF = $ide->getElementsByTagName('nCT')->item(0)->nodeValue; |
||
34 | $serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue; |
||
35 | $mod = $ide->getElementsByTagName('mod')->item(0)->nodeValue; |
||
36 | $dtEmi = new DateTime($ide->getElementsByTagName('dhEmi')->item(0)->nodeValue); |
||
37 | $ano = $dtEmi->format('y'); |
||
38 | $mes = $dtEmi->format('m'); |
||
39 | $tpEmis = (string)$contingency->tpEmis; |
||
40 | $emit = $dom->getElementsByTagName('emit')->item(0); |
||
41 | $cnpj = $emit->getElementsByTagName('CNPJ')->item(0)->nodeValue; |
||
42 | |||
43 | $motivo = trim(Strings::replaceUnacceptableCharacters($contingency->motive)); |
||
44 | $dt = new DateTime(); |
||
45 | $dt->setTimestamp($contingency->timestamp); |
||
46 | $ide->getElementsByTagName('tpEmis') |
||
47 | ->item(0) |
||
48 | ->nodeValue = $contingency->tpEmis; |
||
49 | if (!empty($ide->getElementsByTagName('dhCont')->item(0)->nodeValue)) { |
||
50 | $ide->getElementsByTagName('dhCont') |
||
51 | ->item(0) |
||
52 | ->nodeValue = $dt->format('Y-m-d\TH:i:sP'); |
||
53 | } else { |
||
54 | $dhCont = $dom->createElement('dhCont', $dt->format('Y-m-d\TH:i:sP')); |
||
55 | $ide->appendChild($dhCont); |
||
56 | } |
||
57 | if (!empty($ide->getElementsByTagName('xJust')->item(0)->nodeValue)) { |
||
58 | $ide->getElementsByTagName('xJust')->item(0)->nodeValue = $motivo; |
||
59 | } else { |
||
60 | $xJust = $dom->createElement('xJust', $motivo); |
||
61 | $ide->appendChild($xJust); |
||
62 | } |
||
63 | //corrigir a chave |
||
64 | $infCte = $dom->getElementsByTagName('infCte')->item(0); |
||
65 | $chave = Keys::build( |
||
66 | $cUF, |
||
67 | $ano, |
||
68 | $mes, |
||
69 | $cnpj, |
||
70 | $mod, |
||
71 | $serie, |
||
72 | $nNF, |
||
73 | $tpEmis, |
||
74 | $cNF |
||
75 | ); |
||
76 | $ide->getElementsByTagName('cDV')->item(0)->nodeValue = substr($chave, -1); |
||
77 | $infCte->setAttribute('Id', 'CTe' . $chave); |
||
78 | return Strings::clearXmlString($dom->saveXML(), true); |
||
79 | } |
||
80 | } |
||
81 |