Test Failed
Push — master ( 78dd3e...0fe426 )
by Roberto
04:43 queued 02:21
created

TraitS1020::toNodeS100()   C

Complexity

Conditions 14
Paths 40

Size

Total Lines 186

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 186
rs 5.0133
c 0
b 0
f 0
cc 14
nc 40
nop 0

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\eSocial\Factories\Traits;
4
5
trait TraitS1020
6
{
7
    /**
8
     * builder for version 2.5.0
9
     */
10
    protected function toNode250()
11
    {
12
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
0 ignored issues
show
Bug introduced by
The property node 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...
13
        //o idEvento pode variar de evento para evento
14
        //então cada factory individualmente terá de construir o seu
15
        $ideEvento = $this->dom->createElement("ideEvento");
0 ignored issues
show
Bug introduced by
The property dom 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...
16
        $this->dom->addChild(
17
            $ideEvento,
18
            "tpAmb",
19
            $this->tpAmb,
0 ignored issues
show
Bug introduced by
The property tpAmb 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...
20
            true
21
        );
22
        $this->dom->addChild(
23
            $ideEvento,
24
            "procEmi",
25
            $this->procEmi,
0 ignored issues
show
Bug introduced by
The property procEmi 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...
26
            true
27
        );
28
        $this->dom->addChild(
29
            $ideEvento,
30
            "verProc",
31
            $this->verProc,
0 ignored issues
show
Bug introduced by
The property verProc 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...
32
            true
33
        );
34
        $this->node->insertBefore($ideEvento, $ideEmpregador);
35
36
        $ide = $this->dom->createElement("ideLotacao");
37
        $this->dom->addChild(
38
            $ide,
39
            "codLotacao",
40
            $this->std->codlotacao,
0 ignored issues
show
Bug introduced by
The property std 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...
41
            true
42
        );
43
        $this->dom->addChild(
44
            $ide,
45
            "iniValid",
46
            $this->std->inivalid,
47
            true
48
        );
49
        $this->dom->addChild(
50
            $ide,
51
            "fimValid",
52
            ! empty($this->std->fimvalid) ? $this->std->fimvalid : null,
53
            false
54
        );
55
56
57
        if (!empty($this->std->dadoslotacao)) {
58
            $da = $this->std->dadoslotacao;
59
            $dados = $this->dom->createElement("dadosLotacao");
60
            $this->dom->addChild(
61
                $dados,
62
                "tpLotacao",
63
                $da->tplotacao,
64
                true
65
            );
66
            $this->dom->addChild(
67
                $dados,
68
                "tpInsc",
69
                !empty($da->tpinsc) ? $da->tpinsc : null,
70
                false
71
            );
72
            $this->dom->addChild(
73
                $dados,
74
                "nrInsc",
75
                !empty($da->nrinsc) ? $da->nrinsc : null,
76
                false
77
            );
78
            $fpasLotacao = $this->dom->createElement("fpasLotacao");
79
            $this->dom->addChild(
80
                $fpasLotacao,
81
                "fpas",
82
                $da->fpas,
83
                true
84
            );
85
            $this->dom->addChild(
86
                $fpasLotacao,
87
                "codTercs",
88
                $da->codtercs,
89
                true
90
            );
91
            $this->dom->addChild(
92
                $fpasLotacao,
93
                "codTercsSusp",
94
                !empty($da->codtercssusp) ? $da->codtercssusp : null,
95
                false
96
            );
97
98
            if (!empty($da->procjudterceiro)) {
99
                $procjud = $this->dom->createElement("infoProcJudTerceiros");
100
                foreach ($da->procjudterceiro as $proc) {
101
                    $pjt = $this->dom->createElement("procJudTerceiro");
102
                    $this->dom->addChild(
103
                        $pjt,
104
                        "codTerc",
105
                        $proc->codterc,
106
                        true
107
                    );
108
                    $this->dom->addChild(
109
                        $pjt,
110
                        "nrProcJud",
111
                        $proc->nrprocjud,
112
                        true
113
                    );
114
                    $this->dom->addChild(
115
                        $pjt,
116
                        "codSusp",
117
                        $proc->codsusp,
118
                        true
119
                    );
120
                    $procjud->appendChild($pjt);
121
                }
122
                $fpasLotacao->appendChild($procjud);
123
            }
124
            $dados->appendChild($fpasLotacao);
125
126
            if (!empty($da->infoemprparcial)) {
127
                $parcial = $this->dom->createElement("infoEmprParcial");
128
                $this->dom->addChild(
129
                    $parcial,
130
                    "tpInscContrat",
131
                    $da->infoemprparcial->tpinsccontrat,
132
                    true
133
                );
134
                $this->dom->addChild(
135
                    $parcial,
136
                    "nrInscContrat",
137
                    $da->infoemprparcial->nrinsccontrat,
138
                    true
139
                );
140
                $this->dom->addChild(
141
                    $parcial,
142
                    "tpInscProp",
143
                    $da->infoemprparcial->tpinscprop,
144
                    true
145
                );
146
                $this->dom->addChild(
147
                    $parcial,
148
                    "nrInscProp",
149
                    $da->infoemprparcial->nrinscprop,
150
                    true
151
                );
152
                $dados->appendChild($parcial);
153
            }
154
        }
155
156
        if (!empty($this->std->novavalidade)) {
157
            $nova = $this->dom->createElement("novaValidade");
158
            $this->dom->addChild(
159
                $nova,
160
                "iniValid",
161
                $this->std->novavalidade->inivalid,
162
                true
163
            );
164
            $this->dom->addChild(
165
                $nova,
166
                "fimValid",
167
                ! empty($this->std->novavalidade->fimvalid)
168
                    ? $this->std->novavalidade->fimvalid
169
                    : null,
170
                false
171
            );
172
        }
173
174
        $info = $this->dom->createElement("infoLotacao");
175
        //seleção do modo
176
        if ($this->std->modo == 'INC') {
177
            $node = $this->dom->createElement("inclusao");
178
            $node->appendChild($ide);
179
            $node->appendChild($dados);
0 ignored issues
show
Bug introduced by
The variable $dados 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...
180
        } elseif ($this->std->modo == 'ALT') {
181
            $node = $this->dom->createElement("alteracao");
182
            $node->appendChild($ide);
183
            $node->appendChild($dados);
184
            isset($nova) ? $node->appendChild($nova) : null;
185
        } else {
186
            $node = $this->dom->createElement("exclusao");
187
            $node->appendChild($ide);
188
        }
189
190
        $info->appendChild($node);
191
        $this->node->appendChild($info);
192
        $this->eSocial->appendChild($this->node);
0 ignored issues
show
Bug introduced by
The property eSocial 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...
193
        //$this->xml = $this->dom->saveXML($this->eSocial);
194
        $this->sign();
0 ignored issues
show
Bug introduced by
It seems like sign() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
195
    }
196
    
197
    /**
198
     * builder for version S.1.0.0
199
     */
200
    protected function toNodeS100()
201
    {
202
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
203
        //o idEvento pode variar de evento para evento
204
        //então cada factory individualmente terá de construir o seu
205
        $ideEvento = $this->dom->createElement("ideEvento");
206
        $this->dom->addChild(
207
            $ideEvento,
208
            "tpAmb",
209
            $this->tpAmb,
210
            true
211
        );
212
        $this->dom->addChild(
213
            $ideEvento,
214
            "procEmi",
215
            $this->procEmi,
216
            true
217
        );
218
        $this->dom->addChild(
219
            $ideEvento,
220
            "verProc",
221
            $this->verProc,
222
            true
223
        );
224
        $this->node->insertBefore($ideEvento, $ideEmpregador);
225
226
        $ide = $this->dom->createElement("ideLotacao");
227
        $this->dom->addChild(
228
            $ide,
229
            "codLotacao",
230
            $this->std->codlotacao,
231
            true
232
        );
233
        $this->dom->addChild(
234
            $ide,
235
            "iniValid",
236
            $this->std->inivalid,
237
            true
238
        );
239
        $this->dom->addChild(
240
            $ide,
241
            "fimValid",
242
            ! empty($this->std->fimvalid) ? $this->std->fimvalid : null,
243
            false
244
        );
245
246
247
        if (!empty($this->std->dadoslotacao)) {
248
            $da = $this->std->dadoslotacao;
249
            $dados = $this->dom->createElement("dadosLotacao");
250
            $this->dom->addChild(
251
                $dados,
252
                "tpLotacao",
253
                $da->tplotacao,
254
                true
255
            );
256
            $this->dom->addChild(
257
                $dados,
258
                "tpInsc",
259
                !empty($da->tpinsc) ? $da->tpinsc : null,
260
                false
261
            );
262
            $this->dom->addChild(
263
                $dados,
264
                "nrInsc",
265
                !empty($da->nrinsc) ? $da->nrinsc : null,
266
                false
267
            );
268
            $fpasLotacao = $this->dom->createElement("fpasLotacao");
269
            $this->dom->addChild(
270
                $fpasLotacao,
271
                "fpas",
272
                $da->fpas,
273
                true
274
            );
275
            $this->dom->addChild(
276
                $fpasLotacao,
277
                "codTercs",
278
                $da->codtercs,
279
                true
280
            );
281
            $this->dom->addChild(
282
                $fpasLotacao,
283
                "codTercsSusp",
284
                !empty($da->codtercssusp) ? $da->codtercssusp : null,
285
                false
286
            );
287
288
            if (!empty($da->procjudterceiro)) {
289
                $procjud = $this->dom->createElement("infoProcJudTerceiros");
290
                foreach ($da->procjudterceiro as $proc) {
291
                    $pjt = $this->dom->createElement("procJudTerceiro");
292
                    $this->dom->addChild(
293
                        $pjt,
294
                        "codTerc",
295
                        $proc->codterc,
296
                        true
297
                    );
298
                    $this->dom->addChild(
299
                        $pjt,
300
                        "nrProcJud",
301
                        $proc->nrprocjud,
302
                        true
303
                    );
304
                    $this->dom->addChild(
305
                        $pjt,
306
                        "codSusp",
307
                        $proc->codsusp,
308
                        true
309
                    );
310
                    $procjud->appendChild($pjt);
311
                }
312
                $fpasLotacao->appendChild($procjud);
313
            }
314
            $dados->appendChild($fpasLotacao);
315
316
            if (!empty($da->infoemprparcial)) {
317
                $parcial = $this->dom->createElement("infoEmprParcial");
318
                $this->dom->addChild(
319
                    $parcial,
320
                    "tpInscContrat",
321
                    $da->infoemprparcial->tpinsccontrat,
322
                    true
323
                );
324
                $this->dom->addChild(
325
                    $parcial,
326
                    "nrInscContrat",
327
                    $da->infoemprparcial->nrinsccontrat,
328
                    true
329
                );
330
                $this->dom->addChild(
331
                    $parcial,
332
                    "tpInscProp",
333
                    $da->infoemprparcial->tpinscprop,
334
                    true
335
                );
336
                $this->dom->addChild(
337
                    $parcial,
338
                    "nrInscProp",
339
                    $da->infoemprparcial->nrinscprop,
340
                    true
341
                );
342
                $dados->appendChild($parcial);
343
            }
344
        }
345
346
        if (!empty($this->std->novavalidade)) {
347
            $nova = $this->dom->createElement("novaValidade");
348
            $this->dom->addChild(
349
                $nova,
350
                "iniValid",
351
                $this->std->novavalidade->inivalid,
352
                true
353
            );
354
            $this->dom->addChild(
355
                $nova,
356
                "fimValid",
357
                ! empty($this->std->novavalidade->fimvalid)
358
                    ? $this->std->novavalidade->fimvalid
359
                    : null,
360
                false
361
            );
362
        }
363
364
        $info = $this->dom->createElement("infoLotacao");
365
        //seleção do modo
366
        if ($this->std->modo == 'INC') {
367
            $node = $this->dom->createElement("inclusao");
368
            $node->appendChild($ide);
369
            $node->appendChild($dados);
0 ignored issues
show
Bug introduced by
The variable $dados 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...
370
        } elseif ($this->std->modo == 'ALT') {
371
            $node = $this->dom->createElement("alteracao");
372
            $node->appendChild($ide);
373
            $node->appendChild($dados);
374
            isset($nova) ? $node->appendChild($nova) : null;
375
        } else {
376
            $node = $this->dom->createElement("exclusao");
377
            $node->appendChild($ide);
378
        }
379
380
        $info->appendChild($node);
381
        $this->node->appendChild($info);
382
        $this->eSocial->appendChild($this->node);
383
        //$this->xml = $this->dom->saveXML($this->eSocial);
384
        $this->sign();
0 ignored issues
show
Bug introduced by
It seems like sign() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
385
    }
386
}
387