Completed
Pull Request — master (#127)
by
unknown
04:10
created

EvtRmnRPPS   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 501
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 41
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 501
ccs 0
cts 385
cp 0
rs 8.2769

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D toNode() 0 458 40

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
namespace NFePHP\eSocial\Factories;
4
5
/**
6
 * Class eSocial EvtRmnRPPS Event S-1202 constructor
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHPSocial
10
 * @copyright NFePHP Copyright (c) 2017
11
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
12
 * @license   https://opensource.org/licenses/MIT MIT
13
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
14
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
15
 * @link      http://github.com/nfephp-org/sped-esocial for the canonical source repository
16
 */
17
18
use NFePHP\Common\Certificate;
19
use NFePHP\eSocial\Common\Factory;
20
use NFePHP\eSocial\Common\FactoryId;
21
use NFePHP\eSocial\Common\FactoryInterface;
22
use stdClass;
23
24
class EvtRmnRPPS extends Factory implements FactoryInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    public $sequencial;
30
31
    /**
32
     * @var string
33
     */
34
    protected $evtName = 'evtRmnRPPS';
35
36
    /**
37
     * @var string
38
     */
39
    protected $evtAlias = 'S-1202';
40
41
    /**
42
     * Parameters patterns
43
     *
44
     * @var array
45
     */
46
    protected $parameters = [];
47
48
    /**
49
     * Constructor
50
     *
51
     * @param string $config
52
     * @param stdClass $std
53
     * @param Certificate $certificate
54
     */
55
    public function __construct(
56
        $config,
57
        stdClass $std,
58
        Certificate $certificate
59
    ) {
60
        parent::__construct($config, $std, $certificate);
61
    }
62
63
    /**
64
     * Node constructor
65
     */
66
    protected function toNode()
67
    {
68
        $evtid = FactoryId::build(
69
            $this->tpInsc,
70
            $this->nrInsc,
71
            $this->date,
72
            $this->sequencial
73
        );
74
75
        $evtRmnRPPS = $this->dom->createElement("evtRmnRPPS");
76
77
        $att = $this->dom->createAttribute('Id');
78
79
        $att->value = $evtid;
80
81
        $evtRmnRPPS->appendChild($att);
82
83
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
84
85
        $ideEvento = $this->dom->createElement("ideEvento");
86
        $this->dom->addChild(
87
            $ideEvento,
88
            "indRetif",
89
            $this->std->indretif,
90
            true
91
        );
92
        $this->dom->addChild(
93
            $ideEvento,
94
            "nrRecibo",
95
            !empty($this->std->nrrecibo) ? $this->std->nrrecibo : null,
96
            true
97
        );
98
        $this->dom->addChild(
99
            $ideEvento,
100
            "indApuracao",
101
            $this->std->indapuracao,
102
            true
103
        );
104
        $this->dom->addChild(
105
            $ideEvento,
106
            "perApur",
107
            $this->std->perapur,
108
            true
109
        );
110
        $this->dom->addChild(
111
            $ideEvento,
112
            "tpAmb",
113
            $this->tpAmb,
114
            true
115
        );
116
        $this->dom->addChild(
117
            $ideEvento,
118
            "procEmi",
119
            $this->procEmi,
120
            true
121
        );
122
        $this->dom->addChild(
123
            $ideEvento,
124
            "verProc",
125
            $this->verProc,
126
            true
127
        );
128
129
        $this->node->insertBefore($ideEvento, $ideEmpregador);
130
131
        $ideTrabalhador = $this->dom->createElement("ideTrabalhador");
132
133
        $this->dom->addChild(
134
            $ideTrabalhador,
135
            "cpfTrab",
136
            $this->std->idetrabalhador->cpftrab,
137
            true
138
        );
139
140
        $this->dom->addChild(
141
            $ideTrabalhador,
142
            "nisTrab",
143
            !empty($this->std->idetrabalhador->nistrab) ? $this->std->idetrabalhador->nistrab : null,
144
            false
145
        );
146
147
        $this->dom->addChild(
148
            $ideTrabalhador,
149
            "qtdDepFP",
150
            !empty($this->std->idetrabalhador->qtddepfp) ? $this->std->idetrabalhador->qtddepfp : null,
151
            false
152
        );
153
154
        if (isset($this->std->idetrabalhador->procjudtrab)) {
155
            foreach ($this->std->idetrabalhador->procjudtrab as $proc) {
156
                $procJudTrab = $this->dom->createElement("procJudTrab");
157
158
                $this->dom->addChild(
159
                    $procJudTrab,
160
                    "tpTrib",
161
                    $proc->tptrib,
162
                    true
163
                );
164
165
                $this->dom->addChild(
166
                    $procJudTrab,
167
                    "nrProcJud",
168
                    $proc->nrprocjud,
169
                    true
170
                );
171
172
                $this->dom->addChild(
173
                    $procJudTrab,
174
                    "codSusp",
175
                    $proc->codsusp,
176
                    true
177
                );
178
179
                $ideTrabalhador->appendChild($procJudTrab);
180
            }
181
        }
182
183
        $this->node->appendChild($ideTrabalhador);
184
185
        if (isset($this->std->dmdev)) {
186
            foreach ($this->std->dmdev as $dev) {
187
                $dmDev = $this->dom->createElement("dmDev");
188
189
                $this->dom->addChild(
190
                    $dmDev,
191
                    "ideDmDev",
192
                    $dev->idedmdev,
193
                    true
194
                );
195
196
197
                if (isset($dev->infoperapur)) {
198
                    $infoPerApur = $this->dom->createElement("infoPerApur");
199
200
                    if (isset($dev->infoperapur->ideestab)) {
201
                        foreach ($dev->infoperapur->ideestab as $estab) {
202
                            $ideEstab = $this->dom->createElement("ideEstab");
203
204
                            $this->dom->addChild(
205
                                $ideEstab,
206
                                "tpInsc",
207
                                $estab->tpinsc,
208
                                true
209
                            );
210
211
                            $this->dom->addChild(
212
                                $ideEstab,
213
                                "nrInsc",
214
                                $estab->nrinsc,
215
                                true
216
                            );
217
218
                            if (isset($estab->remunperapur)) {
219
                                foreach ($estab->remunperapur as $remun) {
220
                                    $remunPerApur = $this->dom->createElement("remunPerApur");
221
222
                                    $this->dom->addChild(
223
                                        $remunPerApur,
224
                                        "matricula",
225
                                        !empty($remun->matricula) ? $remun->matricula : null,
226
                                        false
227
                                    );
228
229
                                    $this->dom->addChild(
230
                                        $remunPerApur,
231
                                        "codCateg",
232
                                        $remun->codcateg,
233
                                        true
234
                                    );
235
236
                                    if (isset($remun->itensremun)) {
237
                                        foreach ($remun->itensremun as $itens) {
238
                                            $itensRemun = $this->dom->createElement("itensRemun");
239
240
                                            $this->dom->addChild(
241
                                                $itensRemun,
242
                                                "codRubr",
243
                                                $itens->codrubr,
244
                                                true
245
                                            );
246
247
                                            $this->dom->addChild(
248
                                                $itensRemun,
249
                                                "ideTabRubr",
250
                                                $itens->idetabrubr,
251
                                                true
252
                                            );
253
254
                                            $this->dom->addChild(
255
                                                $itensRemun,
256
                                                "qtdRubr",
257
                                                !empty($itens->qtdrubr) ? $itens->qtdrubr : null,
258
                                                false
259
                                            );
260
261
                                            $this->dom->addChild(
262
                                                $itensRemun,
263
                                                "fatorRubr",
264
                                                !empty($itens->fatorrubr) ? $itens->fatorrubr : null,
265
                                                false
266
                                            );
267
268
                                            $this->dom->addChild(
269
                                                $itensRemun,
270
                                                "vrUnit",
271
                                                !empty($itens->vrunit) ? $itens->vrunit : null,
272
                                                false
273
                                            );
274
275
                                            $this->dom->addChild(
276
                                                $itensRemun,
277
                                                "vrRubr",
278
                                                $itens->vrrubr,
279
                                                true
280
                                            );
281
282
                                            $remunPerApur->appendChild($itensRemun);
283
                                        }
284
                                    }
285
286
                                    if (isset($remun->infosaudecolet)) {
287
                                        $infoSaudeColet = $this->dom->createElement("infoSaudeColet");
288
289
                                        if (isset($remun->infosaudecolet->detoper)) {
290
                                            foreach ($remun->infosaudecolet->detoper as $oper) {
291
                                                $detOper = $this->dom->createElement("detOper");
292
293
                                                $this->dom->addChild(
294
                                                    $detOper,
295
                                                    "cnpjOper",
296
                                                    $oper->cnpjoper,
297
                                                    true
298
                                                );
299
300
                                                $this->dom->addChild(
301
                                                    $detOper,
302
                                                    "regANS",
303
                                                    $oper->regans,
304
                                                    true
305
                                                );
306
307
                                                $this->dom->addChild(
308
                                                    $detOper,
309
                                                    "vrPgTit",
310
                                                    $oper->vrpgtit,
311
                                                    true
312
                                                );
313
314
                                                if (isset($oper->detplano)) {
315
                                                    foreach ($oper->detplano as $plano) {
316
                                                        $detPlano = $this->dom->createElement("detPlano");
317
318
                                                        $this->dom->addChild(
319
                                                            $detPlano,
320
                                                            "tpDep",
321
                                                            $plano->tpdep,
322
                                                            true
323
                                                        );
324
325
                                                        $this->dom->addChild(
326
                                                            $detPlano,
327
                                                            "cpfDep",
328
                                                            !empty($plano->cpfdep) ? $plano->cpfdep : null,
329
                                                            false
330
                                                        );
331
332
                                                        $this->dom->addChild(
333
                                                            $detPlano,
334
                                                            "nmDep",
335
                                                            $plano->nmdep,
336
                                                            true
337
                                                        );
338
339
                                                        $this->dom->addChild(
340
                                                            $detPlano,
341
                                                            "dtNascto",
342
                                                            $plano->dtnascto,
343
                                                            true
344
                                                        );
345
346
                                                        $this->dom->addChild(
347
                                                            $detPlano,
348
                                                            "vlrPgDep",
349
                                                            $plano->vlrpgdep,
350
                                                            true
351
                                                        );
352
353
                                                        $detOper->appendChild($detPlano);
354
                                                    }
355
                                                }
356
357
                                                $infoSaudeColet->appendChild($detOper);
358
                                            }
359
                                        }
360
361
                                        $remunPerApur->appendChild($infoSaudeColet);
362
                                    }
363
364
                                    $ideEstab->appendChild($remunPerApur);
365
                                }
366
                            }
367
368
                            $infoPerApur->appendChild($ideEstab);
369
                        }
370
                    }
371
372
                    $dmDev->appendChild($infoPerApur);
373
                }
374
375
                if (isset($dev->infoperant)) {
376
                    $infoPerAnt = $this->dom->createElement("infoPerAnt");
377
378
                    foreach ($dev->infoperant->ideadc as $adc) {
379
                        $ideADC = $this->dom->createElement("ideADC");
380
381
                        $this->dom->addChild(
382
                            $ideADC,
383
                            "dtLei",
384
                            $adc->dtlei,
385
                            true
386
                        );
387
388
                        $this->dom->addChild(
389
                            $ideADC,
390
                            "nrLei",
391
                            $adc->nrlei,
392
                            true
393
                        );
394
395
                        $this->dom->addChild(
396
                            $ideADC,
397
                            "dtEf",
398
                            !empty($adc->dtef) ? $adc->dtef : null,
399
                            false
400
                        );
401
402
                        if (isset($adc->ideperiodo)) {
403
                            foreach ($adc->ideperiodo as $periodo) {
404
                                $idePeriodo = $this->dom->createElement("idePeriodo");
405
406
                                $this->dom->addChild(
407
                                    $idePeriodo,
408
                                    "perRef",
409
                                    $periodo->perref,
410
                                    true
411
                                );
412
413
                                if (isset($periodo->ideestab)) {
414
                                    foreach ($periodo->ideestab as $estab) {
415
                                        $ideEstab = $this->dom->createElement("ideEstab");
416
417
                                        $this->dom->addChild(
418
                                            $ideEstab,
419
                                            "tpInsc",
420
                                            $estab->tpinsc,
421
                                            true
422
                                        );
423
424
                                        $this->dom->addChild(
425
                                            $ideEstab,
426
                                            "nrInsc",
427
                                            $estab->nrinsc,
428
                                            true
429
                                        );
430
431
                                        if (isset($estab->remunperant)) {
432
                                            foreach ($estab->remunperant as $perant) {
433
                                                $remunPerAnt = $this->dom->createElement("remunPerAnt");
434
435
                                                $this->dom->addChild(
436
                                                    $remunPerAnt,
437
                                                    "matricula",
438
                                                    !empty($perant->matricula) ? $perant->matricula : null,
439
                                                    false
440
                                                );
441
442
                                                $this->dom->addChild(
443
                                                    $remunPerAnt,
444
                                                    "codCateg",
445
                                                    $perant->codcateg,
446
                                                    true
447
                                                );
448
449
                                                if (isset($perant->itensremun)) {
450
                                                    foreach ($perant->itensremun as $itens) {
451
                                                        $itensRemun = $this->dom->createElement("itensRemun");
452
453
                                                        $this->dom->addChild(
454
                                                            $itensRemun,
455
                                                            "codRubr",
456
                                                            $itens->codrubr,
457
                                                            true
458
                                                        );
459
460
                                                        $this->dom->addChild(
461
                                                            $itensRemun,
462
                                                            "ideTabRubr",
463
                                                            $itens->idetabrubr,
464
                                                            true
465
                                                        );
466
467
                                                        $this->dom->addChild(
468
                                                            $itensRemun,
469
                                                            "qtdRubr",
470
                                                            !empty($itens->qtdrubr) ? $itens->qtdrubr : null,
471
                                                            false
472
                                                        );
473
474
                                                        $this->dom->addChild(
475
                                                            $itensRemun,
476
                                                            "fatorRubr",
477
                                                            !empty($itens->fatorrubr) ? $itens->fatorrubr : null,
478
                                                            false
479
                                                        );
480
481
                                                        $this->dom->addChild(
482
                                                            $itensRemun,
483
                                                            "vrUnit",
484
                                                            !empty($itens->vrunit) ? $itens->vrunit : null,
485
                                                            false
486
                                                        );
487
488
                                                        $this->dom->addChild(
489
                                                            $itensRemun,
490
                                                            "vrRubr",
491
                                                            $itens->vrrubr,
492
                                                            true
493
                                                        );
494
495
                                                        $remunPerAnt->appendChild($itensRemun);
496
                                                    }
497
                                                }
498
499
                                                $ideEstab->appendChild($remunPerAnt);
500
                                            }
501
                                        }
502
503
                                        $idePeriodo->appendChild($ideEstab);
504
                                    }
505
                                }
506
507
                                $ideADC->appendChild($idePeriodo);
508
                            }
509
                        }
510
511
                        $infoPerAnt->appendChild($ideADC);
512
                    }
513
514
                    $dmDev->appendChild($infoPerAnt);
515
                }
516
            }
517
518
            $this->node->appendChild($dmDev);
0 ignored issues
show
Bug introduced by
The variable $dmDev 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...
519
        }
520
521
        $this->eSocial->appendChild($this->node);
522
        $this->sign();
523
    }
524
}
525