Passed
Push — master ( feaf84...03ed34 )
by Roberto
06:49 queued 18s
created

EvtTabCarreira::toNode()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 113
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 0
cts 108
cp 0
rs 4.8196
c 0
b 0
f 0
cc 9
eloc 90
nc 16
nop 0
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 EvtTabCarreira Event S-1035 constructor
7
 * READ for 2.4.2 layout
8
 *
9
 * @category  library
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 EvtTabCarreira extends Factory implements FactoryInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    public $sequencial;
30
    /**
31
     * @var string
32
     */
33
    protected $evtName = 'evtTabCarreira';
34
    /**
35
     * @var string
36
     */
37
    protected $evtAlias = 'S-1035';
38
39
    /**
40
     * Constructor
41
     * @param string $config
42
     * @param stdClass $std
43
     * @param Certificate $certificate
44
     */
45
    public function __construct(
46
        $config,
47
        stdClass $std,
48
        Certificate $certificate
49
    ) {
50
        parent::__construct($config, $std, $certificate);
51
    }
52
53
    /**
54
     * Node constructor
55
     */
56
    protected function toNode()
57
    {
58
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
59
        //o idEvento pode variar de evento para evento
60
        //então cada factory individualmente terá de construir o seu
61
        $ideEvento = $this->dom->createElement("ideEvento");
62
        $this->dom->addChild(
63
            $ideEvento,
64
            "tpAmb",
65
            $this->tpAmb,
66
            true
67
        );
68
        $this->dom->addChild(
69
            $ideEvento,
70
            "procEmi",
71
            $this->procEmi,
72
            true
73
        );
74
        $this->dom->addChild(
75
            $ideEvento,
76
            "verProc",
77
            $this->verProc,
78
            true
79
        );
80
        $this->node->insertBefore($ideEvento, $ideEmpregador);
81
        
82
        $ide = $this->dom->createElement("ideCarreira");
83
        $this->dom->addChild(
84
            $ide,
85
            "codCarreira",
86
            $this->std->codcarreira,
87
            true
88
        );
89
        $this->dom->addChild(
90
            $ide,
91
            "iniValid",
92
            $this->std->inivalid,
93
            true
94
        );
95
        $this->dom->addChild(
96
            $ide,
97
            "fimValid",
98
            !empty($this->std->fimvalid) ? $this->std->fimvalid : null,
99
            false
100
        );
101
        if (!empty($this->std->dadoscarreira)) {
102
            $da = $this->std->dadoscarreira;
103
            $dados = $this->dom->createElement("dadosCarreira");
104
            $this->dom->addChild(
105
                $dados,
106
                "dscCarreira",
107
                $da->dsccarreira,
108
                true
109
            );
110
            $this->dom->addChild(
111
                $dados,
112
                "leiCarr",
113
                !empty($da->leicarr) ? $da->leicarr : null,
114
                false
115
            );
116
            $this->dom->addChild(
117
                $dados,
118
                "dtLeiCarr",
119
                $da->dtleicarr,
120
                true
121
            );
122
            $this->dom->addChild(
123
                $dados,
124
                "sitCarr",
125
                $da->sitcarr,
126
                true
127
            );
128
        }
129
        if (!empty($this->std->novavalidade)) {
130
            $nova = $this->dom->createElement("novaValidade");
131
            $this->dom->addChild(
132
                $nova,
133
                "iniValid",
134
                $this->std->novavalidade->inivalid,
135
                true
136
            );
137
            $this->dom->addChild(
138
                $nova,
139
                "fimValid",
140
                ! empty($this->std->novavalidade->fimvalid)
141
                    ? $this->std->novavalidade->fimvalid
142
                    : null,
143
                false
144
            );
145
        }
146
        $info = $this->dom->createElement("infoCarreira");
147
        //seleção do modo
148
        if ($this->std->modo == 'INC') {
149
            $node = $this->dom->createElement("inclusao");
150
            $node->appendChild($ide);
151
            $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...
152
        } elseif ($this->std->modo == 'ALT') {
153
            $node = $this->dom->createElement("alteracao");
154
            $node->appendChild($ide);
155
            $node->appendChild($dados);
156
            if (isset($nova)) {
157
                $node->appendChild($nova);
158
            }
159
        } else {
160
            $node = $this->dom->createElement("exclusao");
161
            $node->appendChild($ide);
162
        }
163
        $info->appendChild($node);
164
        $this->node->appendChild($info);
165
        $this->eSocial->appendChild($this->node);
166
        //$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...
167
        $this->sign();
168
    }
169
}
170