Passed
Push — master ( 2a646e...2d320e )
by Roberto
02:43
created

EvtTabLotacao::toNode()   D

Complexity

Conditions 13
Paths 30

Size

Total Lines 186
Code Lines 145

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
dl 0
loc 186
ccs 0
cts 174
cp 0
rs 4.9922
c 0
b 0
f 0
cc 13
eloc 145
nc 30
nop 0
crap 182

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;
4
5
/**
6
 * Class eSocial EvtTabLotacao Event S-1020 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 EvtTabLotacao extends Factory implements FactoryInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    public $sequencial;
30
31
    /**
32
     * @var string
33
     */
34
    protected $evtName = 'evtTabLotacao';
35
36
    /**
37
     * @var string
38
     */
39
    protected $evtAlias = 'S-1020';
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
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
69
        //o idEvento pode variar de evento para evento
70
        //então cada factory individualmente terá de construir o seu
71
        $ideEvento = $this->dom->createElement("ideEvento");
72
        $this->dom->addChild(
73
            $ideEvento,
74
            "tpAmb",
75
            $this->tpAmb,
76
            true
77
        );
78
        $this->dom->addChild(
79
            $ideEvento,
80
            "procEmi",
81
            $this->procEmi,
82
            true
83
        );
84
        $this->dom->addChild(
85
            $ideEvento,
86
            "verProc",
87
            $this->verProc,
88
            true
89
        );
90
        $this->node->insertBefore($ideEvento, $ideEmpregador);
91
        
92
        $ide = $this->dom->createElement("ideLotacao");
93
        $this->dom->addChild(
94
            $ide,
95
            "codLotacao",
96
            $this->std->codlotacao,
97
            true
98
        );
99
        $this->dom->addChild(
100
            $ide,
101
            "iniValid",
102
            $this->std->inivalid,
103
            true
104
        );
105
        $this->dom->addChild(
106
            $ide,
107
            "fimValid",
108
            ! empty($this->std->fimvalid) ? $this->std->fimvalid : null,
109
            false
110
        );
111
        
112
        
113
        if (!empty($this->std->dadoslotacao)) {
114
            $da = $this->std->dadoslotacao;
115
            $dados = $this->dom->createElement("dadosLotacao");
116
            $this->dom->addChild(
117
                $dados,
118
                "tpLotacao",
119
                $da->tplotacao,
120
                true
121
            );
122
            $this->dom->addChild(
123
                $dados,
124
                "tpInsc",
125
                !empty($da->tpinsc) ? $da->tpinsc : null,
126
                false
127
            );
128
            $this->dom->addChild(
129
                $dados,
130
                "nrInsc",
131
                !empty($da->nrinsc) ? $da->nrinsc : null,
132
                false
133
            );
134
            $fpasLotacao = $this->dom->createElement("fpasLotacao");
135
            $this->dom->addChild(
136
                $fpasLotacao,
137
                "fpas",
138
                $da->fpas,
139
                true
140
            );
141
            $this->dom->addChild(
142
                $fpasLotacao,
143
                "codTercs",
144
                $da->codtercs,
145
                true
146
            );
147
            $this->dom->addChild(
148
                $fpasLotacao,
149
                "codTercsSusp",
150
                !empty($da->codtercssusp) ? $da->codtercssusp : null,
151
                false
152
            );
153
            
154
            if (!empty($da->procjudterceiro)) {
155
                $procjud = $this->dom->createElement("infoProcJudTerceiros");
156
                foreach ($da->procjudterceiro as $proc) {
157
                    $pjt = $this->dom->createElement("procJudTerceiro");
158
                    $this->dom->addChild(
159
                        $pjt,
160
                        "codTerc",
161
                        $proc->codterc,
162
                        true
163
                    );
164
                    $this->dom->addChild(
165
                        $pjt,
166
                        "nrProcJud",
167
                        $proc->nrprocjud,
168
                        true
169
                    );
170
                    $this->dom->addChild(
171
                        $pjt,
172
                        "codSusp",
173
                        $proc->codsusp,
174
                        true
175
                    );
176
                    $procjud->appendChild($pjt);
177
                }
178
                $fpasLotacao->appendChild($procjud);
179
            }
180
            $dados->appendChild($fpasLotacao);
181
            
182
            if (!empty($da->infoemprparcial)) {
183
                $parcial = $this->dom->createElement("infoEmprParcial");
184
                $this->dom->addChild(
185
                    $parcial,
186
                    "tpInscContrat",
187
                    $da->infoemprparcial->tpinsccontrat,
188
                    true
189
                );
190
                $this->dom->addChild(
191
                    $parcial,
192
                    "nrInscContrat",
193
                    $da->infoemprparcial->nrinsccontrat,
194
                    true
195
                );
196
                $this->dom->addChild(
197
                    $parcial,
198
                    "tpInscProp",
199
                    $da->infoemprparcial->tpinscprop,
200
                    true
201
                );
202
                $this->dom->addChild(
203
                    $parcial,
204
                    "nrInscProp",
205
                    $da->infoemprparcial->nrinscprop,
206
                    true
207
                );
208
                $dados->appendChild($parcial);
209
            }
210
        }
211
        
212
        if (!empty($this->std->novavalidade)) {
213
            $nova = $this->dom->createElement("novaValidade");
214
            $this->dom->addChild(
215
                $nova,
216
                "iniValid",
217
                $this->std->novavalidade->inivalid,
218
                true
219
            );
220
            $this->dom->addChild(
221
                $nova,
222
                "fimValid",
223
                ! empty($this->std->novavalidade->fimvalid)
224
                    ? $this->std->novavalidade->fimvalid
225
                    : null,
226
                false
227
            );
228
        }
229
        
230
        $info = $this->dom->createElement("infoLotacao");
231
        //seleção do modo
232
        if ($this->std->modo == 'INC') {
233
            $node = $this->dom->createElement("inclusao");
234
            $node->appendChild($ide);
235
            $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...
236
        } elseif ($this->std->modo == 'ALT') {
237
            $node = $this->dom->createElement("alteracao");
238
            $node->appendChild($ide);
239
            $node->appendChild($dados);
240
            $node->appendChild($nova);
0 ignored issues
show
Bug introduced by
The variable $nova 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...
241
        } else {
242
            $node = $this->dom->createElement("exclusao");
243
            $node->appendChild($ide);
244
        }
245
        
246
        $info->appendChild($node);
247
        $this->node->appendChild($info);
248
        $this->eSocial->appendChild($this->node);
249
        //$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...
250
        $this->sign();
251
    }
252
}
253