Passed
Push — master ( b72b80...2898a5 )
by Roberto
03:33
created

EvtRmnRPPS::ideperiodo()   C

Complexity

Conditions 9
Paths 5

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 0
cts 85
cp 0
rs 6.7717
c 0
b 0
f 0
cc 9
nc 5
nop 2
crap 90

How to fix   Long Method   

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;
4
5
/**
6
 * Class eSocial EvtRmnRPPS Event S-1202 constructor
7
 * Read for 2.4.2 layout
8
 *
9
 * @category  library
10
 * @package   NFePHP\eSocial
11
 * @copyright NFePHP Copyright (c) 2017
12
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
13
 * @license   https://opensource.org/licenses/MIT MIT
14
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
15
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
16
 * @link      http://github.com/nfephp-org/sped-esocial for the canonical source repository
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
    /**
28
     * @var int
29
     */
30
    public $sequencial;
31
32
    /**
33
     * @var string
34
     */
35
    protected $evtName = 'evtRmnRPPS';
36
37
    /**
38
     * @var string
39
     */
40
    protected $evtAlias = 'S-1202';
41
42
    /**
43
     * Parameters patterns
44
     *
45
     * @var array
46
     */
47
    protected $parameters = [];
48
49
    /**
50
     * Constructor
51
     *
52
     * @param string $config
53
     * @param stdClass $std
54
     * @param Certificate $certificate
55
     */
56
    public function __construct($config, stdClass $std, Certificate $certificate)
57
    {
58
        parent::__construct($config, $std, $certificate);
59
    }
60
61
    /**
62
     * Node constructor
63
     */
64
    protected function toNode()
65
    {
66
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
67
        //o idEvento pode variar de evento para evento
68
        //então cada factory individualmente terá de construir o seu
69
        $ideEvento = $this->dom->createElement("ideEvento");
70
        $this->dom->addChild(
71
            $ideEvento,
72
            "indRetif",
73
            $this->std->indretif,
74
            true
75
        );
76
        $this->dom->addChild(
77
            $ideEvento,
78
            "nrRecibo",
79
            !empty($this->std->nrrecibo) ? $this->std->nrrecibo : null,
80
            false
81
        );
82
        $this->dom->addChild(
83
            $ideEvento,
84
            "indApuracao",
85
            $this->std->indapuracao,
86
            true
87
        );
88
        $this->dom->addChild(
89
            $ideEvento,
90
            "perApur",
91
            $this->std->perapur,
92
            true
93
        );
94
        $this->dom->addChild(
95
            $ideEvento,
96
            "tpAmb",
97
            $this->tpAmb,
98
            true
99
        );
100
        $this->dom->addChild(
101
            $ideEvento,
102
            "procEmi",
103
            $this->procEmi,
104
            true
105
        );
106
        $this->dom->addChild(
107
            $ideEvento,
108
            "verProc",
109
            $this->verProc,
110
            true
111
        );
112
        $this->node->insertBefore($ideEvento, $ideEmpregador);
113
        
114
        $ideTrabalhador = $this->dom->createElement("ideTrabalhador");
115
        $this->dom->addChild(
116
            $ideTrabalhador,
117
            "cpfTrab",
118
            $this->std->idetrabalhador->cpftrab,
119
            true
120
        );
121
        $this->dom->addChild(
122
            $ideTrabalhador,
123
            "nisTrab",
124
            !empty($this->std->idetrabalhador->nistrab) ? $this->std->idetrabalhador->nistrab : null,
125
            false
126
        );
127
        $this->dom->addChild(
128
            $ideTrabalhador,
129
            "qtdDepFP",
130
            !empty($this->std->idetrabalhador->qtddepfp) ? $this->std->idetrabalhador->qtddepfp : null,
131
            false
132
        );
133
        //tag procJud
134
        $this->procJud($ideTrabalhador, $this->std);
135
        $this->node->appendChild($ideTrabalhador);
136
137
        $dmDev = null;
138
        if (isset($this->std->dmdev)) {
139
            foreach ($this->std->dmdev as $dev) {
140
                $dmDev = $this->dom->createElement("dmDev");
141
                $this->dom->addChild(
142
                    $dmDev,
143
                    "ideDmDev",
144
                    $dev->idedmdev,
145
                    true
146
                );
147
                //add infoPerApur to dmDev
148
                $this->infoperapur($dmDev, $dev);
149
                //add infoperant to dmDev
150
                $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\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...
151
            }
152
        }
153
        if (!empty($dmDev)) {
154
            $this->node->appendChild($dmDev);
155
        }
156
        //finalização do xml
157
        $this->eSocial->appendChild($this->node);
158
        //$this->xml = $this->dom->saveXML($this->eSocial);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
        $this->sign();
160
    }
161
162
    /**
163
     * Add ProcJud to ideTrabalhador
164
     * @param \DOMElement $node
165
     * @param \stdClass $std
166
     * @return none
167
     */
168
    private function procJud(&$node, $std)
169
    {
170
        if (!isset($std->idetrabalhador->procjudtrab)) {
171
            return;
172
        }
173
        foreach ($std->idetrabalhador->procjudtrab as $proc) {
174
            $procJudTrab = $this->dom->createElement("procJudTrab");
175
            $this->dom->addChild(
176
                $procJudTrab,
177
                "tpTrib",
178
                $proc->tptrib,
179
                true
180
            );
181
            $this->dom->addChild(
182
                $procJudTrab,
183
                "nrProcJud",
184
                $proc->nrprocjud,
185
                true
186
            );
187
            $this->dom->addChild(
188
                $procJudTrab,
189
                "codSusp",
190
                !empty($proc->codsusp) ? $proc->codsusp : null,
191
                false
192
            );
193
            $node->appendChild($procJudTrab);
194
        }
195
    }
196
197
    /**
198
     * Add infoPerApur to dmDev
199
     * @param \DOMElement $node
200
     * @param \stdClass $std
201
     * @return none
202
     */
203
    private function infoperapur(&$node, $std)
204
    {
205
        if (!isset($std->infoperapur)) {
206
            return;
207
        }    
208
        $infoPerApur = $this->dom->createElement("infoPerApur");
209
        $this->ideestab($infoPerApur, $std);
210
        $node->appendChild($infoPerApur);
211
    }
212
    
213
    /**
214
     * Add idestab to infoperapur
215
     * @param \DOMElementtype $node
216
     * @param \stdClass $std
217
     */
218
    private function ideestab(&$node, $std)
219
    {
220
        foreach ($std->infoperapur->ideestab as $estab) {
221
            $ideEstab = $this->dom->createElement("ideEstab");
222
            $this->dom->addChild(
223
                $ideEstab,
224
                "tpInsc",
225
                $estab->tpinsc,
226
                true
227
            );
228
            $this->dom->addChild(
229
                $ideEstab,
230
                "nrInsc",
231
                $estab->nrinsc,
232
                true
233
            );
234
            //add remunperapur to ideEstab
235
            $this->remunperapur($ideEstab, $estab);
236
            $node->appendChild($ideEstab);
237
        }
238
    }
239
240
    /**
241
     * Add remunperapur to ideEstab
242
     * @param \DOMElement $node
243
     * @param \stdClass $std
244
     */
245
    private function remunperapur(&$node, $std)
246
    {
247
        foreach ($std->remunperapur as $remun) {
248
            $remunPerApur = $this->dom->createElement("remunPerApur");
249
            $this->dom->addChild(
250
                $remunPerApur,
251
                "matricula",
252
                !empty($remun->matricula) ? $remun->matricula : null,
253
                false
254
            );
255
            $this->dom->addChild(
256
                $remunPerApur,
257
                "codCateg",
258
                $remun->codcateg,
259
                true
260
            );
261
            //add itensremun to remunPerApur
262
            $this->itensremun($remunPerApur, $remun);
263
            //add infosaudecolet to remunPerApur
264
            $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\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...
265
            $node->appendChild($remunPerApur);
266
        }
267
    }
268
    
269
    /**
270
     * Add itensremun to remunPerApur
271
     * @param \DOMElement $node
272
     * @param \stdClass $std
273
     */
274
    private function itensremun(&$node, $std)
275
    {
276
        foreach ($std->itensremun as $itens) {
277
            $itensRemun = $this->dom->createElement("itensRemun");
278
            $this->dom->addChild(
279
                $itensRemun,
280
                "codRubr",
281
                $itens->codrubr,
282
                true
283
            );
284
            $this->dom->addChild(
285
                $itensRemun,
286
                "ideTabRubr",
287
                $itens->idetabrubr,
288
                true
289
            );
290
            $this->dom->addChild(
291
                $itensRemun,
292
                "qtdRubr",
293
                !empty($itens->qtdrubr) ? $itens->qtdrubr : null,
294
                false
295
            );
296
            $this->dom->addChild(
297
                $itensRemun,
298
                "fatorRubr",
299
                !empty($itens->fatorrubr) ? $itens->fatorrubr : null,
300
                false
301
            );
302
            $this->dom->addChild(
303
                $itensRemun,
304
                "vrUnit",
305
                !empty($itens->vrunit) ? $itens->vrunit : null,
306
                false
307
            );
308
            $this->dom->addChild(
309
                $itensRemun,
310
                "vrRubr",
311
                $itens->vrrubr,
312
                true
313
            );
314
            $node->appendChild($itensRemun);
315
        }
316
    }
317
318
    /**
319
     * Add infosaudecolet to remunPerApur
320
     * @param type $node
321
     * @param type $std
322
     * @return type
323
     */
324
    private function infosaudecolet(&$node, $std)
325
    {
326
        if (!isset($std->infosaudecolet)) {
327
            return;
328
        }
329
        $infoSaudeColet = $this->dom->createElement("infoSaudeColet");
330
        foreach ($std->infosaudecolet->detoper as $oper) {
331
            $detOper = $this->dom->createElement("detOper");
332
            $this->dom->addChild(
333
                $detOper,
334
                "cnpjOper",
335
                $oper->cnpjoper,
336
                true
337
            );
338
            $this->dom->addChild(
339
                $detOper,
340
                "regANS",
341
                $oper->regans,
342
                true
343
            );
344
            $this->dom->addChild(
345
                $detOper,
346
                "vrPgTit",
347
                $oper->vrpgtit,
348
                true
349
            );
350
            if (isset($oper->detplano)) {
351
                foreach ($oper->detplano as $plano) {
352
                    $detPlano = $this->dom->createElement("detPlano");
353
                    $this->dom->addChild(
354
                        $detPlano,
355
                        "tpDep",
356
                        $plano->tpdep,
357
                        true
358
                    );
359
                    $this->dom->addChild(
360
                        $detPlano,
361
                        "cpfDep",
362
                        !empty($plano->cpfdep) ? $plano->cpfdep : null,
363
                        false
364
                    );
365
                    $this->dom->addChild(
366
                        $detPlano,
367
                        "nmDep",
368
                        $plano->nmdep,
369
                        true
370
                    );
371
                    $this->dom->addChild(
372
                        $detPlano,
373
                        "dtNascto",
374
                        $plano->dtnascto,
375
                        true
376
                    );
377
                    $this->dom->addChild(
378
                        $detPlano,
379
                        "vlrPgDep",
380
                        $plano->vlrpgdep,
381
                        true
382
                    );
383
                    $detOper->appendChild($detPlano);
384
                }
385
            }
386
            $infoSaudeColet->appendChild($detOper);
387
        }
388
        $node->appendChild($infoSaudeColet);
389
    }
390
391
    /**
392
     * Add infoperant to dmDev
393
     * @param type $node
394
     * @param type $std
395
     * @return type
396
     */
397
    private function infoperant(&$node, $std)
398
    {
399
        if (!isset($std->infoperant)) {
400
            return;
401
        }    
402
        $infoPerAnt = $this->dom->createElement("infoPerAnt");
403
        //add ideadc to infoPerAnt
404
        $this->ideadc($infoPerAnt, $std);
405
        $node->appendChild($infoPerAnt);
406
    }
407
    
408
    /**
409
     * Add ideadc to infoPerAnt
410
     * @param \DOMElement $node
411
     * @param \stdClass $std
412
     */
413
    private function ideadc(&$node, $std)
414
    {
415
        foreach ($std->infoperant->ideadc as $adc) {
416
            $ideADC = $this->dom->createElement("ideADC");
417
            $this->dom->addChild(
418
                    $ideADC, "dtLei", $adc->dtlei, true
419
            );
420
            $this->dom->addChild(
421
                    $ideADC, "nrLei", $adc->nrlei, true
422
            );
423
            $this->dom->addChild(
424
                    $ideADC, "dtEf", !empty($adc->dtef) ? $adc->dtef : null, false
425
            );
426
            //add ideperiodo to ideADC
427
            $this->ideperiodo($ideADC, $adc);
428
            $node->appendChild($ideADC);
429
        }
430
    }
431
    
432
    /**
433
     * Add ideperiodo to ideADC
434
     * @param \DOMElement $node
435
     * @param \stdClass $std
436
     */
437
    private function ideperiodo(&$node, $std)
438
    {
439
        foreach ($std->ideperiodo as $periodo) {
440
            $idePeriodo = $this->dom->createElement("idePeriodo");
441
            $this->dom->addChild(
442
                $idePeriodo,
443
                "perRef",
444
                $periodo->perref,
445
                true
446
            );
447
            foreach ($periodo->ideestab as $estab) {
448
                $ideEstab = $this->dom->createElement("ideEstab");
449
                $this->dom->addChild(
450
                    $ideEstab,
451
                    "tpInsc",
452
                    $estab->tpinsc,
453
                    true
454
                );
455
                $this->dom->addChild(
456
                    $ideEstab,
457
                    "nrInsc",
458
                    $estab->nrinsc,
459
                    true
460
                );
461
                foreach ($estab->remunperant as $perant) {
462
                    $remunPerAnt = $this->dom->createElement("remunPerAnt");
463
                    $this->dom->addChild(
464
                        $remunPerAnt,
465
                        "matricula",
466
                        !empty($perant->matricula) ? $perant->matricula : null,
467
                        false
468
                    );
469
                    $this->dom->addChild(
470
                        $remunPerAnt,
471
                        "codCateg",
472
                        $perant->codcateg,
473
                        true
474
                    );
475
                    foreach ($perant->itensremun as $itens) {
476
                        $itensRemun = $this->dom->createElement("itensRemun");
477
                        $this->dom->addChild(
478
                            $itensRemun,
479
                            "codRubr",
480
                            $itens->codrubr,
481
                            true
482
                        );
483
                        $this->dom->addChild(
484
                            $itensRemun,
485
                            "ideTabRubr",
486
                            $itens->idetabrubr,
487
                            true
488
                        );
489
                        $this->dom->addChild(
490
                            $itensRemun,
491
                            "qtdRubr",
492
                            !empty($itens->qtdrubr) ? $itens->qtdrubr : null,
493
                            false
494
                        );
495
                        $this->dom->addChild(
496
                            $itensRemun,
497
                            "fatorRubr",
498
                            !empty($itens->fatorrubr) ? $itens->fatorrubr : null,
499
                            false
500
                        );
501
                        $this->dom->addChild(
502
                            $itensRemun,
503
                            "vrUnit",
504
                            !empty($itens->vrunit) ? $itens->vrunit : null,
505
                            false
506
                        );
507
                        $this->dom->addChild(
508
                            $itensRemun,
509
                            "vrRubr",
510
                            $itens->vrrubr,
511
                            true
512
                        );
513
                        $remunPerAnt->appendChild($itensRemun);
514
                    }
515
                    $ideEstab->appendChild($remunPerAnt);
516
                }
517
                $idePeriodo->appendChild($ideEstab);
518
            }    
519
            $node->appendChild($idePeriodo);
520
        }    
521
    }
522
}
523