Passed
Push — master ( 60293e...5490e2 )
by Roberto
03:17
created

EvtCAT   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 492
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 48
lcom 1
cbo 1
dl 0
loc 492
ccs 0
cts 451
cp 0
rs 8.5599
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
F toNode() 0 211 18
A tagRegistrador() 0 23 3
A tagTrabalhador() 0 17 2
A tagVinculo() 0 29 4
B tagCAT() 0 80 6
C tagLocalAcidente() 0 89 14

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
namespace NFePHP\eSocial\Factories;
4
5
/**
6
 * Class eSocial EvtCAT Event S-2210 constructor
7
 * Read for 2.4.2 layout
8
 * Read for 2.5.0 layout
9
 *
10
 * @category  library
11
 * @package   NFePHP\eSocial
12
 * @copyright NFePHP Copyright (c) 2017
13
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
14
 * @license   https://opensource.org/licenses/MIT MIT
15
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
16
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
17
 * @link      http://github.com/nfephp-org/sped-esocial for the canonical source repository
18
 */
19
20
use NFePHP\Common\Certificate;
21
use NFePHP\eSocial\Common\Factory;
22
use NFePHP\eSocial\Common\FactoryId;
23
use NFePHP\eSocial\Common\FactoryInterface;
24
use stdClass;
25
26
class EvtCAT extends Factory implements FactoryInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    protected $evtName = 'evtCAT';
32
    /**
33
     * @var string
34
     */
35
    protected $evtAlias = 'S-2210';
36
    /**
37
     * Parameters patterns
38
     *
39
     * @var array
40
     */
41
    protected $parameters = [];
42
43
    /**
44
     * Constructor
45
     *
46
     * @param string $config
47
     * @param stdClass $std
48
     * @param Certificate $certificate | null
49
     * @param string $date
50
     */
51
    public function __construct(
52
        $config,
53
        stdClass $std,
54
        Certificate $certificate = null,
55
        $date = ''
56
    ) {
57
        parent::__construct($config, $std, $certificate, $date);
58
    }
59
60
    /**
61
     * Node constructor
62
     */
63
    protected function toNode()
64
    {
65
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
66
        $ideEvento = $this->dom->createElement("ideEvento");
67
        $this->dom->addChild(
68
            $ideEvento,
69
            "indRetif",
70
            $this->std->indretif,
71
            true
72
        );
73
        $this->dom->addChild(
74
            $ideEvento,
75
            "nrRecibo",
76
            ! empty($this->std->nrrecibo) ? $this->std->nrrecibo : null,
77
            false
78
        );
79
        $this->dom->addChild(
80
            $ideEvento,
81
            "tpAmb",
82
            $this->tpAmb,
83
            true
84
        );
85
        $this->dom->addChild(
86
            $ideEvento,
87
            "procEmi",
88
            $this->procEmi,
89
            true
90
        );
91
        $this->dom->addChild(
92
            $ideEvento,
93
            "verProc",
94
            $this->verProc,
95
            true
96
        );
97
        $this->node->insertBefore($ideEvento, $ideEmpregador);
98
        
99
        if ($this->layoutStr !== 'v02_05_00') {
100
            $this->tagRegistrador($ideEmpregador);
0 ignored issues
show
Compatibility introduced by
$ideEmpregador of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
101
            $this->tagTrabalhador();
102
        } else {
103
            $this->tagVinculo();
104
        }
105
        $cat = $this->tagCAT();
106
        $this->tagLocalAcidente($cat);
107
        
108
        
109
        
110
        foreach ($this->std->parteatingida as $pa) {
111
            $parteAtingida = $this->dom->createElement("parteAtingida");
112
            $this->dom->addChild(
113
                $parteAtingida,
114
                "codParteAting",
115
                $pa->codparteating,
116
                true
117
            );
118
            $this->dom->addChild(
119
                $parteAtingida,
120
                "lateralidade",
121
                $pa->lateralidade,
122
                true
123
            );
124
            $cat->appendChild($parteAtingida);
125
        }
126
127
        foreach ($this->std->agentecausador as $pa) {
128
            $agenteCausador = $this->dom->createElement("agenteCausador");
129
            $this->dom->addChild(
130
                $agenteCausador,
131
                "codAgntCausador",
132
                $pa->codagntcausador,
133
                true
134
            );
135
            $cat->appendChild($agenteCausador);
136
        }
137
        if (!empty($this->std->atestado)) {
138
            $pa = $this->std->atestado;
139
            $atestado = $this->dom->createElement("atestado");
140
            $this->dom->addChild(
141
                $atestado,
142
                "codCNES",
143
                !empty($pa->codcnes) ? $pa->codcnes : null,
144
                false
145
            );
146
            $this->dom->addChild(
147
                $atestado,
148
                "dtAtendimento",
149
                $pa->dtatendimento,
150
                true
151
            );
152
            $this->dom->addChild(
153
                $atestado,
154
                "hrAtendimento",
155
                $pa->hratendimento,
156
                true
157
            );
158
            $this->dom->addChild(
159
                $atestado,
160
                "indInternacao",
161
                $pa->indinternacao,
162
                true
163
            );
164
            $this->dom->addChild(
165
                $atestado,
166
                "durTrat",
167
                $pa->durtrat,
168
                true
169
            );
170
            $this->dom->addChild(
171
                $atestado,
172
                "indAfast",
173
                $pa->indafast,
174
                true
175
            );
176
            $this->dom->addChild(
177
                $atestado,
178
                "dscLesao",
179
                !empty($pa->dsclesao) ? $pa->dsclesao : null,
180
                false
181
            );
182
            $this->dom->addChild(
183
                $atestado,
184
                "dscCompLesao",
185
                !empty($pa->dsccomplesao) ? $pa->dsccomplesao : null,
186
                false
187
            );
188
            $this->dom->addChild(
189
                $atestado,
190
                "diagProvavel",
191
                !empty($pa->diagprovavel) ? $pa->diagprovavel : null,
192
                false
193
            );
194
            $this->dom->addChild(
195
                $atestado,
196
                "codCID",
197
                $pa->codcid,
198
                true
199
            );
200
            $this->dom->addChild(
201
                $atestado,
202
                "observacao",
203
                !empty($pa->observacao) ? $pa->observacao : null,
204
                false
205
            );
206
            $emitente = $this->dom->createElement("emitente");
207
            $this->dom->addChild(
208
                $emitente,
209
                "nmEmit",
210
                $pa->nmemit,
211
                true
212
            );
213
            $this->dom->addChild(
214
                $emitente,
215
                "ideOC",
216
                $pa->ideoc,
217
                true
218
            );
219
            if ($this->layoutStr == 'v02_05_00') {
220
                $this->dom->addChild(
221
                    $emitente,
222
                    "nrOC",
223
                    $pa->nroc,
224
                    true
225
                );
226
            } else {
227
                $this->dom->addChild(
228
                    $emitente,
229
                    "nrOc",
230
                    $pa->nroc,
231
                    true
232
                );
233
            }
234
            $this->dom->addChild(
235
                $emitente,
236
                "ufOC",
237
                !empty($pa->ufoc) ? $pa->ufoc : null,
238
                true
239
            );
240
            $atestado->appendChild($emitente);
241
            $cat->appendChild($atestado);
242
        }
243
        if (!empty($this->std->catorigem)) {
244
            $pa = $this->std->catorigem;
245
            $catOrigem = $this->dom->createElement("catOrigem");
246
            $this->dom->addChild(
247
                $catOrigem,
248
                "dtCatOrig",
249
                !empty($pa->dtcatorig) ? $pa->dtcatorig : null,
250
                false
251
            );
252
            if ($this->layoutStr != 'v02_05_00') {
253
                $this->dom->addChild(
254
                    $catOrigem,
255
                    "nrCatOrig",
256
                    !empty($pa->nrcatorig) ? $pa->nrcatorig : null,
257
                    false
258
                );
259
            } else {
260
                $this->dom->addChild(
261
                    $catOrigem,
262
                    "nrRecCatOrig",
263
                    !empty($pa->nrreccatorig) ? $pa->nrreccatorig : null,
264
                    false
265
                );
266
            }
267
            $cat->appendChild($catOrigem);
268
        }
269
        $this->node->appendChild($cat);
270
        $this->eSocial->appendChild($this->node);
271
        //$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...
272
        $this->sign();
273
    }
274
    
275
    protected function tagRegistrador(\DOMElement $ideEmpregador)
276
    {
277
        $ideRegistrador = $this->dom->createElement("ideRegistrador");
278
        $this->dom->addChild(
279
            $ideRegistrador,
280
            "tpRegistrador",
281
            $this->std->tpregistrador,
282
            true
283
        );
284
        $this->dom->addChild(
285
            $ideRegistrador,
286
            "tpInsc",
287
            !empty($this->std->tpinsc) ? $this->std->tpinsc : null,
288
            false
289
        );
290
        $this->dom->addChild(
291
            $ideRegistrador,
292
            "nrInsc",
293
            !empty($this->std->nrinsc) ? $this->std->nrinsc : null,
294
            false
295
        );
296
        $this->node->insertBefore($ideRegistrador, $ideEmpregador);
297
    }
298
    
299
    protected function tagTrabalhador()
300
    {
301
        $ideTrabalhador = $this->dom->createElement("ideTrabalhador");
302
        $this->dom->addChild(
303
            $ideTrabalhador,
304
            "cpfTrab",
305
            $this->std->cpftrab,
306
            true
307
        );
308
        $this->dom->addChild(
309
            $ideTrabalhador,
310
            "nisTrab",
311
            !empty($this->std->nistrab) ? $this->std->nistrab : null,
312
            false
313
        );
314
        $this->node->appendChild($ideTrabalhador);
315
    }
316
    
317
    protected function tagVinculo()
318
    {
319
        $ideVinculo = $this->dom->createElement("ideVinculo");
320
        $this->dom->addChild(
321
            $ideVinculo,
322
            "cpfTrab",
323
            $this->std->cpftrab,
324
            true
325
        );
326
        $this->dom->addChild(
327
            $ideVinculo,
328
            "nisTrab",
329
            !empty($this->std->nistrab) ? $this->std->nistrab : null,
330
            false
331
        );
332
        $this->dom->addChild(
333
            $ideVinculo,
334
            "matricula",
335
            !empty($this->std->matricula) ? $this->std->matricula : null,
336
            false
337
        );
338
        $this->dom->addChild(
339
            $ideVinculo,
340
            "codCateg",
341
            !empty($this->std->codcateg) ? $this->std->codcateg : null,
342
            false
343
        );
344
        $this->node->appendChild($ideVinculo);
345
    }
346
    
347
    protected function tagCAT()
348
    {
349
        $cat = $this->dom->createElement("cat");
350
        $this->dom->addChild(
351
            $cat,
352
            "dtAcid",
353
            $this->std->dtacid,
354
            true
355
        );
356
        $this->dom->addChild(
357
            $cat,
358
            "tpAcid",
359
            $this->std->tpacid,
360
            true
361
        );
362
        $this->dom->addChild(
363
            $cat,
364
            "hrAcid",
365
            $this->std->hracid,
366
            true
367
        );
368
        $this->dom->addChild(
369
            $cat,
370
            "hrsTrabAntesAcid",
371
            $this->std->hrstrabantesacid,
372
            true
373
        );
374
        $this->dom->addChild(
375
            $cat,
376
            "tpCat",
377
            $this->std->tpcat,
378
            true
379
        );
380
        $this->dom->addChild(
381
            $cat,
382
            "indCatObito",
383
            $this->std->indcatobito,
384
            true
385
        );
386
        $this->dom->addChild(
387
            $cat,
388
            "dtObito",
389
            !empty($this->std->dtobito) ? $this->std->dtobito : null,
390
            false
391
        );
392
        $this->dom->addChild(
393
            $cat,
394
            "indComunPolicia",
395
            $this->std->indcomunpolicia,
396
            true
397
        );
398
        $this->dom->addChild(
399
            $cat,
400
            "codSitGeradora",
401
            !empty($this->std->codsitgeradora) ? $this->std->codsitgeradora : null,
402
            false
403
        );
404
        $this->dom->addChild(
405
            $cat,
406
            "iniciatCAT",
407
            $this->std->iniciatcat,
408
            true
409
        );
410
        if ($this->layoutStr !== 'v02_04_02') {
411
            $this->dom->addChild(
412
                $cat,
413
                "obsCAT",
414
                !empty($this->std->obscat) ? $this->std->obscat : null,
415
                false
416
            );
417
        } else {
418
            $this->dom->addChild(
419
                $cat,
420
                "observacao",
421
                !empty($this->std->observacao) ? $this->std->observacao : null,
422
                false
423
            );
424
        }
425
        return $cat;
426
    }
427
    
428
    protected function tagLocalAcidente(\DOMElement &$cat)
429
    {
430
        $localAcidente = $this->dom->createElement("localAcidente");
431
        $this->dom->addChild(
432
            $localAcidente,
433
            "tpLocal",
434
            $this->std->tplocal,
435
            true
436
        );
437
        $this->dom->addChild(
438
            $localAcidente,
439
            "dscLocal",
440
            !empty($this->std->dsclocal) ? $this->std->dsclocal : null,
441
            false
442
        );
443
        $this->dom->addChild(
444
            $localAcidente,
445
            "codAmb",
446
            !empty($this->std->codamb) ? $this->std->codamb : null,
447
            false
448
        );
449
        $this->dom->addChild(
450
            $localAcidente,
451
            "tpLograd",
452
            !empty($this->std->tplograd) ? $this->std->tplograd : null,
453
            false
454
        );
455
        $this->dom->addChild(
456
            $localAcidente,
457
            "dscLograd",
458
            !empty($this->std->dsclograd) ? $this->std->dsclograd : null,
459
            false
460
        );
461
        $this->dom->addChild(
462
            $localAcidente,
463
            "nrLograd",
464
            !empty($this->std->nrlograd) ? $this->std->nrlograd : null,
465
            false
466
        );
467
        $this->dom->addChild(
468
            $localAcidente,
469
            "complemento",
470
            !empty($this->std->complemento) ? $this->std->complemento : null,
471
            false
472
        );
473
        $this->dom->addChild(
474
            $localAcidente,
475
            "bairro",
476
            !empty($this->std->bairro) ? $this->std->bairro : null,
477
            false
478
        );
479
        $this->dom->addChild(
480
            $localAcidente,
481
            "cep",
482
            !empty($this->std->cep) ? $this->std->cep : null,
483
            false
484
        );
485
        $this->dom->addChild(
486
            $localAcidente,
487
            "codMunic",
488
            !empty($this->std->codmunic) ? $this->std->codmunic : null,
489
            false
490
        );
491
        $this->dom->addChild(
492
            $localAcidente,
493
            "uf",
494
            !empty($this->std->uf) ? $this->std->uf : null,
495
            false
496
        );
497
        $this->dom->addChild(
498
            $localAcidente,
499
            "cnpjLocalAcid",
500
            !empty($this->std->cnpjlocalacid) ? $this->std->cnpjlocalacid : null,
501
            false
502
        );
503
        $this->dom->addChild(
504
            $localAcidente,
505
            "pais",
506
            !empty($this->std->pais) ? $this->std->pais : null,
507
            false
508
        );
509
        $this->dom->addChild(
510
            $localAcidente,
511
            "codPostal",
512
            !empty($this->std->codpostal) ? $this->std->codpostal : null,
513
            false
514
        );
515
        $cat->appendChild($localAcidente);
516
    }
517
}
518