Completed
Pull Request — master (#46)
by Antonio Oertel
04:53
created

Identify::identificacao()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 41
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 11.3027

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
ccs 26
cts 34
cp 0.7647
rs 4.8196
cc 10
eloc 35
nc 13
nop 2
crap 11.3027

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
namespace NFePHP\Common\Identify;
4
5
/**
6
 * Classe auxiliar para a identificação dos documentos eletrônicos
7
 * @category   NFePHP
8
 * @package    NFePHP\Common\Identify
9
 * @copyright  Copyright (c) 2008-2015
10
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
11
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
12
 * @link       http://github.com/nfephp-org/nfephp for the canonical source repository
13
 */
14
15
use NFePHP\Common\Dom\Dom;
16
use NFePHP\Common\Files\FilesFolders;
17
18
class Identify
19
{
20
    /**
21
     * Lista com a identificação das TAGs principais que identificam o documento
22
     * e o respectivo arquivo xsd
23
     * @var array
24
     */
25
    protected static $schemesId = array();
26
    
27
    /**
28
     * setListSchemesId
29
     * @param array $aList
30
     */
31 1
    public static function setListSchemesId($aList = array())
32
    {
33 1
        if (count($aList) > 0) {
34 1
            self::$schemesId = $aList;
35
        }
36 1
    }
37
    
38
    /**
39
     * identificacao
40
     * Identifica o documento
41
     * @param type $xml
42
     * @return string
43
     */
44 1
    public static function identificacao($xml = '', &$aResp = array())
45
    {
46 1
        if ($xml == '') {
47
            return '';
48 1
        } elseif (is_file($xml)) {
49 1
            $xml = FilesFolders::readFile($xml);
50
        }
51 1
        $dom = new Dom('1.0', 'utf-8');
52 1
        $dom->loadXMLString($xml);
53 1
        $key = '';
54 1
        $schId = (string) self::zSearchNode($dom, $key);
0 ignored issues
show
Documentation introduced by
$dom is of type object<NFePHP\Common\Dom\Dom>, but the function expects a object<NFePHP\Common\Identify\DOMDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55 1
        if ($schId == '') {
56
            return '';
57
        }
58 1
        $chave = '';
59 1
        $tpAmb = '';
60 1
        $dhEmi = '';
61 1
        if ($schId == 'nfe' || $schId == 'cte' || $schId == 'mdfe') {
62
            switch ($schId) {
63 1
                case 'nfe':
64 1
                    $tag = 'infNFe';
65 1
                    break;
66
                case 'cte':
67
                    $tag = 'infCTe';
68
                    break;
69
                case 'mdfe':
70
                    $tag = 'infMDFe';
71
                    break;
72
            }
73 1
            $chave = $dom->getChave($tag);
0 ignored issues
show
Bug introduced by
The variable $tag does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
74 1
            $tpAmb = $dom->getNodeValue('tpAmb');
75 1
            $dhEmi = $dom->getNodeValue('dhEmi');
76
        }
77 1
        $aResp['Id'] =  $schId;
78 1
        $aResp['tag'] =  $key;
79 1
        $aResp['dom'] = $dom;
80 1
        $aResp['chave'] = $chave;
81 1
        $aResp['tpAmb'] = $tpAmb;
82 1
        $aResp['dhEmi'] = $dhEmi;
83 1
        return $schId;
84
    }
85
    
86
    /**
87
     * zSearchNode
88
     * @param DOMDocument $dom
89
     * @return string
90
     */
91 1
    protected static function zSearchNode($dom, &$key)
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
92
    {
93 1
        foreach (self::$schemesId as $key => $schId) {
94 1
            $node = $dom->getElementsByTagName($key)->item(0);
95 1
            if (! empty($node)) {
96 1
                return $schId;
97
            }
98
        }
99
        return '';
100
    }
101
}
102