Completed
Push — master ( 0b1d68...e7164a )
by Roberto
12:40 queued 09:51
created

src/CTe/Dacce.php (16 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-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
use NFePHP\DA\Legacy\Dom;
18
use NFePHP\DA\Legacy\Pdf;
19
use NFePHP\DA\Legacy\Common;
20
21
class Dacce extends Common
22
{
23
24
    public $chCTe;
25
    protected $logoAlign = 'C';
26
    protected $yDados = 0;
27
    protected $debugmode = false;
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 $infCorrecao;
48
    protected $xCondUso;
49
    protected $dhEvento;
50
    protected $cStat;
51
    protected $xMotivo;
52
    protected $dhRegEvento;
53
    protected $nProt;
54
    protected $creditos;
55
56
    private $dom;
57
    private $procEventoCTe;
58
    private $eventoCTe;
59
    private $infEvento;
60
    private $retEventoCTe;
61
    private $retInfEvento;
62
63
    /**
64
     * __construct
65
     *
66
     * @param string $docXML      Arquivo XML (diretório ou string)
67
     * @param string $sOrientacao (Opcional) Orientação da impressão P-retrato L-Paisagem
68
     * @param string $sPapel      Tamanho do papel (Ex. A4)
69
     * @param string $sPathLogo   Caminho para o arquivo do logo
70
     * @param string $sDestino    Destino do PDF I-browser D-download S-string F-salva
71
     * @param array  $aEnd        array com o endereço do emitente
72
     * @param string $sDirPDF     Caminho para o diretorio de armazenamento dos arquivos PDF
73
     * @param string $fonteDANFE  Nome da fonte alternativa do DAnfe
74
     * @param number $mododebug   0-Não 1-Sim e 2-nada (2 default)
75
     */
76
    public function __construct($xml, $dadosEmitente)
77
    {
78
        $this->dadosEmitente = $dadosEmitente;
79
        $this->debugMode();
80
        $this->loadDoc($xml);
81
    }
82
83
    /**
84
     * Ativa ou desativa o modo debug
85
     * @param bool $activate
86
     * @return bool
87
     */
88
    public function debugMode($activate = null)
89
    {
90
        if (isset($activate) && is_bool($activate)) {
91
            $this->debugmode = $activate;
92
        }
93
        if ($this->debugmode) {
94
            error_reporting(E_ALL);
95
            ini_set('display_errors', 'On');
96
        } else {
97
            error_reporting(0);
98
            ini_set('display_errors', 'Off');
99
        }
100
        return $this->debugmode;
101
    }
102
103
    /**
104
     * Add the credits to the integrator in the footer message
105
     * @param string $message
106
     */
107
    public function creditsIntegratorFooter($message = '')
108
    {
109
        $this->creditos = trim($message);
110
    }
111
112
    /**
113
     * Dados brutos do PDF
114
     * @return string
115
     */
116
    public function render()
117
    {
118
        if ($this->pdf == null) {
119
            $this->buildDACCE();
120
        }
121
        return $this->pdf->getPdf();
122
    }
123
124
    protected function loadDoc($xml)
125
    {
126
        $this->dom = new Dom();
127
        $this->dom->loadXML($this->xml);
128
        if (empty($this->dom->getElementsByTagName("eventoCTe")->item(0))) {
129
            throw new \Exception("Este xml não é um evento do CTe");
130
        }
131
        $this->procEventoCTe = $this->dom->getElementsByTagName("procEventoCTe")->item(0);
132
        $this->eventoCTe = $this->procEventoCTe->getElementsByTagName("eventoCTe")->item(0);
133
        $this->retEventoCTe = $this->procEventoCTe->getElementsByTagName("retEventoCTe")->item(0);
134
        $this->infEvento = $this->eventoCTe->getElementsByTagName("infEvento")->item(0);
135
        $this->retInfEvento = $this->retEventoCTe->getElementsByTagName("infEvento")->item(0);
136
        $tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
137
        if ($tpEvento != '110110') {
138
            throw new \Exception('Um evento de CC-e deve ser passado.');
139
        }
140
        $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
141
        $this->chCTe = $this->infEvento->getElementsByTagName("chCTe")->item(0)->nodeValue;
142
        $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
143
        $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
144
        $this->infCorrecao = $this->infEvento->getElementsByTagName("infCorrecao");
145
        $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0)->nodeValue;
146
        $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
147
        $this->cStat = $this->retInfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
148
        $this->xMotivo = $this->retInfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
149
        $this->CNPJDest = !empty($this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue) ?
0 ignored issues
show
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...
150
            $this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue : '';
151
        $this->CPFDest = !empty($this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue) ?
0 ignored issues
show
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...
152
            $this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue : '';
153
        $this->dhRegEvento = $this->retInfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
154
        $this->nProt = $this->retInfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
155
    }
156
157
    /**
158
     * monta
159
     *
160
     * @param string $orientacao
161
     * @param string $papel
162
     * @param string $logoAlign
163
     */
164
    public function monta($orientacao = 'P', $papel = 'A4', $logoAlign = 'C')
165
    {
166
        $this->orientacao = $orientacao;
167
        $this->papel = $papel;
168
        $this->logoAlign = $logoAlign;
169
        $this->buildDACCE();
170
    }
171
172
    /**
173
     * buildDACCE
174
     */
175
    private function buildDACCE()
176
    {
177
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
178
        if ($this->orientacao == 'P') {
179
            $margSup = 2;
180
            $margEsq = 2;
181
            $margDir = 2;
182
            $xInic = 1;
183
            $yInic = 1;
184
            if ($this->papel == 'A4') { // A4 210x297mm
185
                $maxW = 210;
186
                $maxH = 297;
187
            }
188
        } else {
189
            $margSup = 3;
190
            $margEsq = 3;
191
            $margDir = 3;
192
            $xInic = 5;
193
            $yInic = 5;
194
            if ($this->papel == 'A4') { // A4 210x297mm
195
                $maxH = 210;
196
                $maxW = 297;
197
            }
198
        }
199
        // largura imprimivel em mm
200
        $this->wPrint = $maxW - ($margEsq + $xInic);
0 ignored issues
show
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
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->header($x, $y, $pag);
221
        // coloca os dados da CCe
222
        $y = $this->body($x, $y + 15);
223
        // coloca o rodapé
224
        $y = $this->footer($x, $y + $this->hPrint - 20);
0 ignored issues
show
$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
     * header
229
     *
230
     * @param  number $x
231
     * @param  number $y
232
     * @param  number $pag
233
     * @return number
234
     */
235
    private function header($x, $y, $pag)
236
    {
237
        $oldX = $x;
238
        $oldY = $y;
239
        $maxW = $this->wPrint;
240
        $w = round($maxW * 0.41, 0); // 80;
241
        if ($this->orientacao == 'P') {
242
            $aFont = array(
243
                'font'  => $this->fontePadrao,
244
                'size'  => 6,
245
                'style' => 'I'
246
            );
247
        } else {
248
            $aFont = array(
249
                'font'  => $this->fontePadrao,
250
                'size'  => 8,
251
                'style' => 'B'
252
            );
253
        }
254
        $w1 = $w;
0 ignored issues
show
$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...
255
        $h = 32;
256
        $oldY += $h;
257
        $this->pdf->textBox($x, $y, $w, $h);
258
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
259
        $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
260
        if (is_file($this->logomarca)) {
261
            $logoInfo = getimagesize($this->logomarca);
262
            // largura da imagem em mm
263
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
264
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
265
            if ($this->logoAlign == 'L') {
266
                $nImgW = round($w / 3, 0);
267
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
268
                $xImg = $x + 1;
269
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
270
                // estabelecer posições do texto
271
                $x1 = round($xImg + $nImgW + 1, 0);
272
                $y1 = round($h / 3 + $y, 0);
273
                $tw = round(2 * $w / 3, 0);
274
            }
275
            if ($this->logoAlign == 'C') {
276
                $nImgH = round($h / 3, 0);
277
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
278
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
279
                $yImg = $y + 3;
280
                $x1 = $x;
281
                $y1 = round($yImg + $nImgH + 1, 0);
282
                $tw = $w;
283
            }
284
            if ($this->logoAlign == 'R') {
285
                $nImgW = round($w / 3, 0);
286
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
287
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
288
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
289
                $x1 = $x;
290
                $y1 = round($h / 3 + $y, 0);
291
                $tw = round(2 * $w / 3, 0);
292
            }
293
            $this->pdf->Image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH);
0 ignored issues
show
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...
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...
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...
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...
294
        } else {
295
            $x1 = $x;
296
            $y1 = round($h / 3 + $y, 0);
297
            $tw = $w;
298
        }
299
        // Nome emitente
300
        $aFont = ['font' => $this->fontePadrao, 'size' => 12, 'style' => 'B'];
301
        $texto = $this->dadosEmitente['razao'];
302
        $this->pdf->textBox($x1, $y1, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
0 ignored issues
show
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...
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...
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...
303
        // endereço
304
        $y1 = $y1 + 6;
305
        $aFont = ['font' => $this->fontePadrao, 'size' => 8, 'style' => ''];
306
        $lgr = $this->dadosEmitente['logradouro'];
307
        $nro = $this->dadosEmitente['numero'];
308
        $cpl = $this->dadosEmitente['complemento'];
309
        $bairro = $this->dadosEmitente['bairro'];
310
        $CEP = $this->dadosEmitente['CEP'];
311
        // $CEP = $this->formatField($CEP, "#####-###");
312
        $mun = $this->dadosEmitente['municipio'];
313
        $UF = $this->dadosEmitente['UF'];
314
        $fone = $this->dadosEmitente['telefone'];
315
        $email = $this->dadosEmitente['email'];
316
        if ($email != '') {
317
            $email = 'Email: ' . $email;
318
        }
319
        $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - " . $CEP . "\n"
320
            . $mun . " - " . $UF . " - " . $fone . "\n" . $email;
321
        $this->pdf->textBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
322
323
        $w2 = round($maxW - $w, 0);
324
        $x += $w;
325
        $this->pdf->textBox($x, $y, $w2, $h);
326
        $y1 = $y + $h;
327
        $aFont = ['font' => $this->fontePadrao, 'size' => 16, 'style' => 'B'];
328
        $this->pdf->textBox($x, $y + 2, $w2, 8, 'Representação Gráfica de CC-e', $aFont, 'T', 'C', 0, '');
329
        $aFont = array(
330
            'font'  => $this->fontePadrao,
331
            'size'  => 12,
332
            'style' => 'I'
333
        );
334
        $this->pdf->textBox($x, $y + 7, $w2, 8, '(Carta de Correção Eletrônica)', $aFont, 'T', 'C', 0, '');
335
        $texto = 'ID do Evento: ' . $this->id;
336
        $aFont = ['font' => $this->fontePadrao, 'size' => 10, 'style' => ''];
337
        $this->pdf->textBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
338
        $tsHora = $this->convertTime($this->dhEvento);
339
        $texto = 'Criado em : ' . date('d/m/Y H:i:s', $tsHora);
340
        $this->pdf->textBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
341
        $tsHora = $this->convertTime($this->dhRegEvento);
342
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
343
        $this->pdf->textBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
344
345
        $x = $oldX;
346
        $this->pdf->textBox($x, $y1, $maxW, 40);
347
        $sY = $y1 + 40;
348
        $texto = 'De acordo com as determinações legais vigentes, vimos por meio desta comunicar-lhe' .
349
            ' que o Conhecimento, abaixo referenciada, contêm irregularidades que estão destacadas e' .
350
            ' suas respectivas correções, solicitamos que sejam aplicadas essas correções ao executar' .
351
            ' seus lançamentos fiscais.';
352
        $aFont = array(
353
            'font'  => $this->fontePadrao,
354
            'size'  => 10,
355
            'style' => ''
356
        );
357
        $this->pdf->textBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
358
        $x = $oldX;
359
        $y = $y1;
360
        if ($this->CNPJDest != '') {
361
            $texto = 'CNPJ do Destinatário: ' . $this->formatField($this->CNPJDest, "##.###.###/####-##");
0 ignored issues
show
$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...
362
        }
363
        if ($this->CPFDest != '') {
364
            $texto = 'CPF do Destinatário: ' . $this->formatField($this->CPFDest, "###.###.###-##");
0 ignored issues
show
$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...
365
        }
366
        $aFont = array(
367
            'font'  => $this->fontePadrao,
368
            'size'  => 12,
369
            'style' => 'B'
370
        );
371
        $numNF = substr($this->chCTe, 25, 9);
372
        $serie = substr($this->chCTe, 22, 3);
373
        $numNF = $this->formatField($numNF, "###.###.###");
374
        $texto = "Conhecimento: " . $numNF . '  -   Série: ' . $serie;
375
        $this->pdf->textBox($x + 2, $y + 19, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
376
        $bW = 87;
377
        $bH = 15;
378
        $x = 55;
379
        $y = $y1 + 13;
380
        $w = $maxW;
381
        $this->pdf->setFillColor(0, 0, 0);
382
        $this->pdf->code128($x + (($w - $bW) / 2), $y + 2, $this->chCTe, $bW, $bH);
383
        $this->pdf->setFillColor(255, 255, 255);
384
        $y1 = $y + 2 + $bH;
385
        $aFont = array(
386
            'font'  => $this->fontePadrao,
387
            'size'  => 10,
388
            'style' => ''
389
        );
390
        $texto = $this->formatField($this->chCTe, $this->formatoChave);
391
        $this->pdf->textBox($x, $y1, $w - 2, $h, $texto, $aFont, 'T', 'C', 0, '');
392
        $x = $oldX;
393
        $this->pdf->textBox($x, $sY, $maxW, 15);
394
        $texto = $this->xCondUso;
395
        $aFont = array(
396
            'font'  => $this->fontePadrao,
397
            'size'  => 8,
398
            'style' => 'I'
399
        );
400
        $this->pdf->textBox($x + 2, $sY + 2, $maxW - 2, 15, $texto, $aFont, 'T', 'L', 0, '', false);
401
        return $sY + 2;
402
    }
403
404
    /**
405
     * body
406
     *
407
     * @param number $x
408
     * @param number $y
409
     */
410
    private function body($x, $y)
411
    {
412
        if ($this->orientacao == 'P') {
413
            $maxH = 190;
414
        } else {
415
            $maxH = 95;
416
        }
417
        $maxW = $this->wPrint;
418
        $texto = 'CORREÇÕES A SEREM CONSIDERADAS';
419
        $aFont = array(
420
            'font'  => $this->fontePadrao,
421
            'size'  => 10,
422
            'style' => 'B'
423
        );
424
        $this->pdf->textBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false);
425
        $y += 5;
426
        $this->pdf->textBox($x, $y, $maxW, $maxH);
427
        $aFont = array(
428
            'font'  => $this->fontePadrao,
429
            'size'  => 9,
430
            'style' => 'B'
431
        );
432
        $this->pdf->textBox($x, $y, $maxW = ($maxW / 5), 5, "Grupo", $aFont, 'T', 'C', 0, '', false);
433
        $this->pdf->textBox($x = $maxW, $y, $maxW, 5, "Campo", $aFont, 'T', 'C', 0, '', false);
434
        $this->pdf->textBox($x = ($maxW * 2), $y, $maxW, 5, "Número", $aFont, 'T', 'C', 0, '', false);
435
        $this->pdf->textBox($x = ($maxW * 3), $y, ($this->wPrint - $x), 5, "Valor", $aFont, 'T', 'C', 0, '', false);
436
        $aFont = array(
437
            'font'  => $this->fontePadrao,
438
            'size'  => 9,
439
            'style' => ''
440
        );
441
        $i = 0;
442
        $numlinhas = 1;
443
        while ($i < $this->infCorrecao->length) {
444
            $x = 0;
445
            $y = $numlinhas == 1 ? ($y + 5) : ($y + (5 * $numlinhas));
446
            $maxW = $this->wPrint;
447
            $grupo = $this->infCorrecao->item($i)->getElementsByTagName('grupoAlterado')->item(0)->nodeValue;
448
            $campo = $this->infCorrecao->item($i)->getElementsByTagName('campoAlterado')->item(0)->nodeValue;
449
            $numero = $this->infCorrecao->item($i)->getElementsByTagName('nroItemAlterado')->item(0)->nodeValue;
450
            $valor = $this->infCorrecao->item($i)->getElementsByTagName('valorAlterado')->item(0)->nodeValue;
451
452
            $i++;
453
            $this->pdf->textBox($x, $y, $maxW = ($maxW / 5), 5, $grupo, $aFont, 'T', 'C', 0, '', false);
454
            $this->pdf->textBox($x = $maxW, $y, $maxW, 5, $campo, $aFont, 'T', 'C', 0, '', false);
455
            $this->pdf->textBox($x = ($maxW * 2), $y, $maxW, 5, $numero, $aFont, 'T', 'C', 0, '', false);
456
            $this->pdf->textBox($x = ($maxW * 3), $y, ($this->wPrint - $x), 5, $valor, $aFont, 'T', 'C', 0, '', false);
457
            $numlinhas = $this->pdf->getNumLines($valor, ($this->wPrint - $x), $aFont);
458
        }
459
        $aFont = array(
0 ignored issues
show
$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...
460
            'font'  => $this->fontePadrao,
461
            'size'  => 12,
462
            'style' => 'B'
463
        );
464
        $maxW = $this->wPrint;
465
        if ($this->tpAmb != 1) {
466
            $x = 10;
467
            if ($this->orientacao == 'P') {
468
                $y = round($this->hPrint * 2 / 3, 0);
469
            } else {
470
                $y = round($this->hPrint * 2 / 3, 0);
471
            }
472
            $h = 5;
473
            $w = $maxW - (2 * $x);
474
            $this->pdf->setTextColor(90, 90, 90);
475
            $texto = "SEM VALOR FISCAL";
476
            $aFont = array(
477
                'font'  => $this->fontePadrao,
478
                'size'  => 48,
479
                'style' => 'B'
480
            );
481
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
482
            $aFont = array(
483
                'font'  => $this->fontePadrao,
484
                'size'  => 30,
485
                'style' => 'B'
486
            );
487
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
488
            $this->pdf->textBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
489
            $this->pdf->setTextColor(0, 0, 0);
490
        }
491
    }
492
493
    /**
494
     * footer
495
     *
496
     * @param number $x
497
     * @param number $y
498
     */
499
    protected function footer($x, $y)
500
    {
501
        $w = $this->wPrint;
502
        $texto = "Este documento é uma representação gráfica da CC-e e foi impresso apenas para sua"
503
            . " informação e não possue validade fiscal.\n A CC-e deve ser recebida e mantida em"
504
            . " arquivo eletrônico XML e pode ser consultada através dos Portais das SEFAZ.";
505
        $aFont = array(
506
            'font'  => $this->fontePadrao,
507
            'size'  => 10,
508
            'style' => 'I'
509
        );
510
        $this->pdf->textBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
511
        $y = $this->hPrint - 4;
512
        $texto = "Impresso em  " . date('d/m/Y   H:i:s') . " - " . $this->creditos;
513
        $w = $this->wPrint - 4;
514
        $aFont = array(
515
            'font'  => $this->fontePadrao,
516
            'size'  => 6,
517
            'style' => 'I'
518
        );
519
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
520
        $texto = "Powered by NFePHP®";
521
        $aFont = array(
522
            'font'  => $this->fontePadrao,
523
            'size'  => 6,
524
            'style' => 'I'
525
        );
526
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, '');
527
    }
528
}
529