Passed
Push — master ( f8305a...b6c7f7 )
by Roberto
03:01 queued 11s
created

Daevento::debugMode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 14
ccs 0
cts 14
cp 0
crap 20
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\CTe;
4
5
/**
6
 * Classe para geração do envento do CTe em PDF
7
 * NOTA: Este documento não está NORMALIZADO, nem é requerido pela SEFAZ
8
 *
9
 * @category  Library
10
 * @package   nfephp-org/sped-da
11
 * @name      Daevento.php
12
 * @copyright 2009-2019 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\DA\Legacy\Dom;
20
use NFePHP\DA\Legacy\Pdf;
21
use NFePHP\DA\Common\DaCommon;
22
23
class Daevento extends DaCommon
24
{
25
    public $chCTe;
26
    protected $logoAlign = 'C';
27
    protected $yDados = 0;
28
    protected $dadosEmitente = 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 $tpAmb;
46
    protected $cOrgao;
47
    protected $xCorrecao;
48
    protected $xCondUso;
49
    protected $dhEvento;
50
    protected $cStat;
51
    protected $xMotivo;
52
    protected $xJust;
53
    protected $CNPJDest = '';
54
    protected $CPFDest = '';
55
    protected $dhRegEvento;
56
    protected $nProt;
57
    protected $tpEvento;
58
    protected $creditos;
59
    
60
    private $dom;
61
    private $procEventoCTe;
62
    private $evento;
63
    private $infEvento;
64
    private $retEvento;
65
    private $rinfEvento;
66
    
67
68
    /**
69
     * __construct
70
     *
71
     * @param string $xml Arquivo XML (diretório ou string)
72
     * @param array $dadosEmitente Dados do endereço do emitente
73
     */
74
    public function __construct($xml, $dadosEmitente)
75
    {
76
        $this->dadosEmitente = $dadosEmitente;
77
        $this->loadDoc($xml);
78
    }
79
80
    protected function loadDoc($xml)
81
    {
82
        $this->dom = new \DomDocument;
83
        $this->dom->loadXML($xml);
84
        $this->procEventoCTe = $this->dom->getElementsByTagName("procEventoCTe")->item(0);
85
        $this->evento = $this->dom->getElementsByTagName("eventoCTe")->item(0);
86
        $this->infEvento = $this->evento->getElementsByTagName("infEvento")->item(0);
87
        $this->retEvento = $this->dom->getElementsByTagName("retEventoCTe")->item(0);
88
        $this->rinfEvento = $this->retEvento->getElementsByTagName("infEvento")->item(0);
89
        $this->tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
90
        if (!in_array($this->tpEvento, array('110110', '110111'))) {
91
            $this->errMsg = 'Evento não implementado ' . $tpEvento . ' !!';
0 ignored issues
show
Bug introduced by
The variable $tpEvento does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
92
            $this->errStatus = true;
93
            return false;
94
        }
95
        $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
96
        $this->chCTe = $this->infEvento->getElementsByTagName("chCTe")->item(0)->nodeValue;
97
        $this->dadosEmitente['CNPJ'] = substr($this->chCTe, 6, 14);
98
        $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
99
        $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
100
        $this->xCorrecao = $this->infEvento->getElementsByTagName("xCorrecao")->item(0);
101
        $this->xCorrecao = (empty($this->xCorrecao) ? '' : $this->xCorrecao->nodeValue);
102
        $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0);
103
        $this->xCondUso = (empty($this->xCondUso) ? '' : $this->xCondUso->nodeValue);
104
        $this->xJust = $this->infEvento->getElementsByTagName("xJust")->item(0);
105
        $this->xJust = (empty($this->xJust) ? '' : $this->xJust->nodeValue);
106
        $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
107
        $this->cStat = $this->rinfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
108
        $this->xMotivo = $this->rinfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
109
        $this->CNPJDest = !empty($this->rinfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue) ?
110
            $this->rinfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue : '';
111
        $this->CPFDest = !empty($this->rinfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue) ?
112
            $this->rinfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue : '';
113
        $this->dhRegEvento = $this->rinfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
114
        $this->nProt = $this->rinfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
115
    }
116
117
    /**
118
     * Add the credits to the integrator in the footer message
119
     * @param string $message
120
     */
121
    public function creditsIntegratorFooter($message = '')
122
    {
123
        $this->creditos = trim($message);
124
    }
125
126
    /**
127
     * monta
128
     * Esta função monta a DAEventoCTe conforme as informações fornecidas para a classe
129
     * durante sua construção.
130
     * A definição de margens e posições iniciais para a impressão são estabelecidas no
131
     * pelo conteúdo da funçao e podem ser modificados.
132
     *
133
     * @param string $logo base64 da logomarca
134
     * @param string $orientacao (Opcional) Estabelece a orientação da impressão (ex. P-retrato),
135
     *               se nada for fornecido será usado o padrão da CTe
136
     * @param string $papel (Opcional) Estabelece o tamanho do papel (ex. A4)
137
     * @return string O ID do evento extraido do arquivo XML
138
     */
139
    protected function monta(
140
        $logo = '',
141
        $orientacao = '',
142
        $papel = 'A4',
143
        $logoAlign = 'C'
144
    ) {
145
        if ($orientacao == '') {
146
            $orientacao = 'P';
147
        }
148
        $this->logomarca = $this->adjustImage($logo);
149
        $this->orientacao = $orientacao;
150
        $this->papel = $papel;
151
        $this->logoAlign = $logoAlign;
152
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
153
        if ($this->orientacao == 'P') {
154
            // margens do PDF
155
            $margSup = 2;
156
            $margEsq = 2;
157
            $margDir = 2;
158
            // posição inicial do relatorio
159
            $xInic = 1;
160
            $yInic = 1;
161
            if ($this->papel == 'A4') { //A4 210x297mm
162
                $maxW = 210;
163
                $maxH = 297;
164
            }
165
        } else {
166
            // margens do PDF
167
            $margSup = 3;
168
            $margEsq = 3;
169
            $margDir = 3;
170
            // posição inicial do relatorio
171
            $xInic = 5;
172
            $yInic = 5;
173
            if ($papel == 'A4') {
174
                //A4 210x297mm
175
                $maxH = 210;
176
                $maxW = 297;
177
            }
178
        }
179
        //largura imprimivel em mm
180
        $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...
181
        //comprimento imprimivel em mm
182
        $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...
183
        // estabelece contagem de paginas
184
        $this->pdf->aliasNbPages();
185
        // fixa as margens
186
        $this->pdf->setMargins($margEsq, $margSup, $margDir);
187
        $this->pdf->setDrawColor(0, 0, 0);
188
        $this->pdf->setFillColor(255, 255, 255);
189
        // inicia o documento
190
        $this->pdf->open();
191
        // adiciona a primeira página
192
        $this->pdf->addPage($this->orientacao, $this->papel);
193
        $this->pdf->setLineWidth(0.1);
194
        $this->pdf->setTextColor(0, 0, 0);
195
        //montagem da página
196
        $pag = 1;
197
        $x = $xInic;
198
        $y = $yInic;
199
        //coloca o cabeçalho
200
        $y = $this->header($x, $y, $pag);
201
        //coloca os dados da CCe
202
        $y = $this->body($x, $y + 15);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $y is correct as $this->body($x, $y + 15) (which targets NFePHP\DA\CTe\Daevento::body()) 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...
203
        //coloca os dados da CCe
204
        $y = $this->footer($x, $y + $this->hPrint - 20);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $y is correct as $this->footer($x, $y + $this->hPrint - 20) (which targets NFePHP\DA\CTe\Daevento::footer()) 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...
205
        //retorna o ID do evento
206
        return $this->id;
207
    }
208
209
    /**
210
     * header
211
     * @param integer $x
212
     * @param integer $y
213
     * @param integer $pag
214
     * @return integer
215
     */
216
    private function header(
217
        $x,
218
        $y,
219
        $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...
220
    ) {
221
        $oldX = $x;
222
        $oldY = $y;
223
        $maxW = $this->wPrint;
224
        //####################################################################################
225
        //coluna esquerda identificação do emitente
226
        $w = round($maxW * 0.41, 0);// 80;
227
        if ($this->orientacao == 'P') {
228
            $aFont = array('font' => $this->fontePadrao, 'size' => 6, 'style' => 'I');
229
        } else {
230
            $aFont = array('font' => $this->fontePadrao, 'size' => 8, 'style' => 'B');
231
        }
232
        $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...
233
        $h = 32;
234
        $oldY += $h;
235
        $this->pdf->textBox($x, $y, $w, $h);
236
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
237
        $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
238
        if (!empty($this->logomarca)) {
239
            $logoInfo = getimagesize($this->logomarca);
240
            //largura da imagem em mm
241
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
242
            //altura da imagem em mm
243
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
244
            if ($this->logoAlign == 'L') {
245
                $nImgW = round($w / 3, 0);
246
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
247
                $xImg = $x + 1;
248
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
249
                //estabelecer posições do texto
250
                $x1 = round($xImg + $nImgW + 1, 0);
251
                $y1 = round($h / 3 + $y, 0);
252
                $tw = round(2 * $w / 3, 0);
253
            }
254
            if ($this->logoAlign == 'C') {
255
                $nImgH = round($h / 3, 0);
256
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
257
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
258
                $yImg = $y + 3;
259
                $x1 = $x;
260
                $y1 = round($yImg + $nImgH + 1, 0);
261
                $tw = $w;
262
            }
263
            if ($this->logoAlign == 'R') {
264
                $nImgW = round($w / 3, 0);
265
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
266
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
267
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
268
                $x1 = $x;
269
                $y1 = round($h / 3 + $y, 0);
270
                $tw = round(2 * $w / 3, 0);
271
            }
272
            $this->pdf->image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH, 'jpeg');
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...
273
        } else {
274
            $x1 = $x;
275
            $y1 = round($h / 3 + $y, 0);
276
            $tw = $w;
277
        }
278
        //Nome emitente
279
        $aFont = array('font' => $this->fontePadrao, 'size' => 12, 'style' => 'B');
280
        $texto = (isset($this->dadosEmitente['razao']) ? $this->dadosEmitente['razao'] : '');
281
        $this->pdf->textBox($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...
282
        //endereço
283
        $y1 = $y1 + 6;
284
        $aFont = array('font' => $this->fontePadrao, 'size' => 8, 'style' => '');
285
        $lgr = (isset($this->dadosEmitente['logradouro']) ? $this->dadosEmitente['logradouro'] : '');
286
        $nro = (isset($this->dadosEmitente['numero']) ? $this->dadosEmitente['numero'] : '');
287
        $cpl = (isset($this->dadosEmitente['complemento']) ? $this->dadosEmitente['complemento'] : '');
288
        $bairro = (isset($this->dadosEmitente['bairro']) ? $this->dadosEmitente['bairro'] : '');
289
        $CEP = (isset($this->dadosEmitente['CEP']) ? $this->dadosEmitente['CEP'] : '');
290
        $CEP = $this->formatField($CEP, "#####-###");
291
        $mun = (isset($this->dadosEmitente['municipio']) ? $this->dadosEmitente['municipio'] : '');
292
        $UF = (isset($this->dadosEmitente['UF']) ? $this->dadosEmitente['UF'] : '');
293
        $fone = (isset($this->dadosEmitente['telefone']) ? $this->dadosEmitente['telefone'] : '');
294
        $email = (isset($this->dadosEmitente['email']) ? $this->dadosEmitente['email'] : '');
295
        $foneLen = strlen($fone);
296
        if ($foneLen > 0) {
297
            $fone2 = substr($fone, 0, $foneLen - 4);
298
            $fone1 = substr($fone, 0, $foneLen - 8);
299
            $fone = '(' . $fone1 . ') ' . substr($fone2, -4) . '-' . substr($fone, -4);
300
        } else {
301
            $fone = '';
302
        }
303
        if ($email != '') {
304
            $email = 'Email: ' . $email;
305
        }
306
        $texto = "";
307
        $tmp_txt = trim(($lgr != '' ? "$lgr, " : '') . ($nro != 0 ? $nro : "SN") . ($cpl != '' ? " - $cpl" : ''));
308
        $tmp_txt = ($tmp_txt == 'SN' ? '' : $tmp_txt);
309
        $texto .= ($texto != '' && $tmp_txt != '' ? "\n" : '') . $tmp_txt;
310
        $tmp_txt = trim($bairro . ($bairro != '' && $CEP != '' ? " - " : '') . $CEP);
311
        $texto .= ($texto != '' && $tmp_txt != '' ? "\n" : '') . $tmp_txt;
312
        $tmp_txt = $mun;
313
        $tmp_txt .= ($tmp_txt != '' && $UF != '' ? " - " : '') . $UF;
314
        $tmp_txt .= ($tmp_txt != '' && $fone != '' ? " - " : '') . $fone;
315
        $texto .= ($texto != '' && $tmp_txt != '' ? "\n" : '') . $tmp_txt;
316
        $tmp_txt = $email;
317
        $texto .= ($texto != '' && $tmp_txt != '' ? "\n" : '') . $tmp_txt;
318
        $this->pdf->textBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
319
        //##################################################
320
        $w2 = round($maxW - $w, 0);
321
        $x += $w;
322
        $this->pdf->textBox($x, $y, $w2, $h);
323
        $y1 = $y + $h;
324
        $aFont = array('font' => $this->fontePadrao, 'size' => 16, 'style' => 'B');
325
        if ($this->tpEvento == '110110') {
326
            $texto = 'Representação Gráfica de CCe';
327
        } else {
328
            $texto = 'Representação Gráfica de Evento';
329
        }
330
        $this->pdf->textBox($x, $y + 2, $w2, 8, $texto, $aFont, 'T', 'C', 0, '');
331
        $aFont = array('font' => $this->fontePadrao, 'size' => 12, 'style' => 'I');
332
        if ($this->tpEvento == '110110') {
333
            $texto = '(Carta de Correção Eletrônica)';
334
        } elseif ($this->tpEvento == '110111') {
335
            $texto = '(Cancelamento de CTe)';
336
        }
337
        $this->pdf->textBox($x, $y + 7, $w2, 8, $texto, $aFont, 'T', 'C', 0, '');
338
        $texto = 'ID do Evento: ' . $this->id;
339
        $aFont = array('font' => $this->fontePadrao, 'size' => 10, 'style' => '');
340
        $this->pdf->textBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
341
        $tsHora = $this->toTimestamp($this->dhEvento);
342
        $texto = 'Criado em : ' . date('d/m/Y   H:i:s', $tsHora);
343
        $this->pdf->textBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
344
        $tsHora = $this->toTimestamp($this->dhRegEvento);
345
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
346
        $this->pdf->textBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
347
        //####################################################
348
        $x = $oldX;
349
        $this->pdf->textBox($x, $y1, $maxW, 40);
350
        $sY = $y1 + 40;
351
        if ($this->tpEvento == '110110') {
352
            $texto = 'De acordo com as determinações legais vigentes, vimos por meio '
353
                . 'desta comunicar-lhe que o Conhecimento de Transporte, abaixo referenciado, '
354
                . 'contêm irregularidades que estão destacadas e suas respectivas '
355
                . 'correções, solicitamos que sejam aplicadas essas correções ao '
356
                . 'executar seus lançamentos fiscais.';
357
        } elseif ($this->tpEvento == '110111') {
358
            $texto = 'De acordo com as determinações legais vigentes, vimos por meio '
359
                . 'desta comunicar-lhe que o  Conhecimento de Transporte, abaixo referenciado, está '
360
                . 'cancelado, solicitamos que sejam aplicadas essas correções ao '
361
                . 'executar seus lançamentos fiscais.';
362
        }
363
        $aFont = array('font' => $this->fontePadrao, 'size' => 10, 'style' => '');
364
        $this->pdf->textBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
365
        //############################################
366
        $x = $oldX;
367
        $y = $y1;
368
        $aFont = array('font' => $this->fontePadrao, 'size' => 12, 'style' => 'B');
369
        $numNF = substr($this->chCTe, 25, 9);
370
        $serie = substr($this->chCTe, 22, 3);
371
        $numNF = $this->formatField($numNF, "###.###.###");
372
        $texto = "Conhecimento: " . $numNF . '  -   Série: ' . $serie;
373
        $this->pdf->textBox($x + 2, $y + 19, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
374
        $bW = 87;
375
        $bH = 15;
376
        $x = 55;
377
        $y = $y1 + 13;
378
        $w = $maxW;
379
        $this->pdf->setFillColor(0, 0, 0);
380
        $this->pdf->code128($x + (($w - $bW) / 2), $y + 2, $this->chCTe, $bW, $bH);
381
        $this->pdf->setFillColor(255, 255, 255);
382
        $y1 = $y + 2 + $bH;
383
        $aFont = array('font' => $this->fontePadrao, 'size' => 10, 'style' => '');
384
        $texto = $this->formatField($this->chCTe, $this->formatoChave);
385
        $this->pdf->textBox($x, $y1, $w - 2, $h, $texto, $aFont, 'T', 'C', 0, '');
386
        $retVal = $sY + 2;
387
        if ($this->tpEvento == '110110') {
388
            $x = $oldX;
389
            $this->pdf->textBox($x, $sY, $maxW, 15);
390
            $texto = $this->xCondUso;
391
            $aFont = array('font' => $this->fontePadrao, 'size' => 8, 'style' => 'I');
392
            $this->pdf->textBox($x + 2, $sY + 2, $maxW - 2, 15, $texto, $aFont, 'T', 'L', 0, '', false);
393
            $retVal = $sY + 2;
394
        }
395
        if ($this->tpAmb != 1) {
396
            $x = 10;
397
            if ($this->orientacao == 'P') {
398
                $y = round($this->hPrint * 2 / 3, 0);
399
            } else {
400
                $y = round($this->hPrint / 2, 0);
401
            }
402
            $h = 5;
403
            $w = $maxW - (2 * $x);
404
            $this->pdf->setTextColor(90, 90, 90);
405
            $texto = "SEM VALOR FISCAL";
406
            $aFont = array('font' => $this->fontePadrao, 'size' => 48, 'style' => 'B');
407
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
408
            $aFont = array('font' => $this->fontePadrao, 'size' => 30, 'style' => 'B');
409
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
410
            $this->pdf->textBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
411
            $this->pdf->setTextColor(0, 0, 0);
412
        }
413
        return $retVal;
414
    }
415
416
    /**
417
     * body
418
     * @param integer $x
419
     * @param integer $y
420
     */
421
    private function body($x, $y)
422
    {
423
        $maxW = $this->wPrint;
424
        if ($this->tpEvento == '110110') {
425
            $texto = 'CORREÇÕES A SEREM CONSIDERADAS';
426
        } else {
427
            $texto = 'JUSTIFICATIVA DO CANCELAMENTO';
428
        }
429
        $aFont = array('font' => $this->fontePadrao, 'size' => 10, 'style' => 'B');
430
        $this->pdf->textBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false);
431
        $y += 5;
432
        $this->pdf->textBox($x, $y, $maxW, 190);
433
        if ($this->tpEvento == '110110') {
434
            $texto = $this->xCorrecao;
435
        } elseif ($this->tpEvento == '110111') {
436
            $texto = $this->xJust;
437
        }
438
        $aFont = array('font' => $this->fontePadrao, 'size' => 12, 'style' => 'B');
439
        $this->pdf->textBox($x + 2, $y + 2, $maxW - 2, 150, $texto, $aFont, 'T', 'L', 0, '', false);
440
    }
441
442
    /**
443
     * footer
444
     * @param integer $x
445
     * @param integer $y
446
     */
447
    private function footer($x, $y)
448
    {
449
        $w = $this->wPrint;
450
        if ($this->tpEvento == '110110') {
451
            $texto = "Este documento é uma representação gráfica da CCe e foi "
452
                . "impresso apenas para sua informação e não possue validade fiscal."
453
                . "\n A CCe deve ser recebida e mantida em arquivo eletrônico XML e "
454
                . "pode ser consultada através dos Portais das SEFAZ.";
455
        } elseif ($this->tpEvento == '110111') {
456
            $texto = "Este documento é uma representação gráfica do evento de CTe e foi "
457
                . "impresso apenas para sua informação e não possue validade fiscal."
458
                . "\n O Evento deve ser recebido e mantido em arquivo eletrônico XML e "
459
                . "pode ser consultada através dos Portais das SEFAZ.";
460
        }
461
        $aFont = array('font' => $this->fontePadrao, 'size' => 10, 'style' => 'I');
462
        $this->pdf->textBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
0 ignored issues
show
Bug introduced by
The variable $texto 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...
463
        $y = $this->hPrint - 4;
464
        $texto = "Impresso em  " . date('d/m/Y   H:i:s');
465
        $w = $this->wPrint - 4;
466
        $aFont = array('font' => $this->fontePadrao, 'size' => 6, 'style' => 'I');
467
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
468
        $texto = $this->creditos . "  Powered by NFePHP®";
469
        $aFont = array('font' => $this->fontePadrao, 'size' => 6, 'style' => 'I');
470
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org');
471
    }
472
}
473