Passed
Push — master ( e6d6d0...8c02e1 )
by Roberto
03:19 queued 12s
created

Make::parse()   F

Complexity

Conditions 13
Paths 1046

Size

Total Lines 124

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 0
cts 97
cp 0
rs 1.96
c 0
b 0
f 0
cc 13
nc 1046
nop 0
crap 182

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
namespace NFePHP\BPe;
4
5
use NFePHP\BPe\Common\Make as MakeBase;
6
use NFePHP\Common\Keys;
7
8
class Make extends MakeBase
9
{
10
    protected $rootname = 'BPe';
11
    protected $versao = '1.00';
12
    protected $xmlns = 'http://www.portalfiscal.inf.br/bpe';
13
    
14
    protected $available = [
15
        'taginfbpe' => ['class' => Tags\InfBpe::class, 'type' => 'single', 'occurrence' => [0, 1]],
16
        'tagide' => ['class' => Tags\Ide::class, 'type' => 'single', 'occurrence' => [1, 1]],
17
        'tagemit' => ['class' => Tags\Emit::class, 'type' => 'single', 'occurrence' => [1, 1]],
18
        'tagenderemit' => ['class' => Tags\EnderEmit::class, 'type' => 'single', 'occurrence' => [1, 1]],
19
        'tagcomp' => ['class' => Tags\Comp::class, 'type' => 'single', 'occurrence' => [0, 1]],
20
        'tagendercomp' => ['class' => Tags\EnderComp::class, 'type' => 'single', 'occurrence' => [0, 1]],
21
        'tagagencia' => ['class' => Tags\Agencia::class, 'type' => 'single', 'occurrence' => [0, 1]],
22
        'tagenderagencia' => ['class' => Tags\EnderAgencia::class, 'type' => 'single', 'occurrence' => [0, 1]],
23
        'taginfbpesub' => ['class' => Tags\InfBpeSub::class, 'type' => 'single', 'occurrence' => [0, 1]],
24
        'taginfpassagem' => ['class' => Tags\InfPassagem::class, 'type' => 'single', 'occurrence' => [1, 1]],
25
        'taginfpassageiro' => ['class' => Tags\InfPassageiro::class, 'type' => 'single', 'occurrence' => [0, 1]],
26
        'taginfviagem' => ['class' => Tags\InfViagem::class, 'type' => 'multiple', 'occurrence' => [1, 9999]],
27
        'taginfvalorbpe' => ['class' => Tags\InfValorBPe::class, 'type' => 'single', 'occurrence' => [1, 1]],
28
        'tagcompvalor' => ['class' => Tags\CompValor::class, 'type' => 'multiple', 'occurrence' => [1, 999]],
29
        'tagicms' => ['class' => Tags\Icms::class, 'type' => 'single', 'occurrence' => [0, 1]],
30
        'tagicmsuffim' => ['class' => Tags\IcmsUfFim::class, 'type' => 'single', 'occurrence' => [0, 1]],
31
        'tagpag' => ['class' => Tags\Pag::class, 'type' => 'multiple', 'occurrence' => [1, 10]],
32
        'tagautxml' => ['class' => Tags\AutXML::class, 'type' => 'multiple', 'occurrence' => [0, 10]],
33
        'taginfadic' => ['class' => Tags\InfAdic::class, 'type' => 'single', 'occurrence' => [0, 1]],
34
        'tagresptec' => ['class' => Tags\RespTec::class, 'type' => 'single', 'occurrence' => [0, 1]],
35
        'taginfbpesupl' => ['class' => Tags\InfBPeSupl::class, 'type' => 'single', 'occurrence' => [0, 1]],
36
    ];
37
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->createEmptyProperties();
42
    }
43
    
44
    /**
45
     * Convert all subclasse to DOMNodes and creates entire XML
46
     * NOTE: append order is very important
47
     * @return string
48
     */
49
    public function parse()
50
    {
51
        //valida se algum objeto não foi passado ou se supera o limite estabelecido
52
        $this->validateDataObjects();
53
        try {
54
            //infBPe tag  OBRIGATÓRIA mas pode ser construida a partir de outras
55
            if (!empty($this->ide) && !empty($this->emit)) {
56
                //construir a chave e montar a tag
57
                $data = \DateTime::createFromFormat('Y-m-d\TH:i:sP', $this->ide->std->dhemi);
0 ignored issues
show
Bug introduced by
The property ide does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
                $buildId = Keys::build(
59
                    $this->ide->std->cuf,
60
                    $data->format('y'),
61
                    $data->format('m'),
62
                    substr($this->emit->std->cnpj, 0, 14),
0 ignored issues
show
Bug introduced by
The property emit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
                    '63',
64
                    intval($this->ide->std->serie),
65
                    intval($this->ide->std->nbp),
66
                    intval($this->ide->std->tpemis),
67
                    intval($this->ide->std->cbp)
68
                );
69
                if (empty($this->infbpe)) {
70
                    $this->ide->std->cdv = substr($buildId, -1);
71
                    $std = (object) ['Id' => $buildId, 'versao' => $this->versao];
72
                    $class = $this->available["taginfbpe"]['class'];
73
                    $this->createProperty('infbpe', $this->loadTagClass($class, [$std]));
74
                    $this->chave = $buildId;
75
                } else {
76
                    if (!empty($this->infbpe->std->id)) {
77
                        if ($buildId != $this->infbpe->std->id) {
0 ignored issues
show
Bug introduced by
The property infbpe does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
                            $this->errors[] = "Chave passada na tagInfBPe está incorreta! Chave correta [$buildId].";
79
                        }
80
                        $this->chave = $buildId;
81
                    } else {
82
                        $this->infbpe->std->id = $buildId;
83
                        $this->infbpe->std->versao = !empty($this->infbpe->std->versao)
84
                            ? $this->infbpe->std->versao
85
                            : $this->versao;
86
                        $this->ide->std->cdv = substr($buildId, -1);
87
                    }
88
                }
89
            }
90
            //cria o hashCSRT geralmente é necessário quando não é criada a chave
91
            //durante a montagem com a classe make
92
            if (!empty($this->resptec && $this->resptec->stdsubnode->csrt != null)) {
93
                $this->resptec->std->hashcsrt = $this->hashCSRT(
0 ignored issues
show
Bug introduced by
The property resptec does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
94
                    $this->resptec->stdsubnode->csrt,
95
                    $buildId
0 ignored issues
show
Bug introduced by
The variable $buildId 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...
96
                );
97
            }
98
            
99
            
100
            //caso não seja criada a tag infBPe
101
            if (empty($this->infbpe)) {
102
                throw new \Exception('A Tag infBPE é obrigatória.');
103
            }
104
            //cria o node infBPe
105
            $infBPe = $this->infbpe->toNode();
106
            //cria Ide
107
            $this->appendNodeToParent($infBPe, $this->ide);
108
            //se existir a tag TTAR o endereço deve ser colocado antes dessa tag
109
            if (!empty($this->emit->std->tar)) {
110
                $node = $this->emit->toNode();
111
                $subnode = $this->enderemit->toNode();
0 ignored issues
show
Bug introduced by
The property enderemit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
112
                $this->dom->appChildBefore($node, $subnode, 'TAR');
113
                $this->dom->appChild($infBPe, $node);
114
            } else {
115
                //cria emit e enderemit
116
                $this->appendNodeToParent($infBPe, $this->emit, $this->enderemit);
117
            }
118
            //cria comp
119
            $this->appendNodeToParent($infBPe, $this->comp, $this->endercomp);
0 ignored issues
show
Bug introduced by
The property comp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property endercomp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
120
            //cria agencia
121
            $this->appendNodeToParent($infBPe, $this->agencia, $this->enderagencia);
0 ignored issues
show
Bug introduced by
The property agencia does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property enderagencia does not seem to exist. Did you mean agencia?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
122
            //cria infBPeSub apenas se for um BPe de substituição
123
            if ($this->ide->std->tpbpe == 3) {
124
                $this->appendNodeToParent($infBPe, $this->infbpesub);
0 ignored issues
show
Bug introduced by
The property infbpesub does not seem to exist. Did you mean infbpe?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
125
            }
126
            //cria infPassagem
127
            $this->appendNodeToParent($infBPe, $this->infpassagem, $this->infpassageiro);
0 ignored issues
show
Bug introduced by
The property infpassagem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property infpassageiro does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
128
            //cria infViagem
129
            $this->appendNodeToParent($infBPe, $this->infviagem);
0 ignored issues
show
Bug introduced by
The property infviagem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
130
            //cria infValorBPe
131
            $this->appendNodeToParent($infBPe, $this->infvalorbpe, $this->compvalor);
0 ignored issues
show
Bug introduced by
The property infvalorbpe does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property compvalor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
132
            //cria imp
133
            $imp = $this->dom->createElement('imp');
134
            //cria imp/ICMS
135
            $this->appendNodeToParent($imp, $this->icms);
0 ignored issues
show
Bug introduced by
The property icms does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
            
137
            $this->dom->addChild(
138
                $imp,
139
                'vTotTrib',
140
                $this->icms->std->vtottrib,
141
                false,
142
                ''
143
            );
144
            $this->dom->addChild(
145
                $imp,
146
                'infAdFisco',
147
                $this->icms->std->infadfisco,
148
                false,
149
                ''
150
            );
151
            //cria imp/ICMSUFFIM
152
            $this->appendNodeToParent($imp, $this->icmsuffim);
0 ignored issues
show
Bug introduced by
The property icmsuffim does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
153
            $this->dom->appChild($infBPe, $imp);
154
            //cria pag
155
            $this->appendNodeToParent($infBPe, $this->pag);
0 ignored issues
show
Bug introduced by
The property pag does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
156
            //cria autXML
157
            $this->appendNodeToParent($infBPe, $this->autxml);
0 ignored issues
show
Bug introduced by
The property autxml does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
158
            //cria infAdic
159
            $this->appendNodeToParent($infBPe, $this->infadic);
0 ignored issues
show
Bug introduced by
The property infadic does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
160
            //cria infRespTec
161
            $this->appendNodeToParent($infBPe, $this->resptec);
162
            $this->dom->appChild($this->BPe, $infBPe);
0 ignored issues
show
Bug introduced by
The property BPe does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
163
            //cria infBPeSupl
164
            $this->appendNodeToParent($this->BPe, $this->infbpesupl);
0 ignored issues
show
Bug introduced by
The property infbpesupl does not seem to exist. Did you mean infbpe?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
165
            $this->dom->appendChild($this->BPe);
166
            $this->xml = null;
167
            $this->xml = $this->dom->saveXML();
168
            return $this->xml;
169
        } catch (\ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
170
            throw new \Exception($e);
171
        }
172
    }
173
    
174
    /**
175
     * Append tags in parent with subtags
176
     * @param \DOMElement $parent
177
     * @param \NFePHP\BPe\Factories\TagInterface $nodes | null
178
     * @param \NFePHP\BPe\Factories\TagInterface $subnode | null
179
     * @return void
180
     * @throws \Exception
181
     */
182
    protected function appendNodeToParent($parent, $nodes, $subnode = null)
183
    {
184
        if (empty($nodes)) {
185
            return;
186
        }
187
        if (is_array($nodes)) {
188
            foreach ($nodes as $node) {
189
                $this->dom->appChild($parent, $node->toNode());
190
            }
191
        } elseif (is_object($nodes)) {
192
            if ($subnode != null) {
193
                $novonode = $nodes->toNode();
194
                if (is_array($subnode)) {
195
                    foreach ($subnode as $sub) {
196
                        $this->dom->appChild($novonode, $sub->toNode());
197
                    }
198
                } elseif (is_object($subnode)) {
199
                    $this->dom->appChild($novonode, $subnode->toNode());
200
                }
201
                $this->dom->appChild($parent, $novonode);
202
            } else {
203
                $this->dom->appChild($parent, $nodes->toNode());
204
            }
205
        } else {
206
            throw new \Exception('Algo errado no código.');
207
        }
208
    }
209
}
210