Completed
Push — master ( 02ff7b...a9c9d6 )
by Roberto
06:15
created

Dacce::pBuildDACCE()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 56
Code Lines 36

Duplication

Lines 56
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 36
c 1
b 0
f 0
nc 4
nop 0
dl 56
loc 56
ccs 0
cts 41
cp 0
crap 20
rs 9.0544

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\DA\CTe;
4
5
/**
6
 * Esta classe gera a carta de correção em PDF
7
 * NOTA: Esse documento NÃO É NORMALIZADO, nem requerido pela SEFAZ
8
 *
9
 * @category  Library
10
 * @package   nfephp-org/sped-da
11
 * @name      Dacce.php
12
 * @copyright 2009-2016 NFePHP
13
 * @license   http://www.gnu.org/licenses/lgpl.html GNU/LGPL v.3
14
 * @link      http://github.com/nfephp-org/sped-da for the canonical source repository
15
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
16
 */
17
18
use Exception;
19
use NFePHP\Common\Dom\Dom;
20
use NFePHP\DA\Legacy\Pdf;
21
use NFePHP\DA\Legacy\Common;
22
23
class Dacce extends Common
24
{
25
    protected $logoAlign = 'C';
26
    protected $yDados = 0;
27
    protected $debugMode = 0;
28
    protected $aEnd = array();
29
    protected $pdf;
30
    protected $xml;
31
    protected $logomarca = '';
32
    protected $errMsg = '';
33
    protected $errStatus = false;
34
    protected $orientacao = 'P';
35
    protected $papel = 'A4';
36
    protected $destino = 'I';
37
    protected $pdfDir = '';
38
    protected $fontePadrao = 'Times';
39
    protected $version = '0.1.1';
40
    protected $wPrint;
41
    protected $hPrint;
42
    protected $wCanhoto;
43
    protected $formatoChave = "#### #### #### #### #### #### #### #### #### #### ####";
44
    protected $id;
45
    protected $chCTe;
46
    protected $tpAmb;
47
    protected $cOrgao;
48
    protected $infCorrecao;
49
    protected $xCondUso;
50
    protected $dhEvento;
51
    protected $cStat;
52
    protected $xMotivo;
53
    protected $dhRegEvento;
54
    protected $nProt;
55
    private $dom;
56
    private $procEventoCTe;
57
    private $eventoCTe;
58
    private $infEvento;
59
    private $retEventoCTe;
60
    private $retInfEvento;
61
62
    /**
63
     * __construct
64
     *
65
     * @param string $docXML      Arquivo XML (diretório ou string)
66
     * @param string $sOrientacao (Opcional) Orientação da impressão P-retrato L-Paisagem
67
     * @param string $sPapel      Tamanho do papel (Ex. A4)
68
     * @param string $sPathLogo   Caminho para o arquivo do logo
69
     * @param string $sDestino    Destino do PDF I-browser D-download S-string F-salva
70
     * @param array  $aEnd        array com o endereço do emitente
71
     * @param string $sDirPDF     Caminho para o diretorio de armazenamento dos arquivos PDF
72
     * @param string $fonteDANFE  Nome da fonte alternativa do DAnfe
0 ignored issues
show
Bug introduced by
There is no parameter named $fonteDANFE. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     * @param number $mododebug   0-Não 1-Sim e 2-nada (2 default)
74
     */
75 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
        $docXML = '',
77
        $sOrientacao = '',
78
        $sPapel = '',
79
        $sPathLogo = '',
80
        $sDestino = 'I',
81
        $aEnd = '',
82
        $sDirPDF = '',
83
        $fontePDF = '',
84
        $mododebug = 0
85
    ) {
86
        if (is_numeric($mododebug)) {
87
            $this->debugMode = (int) $mododebug;
88
        }
89
        if ($this->debugMode === 1) {
90
            // ativar modo debug
91
            error_reporting(E_ALL);
92
            ini_set('display_errors', 'On');
93
        } elseif ($this->debugMode === 0) {
94
            // desativar modo debug
95
            error_reporting(0);
96
            ini_set('display_errors', 'Off');
97
        }
98
        if (is_array($aEnd)) {
99
            $this->aEnd = $aEnd;
100
        }
101
        $this->orientacao = $sOrientacao;
102
        $this->papel = $sPapel;
103
        $this->pdf = '';
104
        $this->xml = $docXML;
105
        $this->logomarca = $sPathLogo;
106
        $this->destino = $sDestino;
107
        $this->pdfDir = $sDirPDF;
108
        // verifica se foi passa a fonte a ser usada
109
        if (empty($fontePDF)) {
110
            $this->fontePadrao = 'Times';
111
        } else {
112
            $this->fontePadrao = $fontePDF;
113
        }
114
        // se for passado o xml
115
        if (!empty($this->xml)) {
116
            if (is_file($this->xml)) {
117
                $this->xml = file_get_contents($this->xml);
118
            }
119
            $this->dom = new Dom();
120
            $this->dom->loadXML($this->xml);
121
            $this->procEventoCTe = $this->dom->getElementsByTagName("procEventoCTe")->item(0);
122
            $this->eventoCTe = $this->procEventoCTe->getElementsByTagName("eventoCTe")->item(0);
123
            $this->retEventoCTe = $this->procEventoCTe->getElementsByTagName("retEventoCTe")->item(0);
124
            $this->infEvento = $this->eventoCTe->getElementsByTagName("infEvento")->item(0);
125
            $this->retInfEvento = $this->retEventoCTe->getElementsByTagName("infEvento")->item(0);
126
            $tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
127
            if ($tpEvento != '110110') {
128
                $this->errMsg = 'Um evento de CC-e deve ser passado.';
129
                $this->errStatus = true;
130
                throw new Exception($this->errMsg);
131
            }
132
            $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
133
            $this->chCTe = $this->infEvento->getElementsByTagName("chCTe")->item(0)->nodeValue;
134
            $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
135
            $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
136
            $this->infCorrecao = $this->infEvento->getElementsByTagName("infCorrecao");
137
            $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0)->nodeValue;
138
            $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
139
            $this->cStat = $this->retInfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
140
            $this->xMotivo = $this->retInfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
141
            $this->CNPJDest = !empty($this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue)
0 ignored issues
show
Bug introduced by
The property CNPJDest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
142
                ? $this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue
143
                : '';
144
            $this->CPFDest = !empty($this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue)
0 ignored issues
show
Bug introduced by
The property CPFDest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
145
                ? $this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue
146
                : '';
147
            $this->dhRegEvento = $this->retInfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
148
            $this->nProt = $this->retInfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
149
        }
150
    }
151
152
    /**
153
     * monta
154
     *
155
     * @param string $orientacao
156
     * @param string $papel
157
     * @param string $logoAlign
158
     */
159 View Code Duplication
    public function monta($orientacao = 'P', $papel = 'A4', $logoAlign = 'C')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $this->orientacao = $orientacao;
162
        $this->papel = $papel;
163
        $this->logoAlign = $logoAlign;
164
        $this->pBuildDACCE();
165
    }
166
167
    /**
168
     * pBuildDACCE
169
     */
170 View Code Duplication
    private function pBuildDACCE()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
173
        if ($this->orientacao == 'P') {
174
            // margens do PDF
175
            $margSup = 2;
176
            $margEsq = 2;
177
            $margDir = 2;
178
            // posição inicial do relatorio
179
            $xInic = 1;
180
            $yInic = 1;
181
            if ($this->papel == 'A4') { // A4 210x297mm
182
                $maxW = 210;
183
                $maxH = 297;
184
            }
185
        } else {
186
            // margens do PDF
187
            $margSup = 3;
188
            $margEsq = 3;
189
            $margDir = 3;
190
            // posição inicial do relatorio
191
            $xInic = 5;
192
            $yInic = 5;
193
            if ($this->papel == 'A4') { // A4 210x297mm
194
                $maxH = 210;
195
                $maxW = 297;
196
            }
197
        } // orientação
198
199
        // largura imprimivel em mm
200
        $this->wPrint = $maxW - ($margEsq + $xInic);
0 ignored issues
show
Bug introduced by
The variable $maxW 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...
201
        // comprimento imprimivel em mm
202
        $this->hPrint = $maxH - ($margSup + $yInic);
0 ignored issues
show
Bug introduced by
The variable $maxH 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...
203
        // estabelece contagem de paginas
204
        $this->pdf->aliasNbPages();
205
        // fixa as margens
206
        $this->pdf->setMargins($margEsq, $margSup, $margDir);
207
        $this->pdf->setDrawColor(0, 0, 0);
208
        $this->pdf->setFillColor(255, 255, 255);
209
        // inicia o documento
210
        $this->pdf->open();
211
        // adiciona a primeira página
212
        $this->pdf->addPage($this->orientacao, $this->papel);
213
        $this->pdf->setLineWidth(0.1);
214
        $this->pdf->setTextColor(0, 0, 0);
215
        // montagem da página
216
        $pag = 1;
217
        $x = $xInic;
218
        $y = $yInic;
219
        // coloca o cabeçalho
220
        $y = $this->pHeader($x, $y, $pag);
221
        // coloca os dados da CCe
222
        $y = $this->pBody($x, $y + 15);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $y is correct as $this->pBody($x, $y + 15) (which targets NFePHP\DA\CTe\Dacce::pBody()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
223
        // coloca o rodapé
224
        $y = $this->pFooter($x, $y + $this->hPrint - 20);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $y is correct as $this->pFooter($x, $y + $this->hPrint - 20) (which targets NFePHP\DA\CTe\Dacce::pFooter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$y is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
    }
226
227
    /**
228
     * pHeader
229
     *
230
     * @param  number $x
231
     * @param  number $y
232
     * @param  number $pag
233
     * @return number
234
     */
235
    private function pHeader($x, $y, $pag)
0 ignored issues
show
Unused Code introduced by
The parameter $pag is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
236
    {
237
        $oldX = $x;
238
        $oldY = $y;
239
        $maxW = $this->wPrint;
240
        // ####################################################################################
241
        // coluna esquerda identificação do emitente
242
        $w = round($maxW * 0.41, 0); // 80;
243
        if ($this->orientacao == 'P') {
244
            $aFont = array(
245
                'font' => $this->fontePadrao,
246
                'size' => 6,
247
                'style' => 'I'
248
            );
249
        } else {
250
            $aFont = array(
251
                'font' => $this->fontePadrao,
252
                'size' => 8,
253
                'style' => 'B'
254
            );
255
        }
256
        $w1 = $w;
0 ignored issues
show
Unused Code introduced by
$w1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
257
        $h = 32;
258
        $oldY += $h;
259
        $this->pTextBox($x, $y, $w, $h);
260
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
261
        $this->pTextBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
262 View Code Duplication
        if (is_file($this->logomarca)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
263
            $logoInfo = getimagesize($this->logomarca);
264
            // largura da imagem em mm
265
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
266
            // altura da imagem em mm
267
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
268
            if ($this->logoAlign == 'L') {
269
                $nImgW = round($w / 3, 0);
270
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
271
                $xImg = $x + 1;
272
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
273
                // estabelecer posições do texto
274
                $x1 = round($xImg + $nImgW + 1, 0);
275
                $y1 = round($h / 3 + $y, 0);
276
                $tw = round(2 * $w / 3, 0);
277
            }
278
            if ($this->logoAlign == 'C') {
279
                $nImgH = round($h / 3, 0);
280
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
281
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
282
                $yImg = $y + 3;
283
                $x1 = $x;
284
                $y1 = round($yImg + $nImgH + 1, 0);
285
                $tw = $w;
286
            }
287
            if ($this->logoAlign == 'R') {
288
                $nImgW = round($w / 3, 0);
289
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
290
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
291
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
292
                $x1 = $x;
293
                $y1 = round($h / 3 + $y, 0);
294
                $tw = round(2 * $w / 3, 0);
295
            }
296
            $this->pdf->Image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH);
0 ignored issues
show
Bug introduced by
The variable $xImg 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...
Bug introduced by
The variable $yImg 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...
Bug introduced by
The variable $nImgW 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...
Bug introduced by
The variable $nImgH 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...
297
        } else {
298
            $x1 = $x;
299
            $y1 = round($h / 3 + $y, 0);
300
            $tw = $w;
301
        }
302
        // Nome emitente
303
        $aFont = ['font' => $this->fontePadrao,'size' => 12,'style' => 'B'];
304
        $texto = $this->aEnd['razao'];
305
        $this->pTextBox($x1, $y1, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
0 ignored issues
show
Bug introduced by
The variable $x1 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...
Bug introduced by
The variable $y1 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...
Bug introduced by
The variable $tw 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...
306
        // endereço
307
        $y1 = $y1 + 6;
308
        $aFont = ['font' => $this->fontePadrao,'size' => 8,'style' => ''];
309
        $lgr = $this->aEnd['logradouro'];
310
        $nro = $this->aEnd['numero'];
311
        $cpl = $this->aEnd['complemento'];
312
        $bairro = $this->aEnd['bairro'];
313
        $CEP = $this->aEnd['CEP'];
314
        $CEP = $this->pFormat($CEP, "#####-###");
315
        $mun = $this->aEnd['municipio'];
316
        $UF = $this->aEnd['UF'];
317
        $fone = $this->aEnd['telefone'];
318
        $email = $this->aEnd['email'];
319
        $foneLen = strlen($fone);
320 View Code Duplication
        if ($foneLen > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
321
            $fone2 = substr($fone, 0, $foneLen - 4);
322
            $fone1 = substr($fone, 0, $foneLen - 8);
323
            $fone = '(' . $fone1 . ') ' . substr($fone2, - 4) . '-' . substr($fone, - 4);
324
        } else {
325
            $fone = '';
326
        }
327
        if ($email != '') {
328
            $email = 'Email: ' . $email;
329
        }
330
        $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - " . $CEP . "\n"
331
            . $mun . " - " . $UF . " " . $fone . "\n" . $email;
332
        $this->pTextBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
333
        // ##################################################
334
        $w2 = round($maxW - $w, 0);
335
        $x += $w;
336
        $this->pTextBox($x, $y, $w2, $h);
337
        $y1 = $y + $h;
338
        $aFont = ['font' => $this->fontePadrao,'size' => 16,'style' => 'B'];
339
        $this->pTextBox($x, $y + 2, $w2, 8, 'Representação Gráfica de CC-e', $aFont, 'T', 'C', 0, '');
340
        $aFont = array(
341
            'font' => $this->fontePadrao,
342
            'size' => 12,
343
            'style' => 'I'
344
        );
345
        $this->pTextBox($x, $y + 7, $w2, 8, '(Carta de Correção Eletrônica)', $aFont, 'T', 'C', 0, '');
346
        $texto = 'ID do Evento: ' . $this->id;
347
        $aFont = ['font' => $this->fontePadrao,'size' => 10,'style' => ''];
348
        $this->pTextBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
349
        $tsHora = $this->pConvertTime($this->dhEvento);
350
        $texto = 'Criado em : ' . date('d/m/Y   H:i:s', $tsHora);
351
        $this->pTextBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
352
        $tsHora = $this->pConvertTime($this->dhRegEvento);
353
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
354
        $this->pTextBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
355
        // ####################################################
356
        $x = $oldX;
357
        $this->pTextBox($x, $y1, $maxW, 40);
358
        $sY = $y1 + 40;
359
        $texto = 'De acordo com as determinações legais vigentes, vimos por meio desta comunicar-lhe'.
360
            ' que o Conhecimento, abaixo referenciada, contêm irregularidades que estão destacadas e' .
361
            ' suas respectivas correções, solicitamos que sejam aplicadas essas correções ao executar'.
362
            ' seus lançamentos fiscais.';
363
        $aFont = array(
364
            'font' => $this->fontePadrao,
365
            'size' => 10,
366
            'style' => ''
367
        );
368
        $this->pTextBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
369
        // ############################################
370
        $x = $oldX;
371
        $y = $y1;
372
        if ($this->CNPJDest != '') {
373
            $texto = 'CNPJ do Destinatário: ' . $this->pFormat($this->CNPJDest, "##.###.###/####-##");
0 ignored issues
show
Unused Code introduced by
$texto is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
374
        }
375
        if ($this->CPFDest != '') {
376
            $texto = 'CPF do Destinatário: ' . $this->pFormat($this->CPFDest, "###.###.###-##");
0 ignored issues
show
Unused Code introduced by
$texto is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
377
        }
378
        $aFont = array(
379
            'font' => $this->fontePadrao,
380
            'size' => 12,
381
            'style' => 'B'
382
        );
383
        
384
        $numNF = substr($this->chCTe, 25, 9);
385
        $serie = substr($this->chCTe, 22, 3);
386
        $numNF = $this->pFormat($numNF, "###.###.###");
387
        $texto = "Conhecimento: " . $numNF . '  -   Série: ' . $serie;
388
        $this->pTextBox($x + 2, $y + 19, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
389
        $bW = 87;
390
        $bH = 15;
391
        $x = 55;
392
        $y = $y1 + 13;
393
        $w = $maxW;
394
        $this->pdf->setFillColor(0, 0, 0);
395
        $this->pdf->code128($x + (($w - $bW) / 2), $y + 2, $this->chCTe, $bW, $bH);
396
        $this->pdf->setFillColor(255, 255, 255);
397
        $y1 = $y + 2 + $bH;
398
        $aFont = array(
399
            'font' => $this->fontePadrao,
400
            'size' => 10,
401
            'style' => ''
402
        );
403
        $texto = $this->pFormat($this->chCTe, $this->formatoChave);
404
        $this->pTextBox($x, $y1, $w - 2, $h, $texto, $aFont, 'T', 'C', 0, '');
405
        $x = $oldX;
406
        $this->pTextBox($x, $sY, $maxW, 15);
407
        $texto = $this->xCondUso;
408
        $aFont = array(
409
            'font' => $this->fontePadrao,
410
            'size' => 8,
411
            'style' => 'I'
412
        );
413
        $this->pTextBox($x + 2, $sY + 2, $maxW - 2, 15, $texto, $aFont, 'T', 'L', 0, '', false);
414
        return $sY + 2;
415
    }
416
417
    /**
418
     * pBody
419
     *
420
     * @param number $x
421
     * @param number $y
422
     */
423
    private function pBody($x, $y)
424
    {
425
        if ($this->orientacao == 'P') {
426
            $maxH = 190;
427
        } else {
428
            $maxH = 95;
429
        }
430
        $maxW = $this->wPrint;
431
        $texto = 'CORREÇÕES A SEREM CONSIDERADAS';
432
        $aFont = array(
433
            'font' => $this->fontePadrao,
434
            'size' => 10,
435
            'style' => 'B'
436
        );
437
        $this->pTextBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false);
438
        $y += 5;
439
        $this->pTextBox($x, $y, $maxW, $maxH);
440
        
441
        
442
         $aFont = array(
443
            'font' => $this->fontePadrao,
444
            'size' => 9,
445
            'style' => 'B'
446
        );
447
        $this->pTextBox($x, $y, $maxW = ($maxW/5), 5, "Grupo", $aFont, 'T', 'C', 0, '', false);
448
        $this->pTextBox($x = $maxW, $y, $maxW, 5, "Campo", $aFont, 'T', 'C', 0, '', false);
449
        $this->pTextBox($x = ($maxW*2), $y, $maxW, 5, "Número", $aFont, 'T', 'C', 0, '', false);
450
        $this->pTextBox($x = ($maxW*3), $y, ($this->wPrint-$x), 5, "Valor", $aFont, 'T', 'C', 0, '', false);
451
        
452
        $aFont = array(
453
            'font' => $this->fontePadrao,
454
            'size' => 9,
455
            'style' => ''
456
        );
457
        //Correções
458
        $i=0;
459
        $numlinhas = 1;
460
        while ($i < $this->infCorrecao->length) {
461
            $x = 0;
462
            $y = $numlinhas == 1 ? ($y+5) : ($y+(5*$numlinhas));
463
            $maxW = $this->wPrint;
464
            $grupo = $this->infCorrecao->item($i)->getElementsByTagName('grupoAlterado')->item(0)->nodeValue;
465
            $campo = $this->infCorrecao->item($i)->getElementsByTagName('campoAlterado')->item(0)->nodeValue;
466
            $numero = $this->infCorrecao->item($i)->getElementsByTagName('nroItemAlterado')->item(0)->nodeValue;
467
            $valor = $this->infCorrecao->item($i)->getElementsByTagName('valorAlterado')->item(0)->nodeValue;
468
                                    
469
            $i++;
470
            $this->pTextBox($x, $y, $maxW = ($maxW/5), 5, $grupo, $aFont, 'T', 'C', 0, '', false);
471
            $this->pTextBox($x = $maxW, $y, $maxW, 5, $campo, $aFont, 'T', 'C', 0, '', false);
472
            $this->pTextBox($x = ($maxW*2), $y, $maxW, 5, $numero, $aFont, 'T', 'C', 0, '', false);
473
            $this->pTextBox($x = ($maxW*3), $y, ($this->wPrint-$x), 5, $valor, $aFont, 'T', 'C', 0, '', false);
474
            $numlinhas = $this->pGetNumLines($valor, ($this->wPrint-$x), $aFont);
475
        } //fim da soma das areas de itens usadas
476
                        
477
        
478
        //$texto = str_replace(";", PHP_EOL, $this->xCorrecao);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
479
        $aFont = array(
0 ignored issues
show
Unused Code introduced by
$aFont is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
480
            'font' => $this->fontePadrao,
481
            'size' => 12,
482
            'style' => 'B'
483
        );
484
        //$this->pTextBox($x + 2, $y + 2, $maxW - 2, 150, $texto, $aFont, 'T', 'L', 0, '', false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
485
        $maxW = $this->wPrint;
486 View Code Duplication
        if ($this->tpAmb != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
487
            $x = 10;
488
            if ($this->orientacao == 'P') {
489
                $y = round($this->hPrint * 2 / 3, 0);
490
            } else {
491
                $y = round($this->hPrint * 2 / 3, 0);
492
            }
493
            $h = 5;
494
            $w = $maxW - (2 * $x);
495
            $this->pdf->SetTextColor(90, 90, 90);
496
            $texto = "SEM VALOR FISCAL";
497
            $aFont = array(
498
                'font' => $this->fontePadrao,
499
                'size' => 48,
500
                'style' => 'B'
501
            );
502
            $this->pTextBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
503
            $aFont = array(
504
                'font' => $this->fontePadrao,
505
                'size' => 30,
506
                'style' => 'B'
507
            );
508
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
509
            $this->pTextBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
510
            $this->pdf->SetTextColor(0, 0, 0);
511
        }
512
    }
513
514
    /**
515
     * pFooter
516
     *
517
     * @param number $x
518
     * @param number $y
519
     */
520 View Code Duplication
    protected function pFooter($x, $y)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
521
    {
522
        $w = $this->wPrint;
523
        $texto = "Este documento é uma representação gráfica da CC-e e foi impresso apenas para sua"
524
            . " informação e não possue validade fiscal.\n A CC-e deve ser recebida e mantida em"
525
            . " arquivo eletrônico XML e pode ser consultada através dos Portais das SEFAZ.";
526
        $aFont = array(
527
            'font' => $this->fontePadrao,
528
            'size' => 10,
529
            'style' => 'I'
530
        );
531
        $this->pTextBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
532
        $y = $this->hPrint - 4;
533
        $texto = "Impresso em  " . date('d/m/Y   H:i:s');
534
        $w = $this->wPrint - 4;
535
        $aFont = array(
536
            'font' => $this->fontePadrao,
537
            'size' => 6,
538
            'style' => 'I'
539
        );
540
        $this->pTextBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
541
        $texto = "Dacce ver. " . $this->version . "  Powered by NFePHP (GNU/GPLv3 GNU/LGPLv3) © www.nfephp.org";
542
        $aFont = array(
543
            'font' => $this->fontePadrao,
544
            'size' => 6,
545
            'style' => 'I'
546
        );
547
        $this->pTextBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org');
548
    }
549
550
    /**
551
     * printDocument
552
     *
553
     * @param  string $nome
554
     * @param  string $destino
555
     * @param  string $printer
556
     * @return mixed
557
     */
558
    public function printDocument($nome = '', $destino = 'I', $printer = '')
559
    {
560
        return $this->printDACCE($nome, $destino, $printer);
561
    }
562
563
    /**
564
     * printDACCE
565
     *
566
     * @param  string $nome
567
     * @param  string $destino
568
     * @param  string $printer
569
     * @return mixed
570
     */
571 View Code Duplication
    public function printDACCE($nome = '', $destino = 'I', $printer = '')
0 ignored issues
show
Unused Code introduced by
The parameter $printer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
572
    {
573
        if ($this->pdf == null) {
574
            $this->pBuildDACCE();
575
        }
576
        return $this->pdf->Output($nome, $destino);
577
    }
578
}
579