Test Failed
Push — master ( 88ea0f...5ac8de )
by Roberto
03:16 queued 14s
created

TraitS1202   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 482
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 0
dl 0
loc 482
rs 8.8798
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A toNodeS100() 0 4 1
B toNode250() 0 97 7
A procJud() 0 28 4
A infoperapur() 0 9 2
A ideestab() 0 21 2
A remunperapur() 0 23 3
B itensremun() 0 43 5
B infosaudecolet() 0 66 6
A infoperant() 0 10 2
A ideadc() 0 27 3
C ideperiodo() 0 85 9

How to fix   Complexity   

Complex Class

Complex classes like TraitS1202 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 TraitS1202, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace NFePHP\eSocial\Factories\Traits;
4
5
trait TraitS1202
6
{
7
    
8
    /**
9
     * builder for version S.1.0.0
10
     */
11
    protected function toNodeS100()
12
    {
13
        throw new \Exception("TODO !!");
14
    }
15
    
16
    /**
17
     * builder for version 2.5.0
18
     */
19
    protected function toNode250()
20
    {
21
        $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...
22
        //o idEvento pode variar de evento para evento
23
        //então cada factory individualmente terá de construir o seu
24
        $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...
25
        $this->dom->addChild(
26
            $ideEvento,
27
            "indRetif",
28
            $this->std->indretif,
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...
29
            true
30
        );
31
        $this->dom->addChild(
32
            $ideEvento,
33
            "nrRecibo",
34
            !empty($this->std->nrrecibo) ? $this->std->nrrecibo : null,
35
            false
36
        );
37
        $this->dom->addChild(
38
            $ideEvento,
39
            "indApuracao",
40
            $this->std->indapuracao,
41
            true
42
        );
43
        $this->dom->addChild(
44
            $ideEvento,
45
            "perApur",
46
            $this->std->perapur,
47
            true
48
        );
49
        $this->dom->addChild(
50
            $ideEvento,
51
            "tpAmb",
52
            $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...
53
            true
54
        );
55
        $this->dom->addChild(
56
            $ideEvento,
57
            "procEmi",
58
            $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...
59
            true
60
        );
61
        $this->dom->addChild(
62
            $ideEvento,
63
            "verProc",
64
            $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...
65
            true
66
        );
67
        $this->node->insertBefore($ideEvento, $ideEmpregador);
68
69
        $ideTrabalhador = $this->dom->createElement("ideTrabalhador");
70
        $this->dom->addChild(
71
            $ideTrabalhador,
72
            "cpfTrab",
73
            $this->std->idetrabalhador->cpftrab,
74
            true
75
        );
76
        $this->dom->addChild(
77
            $ideTrabalhador,
78
            "nisTrab",
79
            !empty($this->std->idetrabalhador->nistrab) ? $this->std->idetrabalhador->nistrab : null,
80
            false
81
        );
82
        $this->dom->addChild(
83
            $ideTrabalhador,
84
            "qtdDepFP",
85
            !empty($this->std->idetrabalhador->qtddepfp) ? $this->std->idetrabalhador->qtddepfp : null,
86
            false
87
        );
88
        //tag procJud
89
        $this->procJud($ideTrabalhador, $this->std);
90
        $this->node->appendChild($ideTrabalhador);
91
92
        $dmDev = null;
93
        if (isset($this->std->dmdev)) {
94
            foreach ($this->std->dmdev as $dev) {
95
                $dmDev = $this->dom->createElement("dmDev");
96
                $this->dom->addChild(
97
                    $dmDev,
98
                    "ideDmDev",
99
                    $dev->idedmdev,
100
                    true
101
                );
102
                //add infoPerApur to dmDev
103
                $this->infoperapur($dmDev, $dev);
104
                //add infoperant to dmDev
105
                $this->infoperant($dmDev, $dev);
0 ignored issues
show
Documentation introduced by
$dmDev is of type object<DOMElement>, but the function expects a object<NFePHP\eSocial\Factories\Traits\type>.

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...
106
            }
107
        }
108
        if (!empty($dmDev)) {
109
            $this->node->appendChild($dmDev);
110
        }
111
        //finalização do xml
112
        $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...
113
        //$this->xml = $this->dom->saveXML($this->eSocial);
114
        $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...
115
    }
116
117
    /**
118
     * Add ProcJud to ideTrabalhador
119
     * @param \DOMElement $node
120
     * @param \stdClass $std
121
     * @return none
122
     */
123
    private function procJud(&$node, $std)
124
    {
125
        if (!isset($std->idetrabalhador->procjudtrab)) {
126
            return;
127
        }
128
        foreach ($std->idetrabalhador->procjudtrab as $proc) {
129
            $procJudTrab = $this->dom->createElement("procJudTrab");
130
            $this->dom->addChild(
131
                $procJudTrab,
132
                "tpTrib",
133
                $proc->tptrib,
134
                true
135
            );
136
            $this->dom->addChild(
137
                $procJudTrab,
138
                "nrProcJud",
139
                $proc->nrprocjud,
140
                true
141
            );
142
            $this->dom->addChild(
143
                $procJudTrab,
144
                "codSusp",
145
                !empty($proc->codsusp) ? $proc->codsusp : null,
146
                false
147
            );
148
            $node->appendChild($procJudTrab);
149
        }
150
    }
151
152
    /**
153
     * Add infoPerApur to dmDev
154
     * @param \DOMElement $node
155
     * @param \stdClass $std
156
     * @return none
157
     */
158
    private function infoperapur(&$node, $std)
159
    {
160
        if (!isset($std->infoperapur)) {
161
            return;
162
        }
163
        $infoPerApur = $this->dom->createElement("infoPerApur");
164
        $this->ideestab($infoPerApur, $std);
165
        $node->appendChild($infoPerApur);
166
    }
167
168
    /**
169
     * Add idestab to infoperapur
170
     * @param \DOMElementtype $node
171
     * @param \stdClass $std
172
     */
173
    private function ideestab(&$node, $std)
174
    {
175
        foreach ($std->infoperapur->ideestab as $estab) {
176
            $ideEstab = $this->dom->createElement("ideEstab");
177
            $this->dom->addChild(
178
                $ideEstab,
179
                "tpInsc",
180
                $estab->tpinsc,
181
                true
182
            );
183
            $this->dom->addChild(
184
                $ideEstab,
185
                "nrInsc",
186
                $estab->nrinsc,
187
                true
188
            );
189
            //add remunperapur to ideEstab
190
            $this->remunperapur($ideEstab, $estab);
191
            $node->appendChild($ideEstab);
192
        }
193
    }
194
195
    /**
196
     * Add remunperapur to ideEstab
197
     * @param \DOMElement $node
198
     * @param \stdClass $std
199
     */
200
    private function remunperapur(&$node, $std)
201
    {
202
        foreach ($std->remunperapur as $remun) {
203
            $remunPerApur = $this->dom->createElement("remunPerApur");
204
            $this->dom->addChild(
205
                $remunPerApur,
206
                "matricula",
207
                !empty($remun->matricula) ? $remun->matricula : null,
208
                false
209
            );
210
            $this->dom->addChild(
211
                $remunPerApur,
212
                "codCateg",
213
                $remun->codcateg,
214
                true
215
            );
216
            //add itensremun to remunPerApur
217
            $this->itensremun($remunPerApur, $remun);
218
            //add infosaudecolet to remunPerApur
219
            $this->infosaudecolet($remunPerApur, $remun);
0 ignored issues
show
Documentation introduced by
$remunPerApur is of type object<DOMElement>, but the function expects a object<NFePHP\eSocial\Factories\Traits\type>.

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...
220
            $node->appendChild($remunPerApur);
221
        }
222
    }
223
224
    /**
225
     * Add itensremun to remunPerApur
226
     * @param \DOMElement $node
227
     * @param \stdClass $std
228
     */
229
    private function itensremun(&$node, $std)
230
    {
231
        foreach ($std->itensremun as $itens) {
232
            $itensRemun = $this->dom->createElement("itensRemun");
233
            $this->dom->addChild(
234
                $itensRemun,
235
                "codRubr",
236
                $itens->codrubr,
237
                true
238
            );
239
            $this->dom->addChild(
240
                $itensRemun,
241
                "ideTabRubr",
242
                $itens->idetabrubr,
243
                true
244
            );
245
            $this->dom->addChild(
246
                $itensRemun,
247
                "qtdRubr",
248
                !empty($itens->qtdrubr) ? $itens->qtdrubr : null,
249
                false
250
            );
251
            $this->dom->addChild(
252
                $itensRemun,
253
                "fatorRubr",
254
                !empty($itens->fatorrubr) ? $itens->fatorrubr : null,
255
                false
256
            );
257
            $this->dom->addChild(
258
                $itensRemun,
259
                "vrUnit",
260
                !empty($itens->vrunit) ? $itens->vrunit : null,
261
                false
262
            );
263
            $this->dom->addChild(
264
                $itensRemun,
265
                "vrRubr",
266
                $itens->vrrubr,
267
                true
268
            );
269
            $node->appendChild($itensRemun);
270
        }
271
    }
272
273
    /**
274
     * Add infosaudecolet to remunPerApur
275
     * @param type $node
276
     * @param type $std
277
     * @return type
278
     */
279
    private function infosaudecolet(&$node, $std)
280
    {
281
        if (!isset($std->infosaudecolet)) {
282
            return;
283
        }
284
        $infoSaudeColet = $this->dom->createElement("infoSaudeColet");
285
        foreach ($std->infosaudecolet->detoper as $oper) {
286
            $detOper = $this->dom->createElement("detOper");
287
            $this->dom->addChild(
288
                $detOper,
289
                "cnpjOper",
290
                $oper->cnpjoper,
291
                true
292
            );
293
            $this->dom->addChild(
294
                $detOper,
295
                "regANS",
296
                $oper->regans,
297
                true
298
            );
299
            $this->dom->addChild(
300
                $detOper,
301
                "vrPgTit",
302
                $oper->vrpgtit,
303
                true
304
            );
305
            if (isset($oper->detplano)) {
306
                foreach ($oper->detplano as $plano) {
307
                    $detPlano = $this->dom->createElement("detPlano");
308
                    $this->dom->addChild(
309
                        $detPlano,
310
                        "tpDep",
311
                        $plano->tpdep,
312
                        true
313
                    );
314
                    $this->dom->addChild(
315
                        $detPlano,
316
                        "cpfDep",
317
                        !empty($plano->cpfdep) ? $plano->cpfdep : null,
318
                        false
319
                    );
320
                    $this->dom->addChild(
321
                        $detPlano,
322
                        "nmDep",
323
                        $plano->nmdep,
324
                        true
325
                    );
326
                    $this->dom->addChild(
327
                        $detPlano,
328
                        "dtNascto",
329
                        $plano->dtnascto,
330
                        true
331
                    );
332
                    $this->dom->addChild(
333
                        $detPlano,
334
                        "vlrPgDep",
335
                        $plano->vlrpgdep,
336
                        true
337
                    );
338
                    $detOper->appendChild($detPlano);
339
                }
340
            }
341
            $infoSaudeColet->appendChild($detOper);
342
        }
343
        $node->appendChild($infoSaudeColet);
344
    }
345
346
    /**
347
     * Add infoperant to dmDev
348
     * @param type $node
349
     * @param type $std
350
     * @return type
351
     */
352
    private function infoperant(&$node, $std)
353
    {
354
        if (!isset($std->infoperant)) {
355
            return;
356
        }
357
        $infoPerAnt = $this->dom->createElement("infoPerAnt");
358
        //add ideadc to infoPerAnt
359
        $this->ideadc($infoPerAnt, $std);
360
        $node->appendChild($infoPerAnt);
361
    }
362
363
    /**
364
     * Add ideadc to infoPerAnt
365
     * @param \DOMElement $node
366
     * @param \stdClass $std
367
     */
368
    private function ideadc(&$node, $std)
369
    {
370
        foreach ($std->infoperant->ideadc as $adc) {
371
            $ideADC = $this->dom->createElement("ideADC");
372
            $this->dom->addChild(
373
                $ideADC,
374
                "dtLei",
375
                $adc->dtlei,
376
                true
377
            );
378
            $this->dom->addChild(
379
                $ideADC,
380
                "nrLei",
381
                $adc->nrlei,
382
                true
383
            );
384
            $this->dom->addChild(
385
                $ideADC,
386
                "dtEf",
387
                !empty($adc->dtef) ? $adc->dtef : null,
388
                false
389
            );
390
            //add ideperiodo to ideADC
391
            $this->ideperiodo($ideADC, $adc);
392
            $node->appendChild($ideADC);
393
        }
394
    }
395
396
    /**
397
     * Add ideperiodo to ideADC
398
     * @param \DOMElement $node
399
     * @param \stdClass $std
400
     */
401
    private function ideperiodo(&$node, $std)
402
    {
403
        foreach ($std->ideperiodo as $periodo) {
404
            $idePeriodo = $this->dom->createElement("idePeriodo");
405
            $this->dom->addChild(
406
                $idePeriodo,
407
                "perRef",
408
                $periodo->perref,
409
                true
410
            );
411
            foreach ($periodo->ideestab as $estab) {
412
                $ideEstab = $this->dom->createElement("ideEstab");
413
                $this->dom->addChild(
414
                    $ideEstab,
415
                    "tpInsc",
416
                    $estab->tpinsc,
417
                    true
418
                );
419
                $this->dom->addChild(
420
                    $ideEstab,
421
                    "nrInsc",
422
                    $estab->nrinsc,
423
                    true
424
                );
425
                foreach ($estab->remunperant as $perant) {
426
                    $remunPerAnt = $this->dom->createElement("remunPerAnt");
427
                    $this->dom->addChild(
428
                        $remunPerAnt,
429
                        "matricula",
430
                        !empty($perant->matricula) ? $perant->matricula : null,
431
                        false
432
                    );
433
                    $this->dom->addChild(
434
                        $remunPerAnt,
435
                        "codCateg",
436
                        $perant->codcateg,
437
                        true
438
                    );
439
                    foreach ($perant->itensremun as $itens) {
440
                        $itensRemun = $this->dom->createElement("itensRemun");
441
                        $this->dom->addChild(
442
                            $itensRemun,
443
                            "codRubr",
444
                            $itens->codrubr,
445
                            true
446
                        );
447
                        $this->dom->addChild(
448
                            $itensRemun,
449
                            "ideTabRubr",
450
                            $itens->idetabrubr,
451
                            true
452
                        );
453
                        $this->dom->addChild(
454
                            $itensRemun,
455
                            "qtdRubr",
456
                            !empty($itens->qtdrubr) ? $itens->qtdrubr : null,
457
                            false
458
                        );
459
                        $this->dom->addChild(
460
                            $itensRemun,
461
                            "fatorRubr",
462
                            !empty($itens->fatorrubr) ? $itens->fatorrubr : null,
463
                            false
464
                        );
465
                        $this->dom->addChild(
466
                            $itensRemun,
467
                            "vrUnit",
468
                            !empty($itens->vrunit) ? $itens->vrunit : null,
469
                            false
470
                        );
471
                        $this->dom->addChild(
472
                            $itensRemun,
473
                            "vrRubr",
474
                            $itens->vrrubr,
475
                            true
476
                        );
477
                        $remunPerAnt->appendChild($itensRemun);
478
                    }
479
                    $ideEstab->appendChild($remunPerAnt);
480
                }
481
                $idePeriodo->appendChild($ideEstab);
482
            }
483
            $node->appendChild($idePeriodo);
484
        }
485
    }
486
}
487