Passed
Push — master ( 551514...553a8e )
by Roberto
02:41 queued 10s
created

Dacce::footer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 29
ccs 0
cts 29
cp 0
crap 2
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\NFe;
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
use Exception;
18
use NFePHP\DA\Legacy\Dom;
19
use NFePHP\DA\Legacy\Pdf;
20
use NFePHP\DA\Legacy\Common;
21
22
class Dacce extends Common
23
{
24
25
    public $chNFe;
26
    protected $logoAlign = 'C';
27
    protected $yDados = 0;
28
    protected $debugMode = 0;
29
    protected $aEnd = array();
30
    protected $pdf;
31
    protected $xml;
32
    protected $logomarca = '';
33
    protected $errMsg = '';
34
    protected $errStatus = false;
35
    protected $orientacao = 'P';
36
    protected $papel = 'A4';
37
    protected $destino = 'I';
38
    protected $pdfDir = '';
39
    protected $fontePadrao = 'Times';
40
    protected $version = '0.1.1';
41
    protected $wPrint;
42
    protected $hPrint;
43
    protected $wCanhoto;
44
    protected $formatoChave = "#### #### #### #### #### #### #### #### #### #### ####";
45
    protected $id;
46
    protected $tpAmb;
47
    protected $cOrgao;
48
    protected $xCorrecao;
49
    protected $xCondUso;
50
    protected $dhEvento;
51
    protected $cStat;
52
    protected $xMotivo;
53
    protected $CNPJDest = '';
54
    protected $CPFDest = '';
55
    protected $dhRegEvento;
56
    protected $nProt;
57
    private $dom;
58
    private $procEventoNFe;
59
    private $evento;
60
    private $infEvento;
61
    private $retEvento;
62
    private $retInfEvento;
63
64
    /**
65
     * Construtor recebe parametro pra impressao
66
     * @param string $docXML      conteudo do Arquivo XML
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
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...
74
     * @param number $mododebug   0-Não 1-Sim e 2-nada (2 default)
75
     */
76
    public function __construct(
77
        $docXML = '',
78
        $sOrientacao = '',
79
        $sPapel = '',
80
        $sPathLogo = '',
81
        $sDestino = 'I',
82
        $aEnd = '',
83
        $sDirPDF = '',
84
        $fontePDF = '',
85
        $mododebug = 0
86
    ) {
87
        if (is_numeric($mododebug)) {
88
            $this->debugMode = (int) $mododebug;
89
        }
90
        if ($this->debugMode === 1) {
91
            // ativar modo debug
92
            error_reporting(E_ALL);
93
            ini_set('display_errors', 'On');
94
        } elseif ($this->debugMode === 0) {
95
            // desativar modo debug
96
            error_reporting(0);
97
            ini_set('display_errors', 'Off');
98
        }
99
        if (is_array($aEnd)) {
100
            $this->aEnd = $aEnd;
101
        }
102
        $this->orientacao = $sOrientacao;
103
        $this->papel = $sPapel;
104
        $this->pdf = '';
105
        $this->xml = $docXML;
106
        $this->logomarca = $sPathLogo;
107
        $this->destino = $sDestino;
108
        $this->pdfDir = $sDirPDF;
109
        // verifica se foi passa a fonte a ser usada
110
        if (empty($fontePDF)) {
111
            $this->fontePadrao = 'Times';
112
        } else {
113
            $this->fontePadrao = $fontePDF;
114
        }
115
        // se for passado o xml
116
        if (!empty($this->xml)) {
117
            $this->dom = new Dom();
118
            $this->dom->loadXML($this->xml);
119
            $this->procEventoNFe = $this->dom->getElementsByTagName("procEventoNFe")->item(0);
120
            $this->evento = $this->procEventoNFe->getElementsByTagName("evento")->item(0);
121
            $this->retEvento = $this->procEventoNFe->getElementsByTagName("retEvento")->item(0);
122
            $this->infEvento = $this->evento->getElementsByTagName("infEvento")->item(0);
123
            $this->retInfEvento = $this->retEvento->getElementsByTagName("infEvento")->item(0);
124
            $tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
125
            if ($tpEvento != '110110') {
126
                $this->errMsg = 'Um evento de CC-e deve ser passado.';
127
                $this->errStatus = true;
128
                throw new Exception($this->errMsg);
129
            }
130
            $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
131
            $this->chNFe = $this->infEvento->getElementsByTagName("chNFe")->item(0)->nodeValue;
132
            $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
133
            $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
134
            $this->xCorrecao = $this->infEvento->getElementsByTagName("xCorrecao")->item(0)->nodeValue;
135
            $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0)->nodeValue;
136
            $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
137
            $this->cStat = $this->retInfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
138
            $this->xMotivo = $this->retInfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
139
            $this->CNPJDest = !empty($this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue) ?
140
                    $this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue
141
                    : '';
142
            $this->CPFDest = !empty($this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue) ?
143
                    $this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue
144
                    : '';
145
            $this->dhRegEvento = $this->retInfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
146
            $this->nProt = $this->retInfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
147
        }
148
    }
149
150
    /**
151
     * Monta o pdf
152
     * @param string $orientacao
153
     * @param string $papel
154
     * @param string $logoAlign
155
     */
156
    public function monta($orientacao = 'P', $papel = 'A4', $logoAlign = 'C')
157
    {
158
        $this->orientacao = $orientacao;
159
        $this->papel = $papel;
160
        $this->logoAlign = $logoAlign;
161
        $this->buildDACCE();
162
    }
163
164
    /**
165
     * Dados brutos do PDF
166
     * @return string
167
     */
168
    public function render()
169
    {
170
        return $this->printDACCE('', 'S');
171
    }
172
173
    /**
174
     * buildDACCE
175
     */
176
    private function buildDACCE()
177
    {
178
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
179
        if ($this->orientacao == 'P') {
180
            // margens do PDF
181
            $margSup = 2;
182
            $margEsq = 2;
183
            $margDir = 2;
184
            // posição inicial do relatorio
185
            $xInic = 1;
186
            $yInic = 1;
187
            if ($this->papel == 'A4') { // A4 210x297mm
188
                $maxW = 210;
189
                $maxH = 297;
190
            }
191
        } else {
192
            // margens do PDF
193
            $margSup = 3;
194
            $margEsq = 3;
195
            $margDir = 3;
196
            // posição inicial do relatorio
197
            $xInic = 5;
198
            $yInic = 5;
199
            if ($papel == 'A4') { // A4 210x297mm
0 ignored issues
show
Bug introduced by
The variable $papel 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...
200
                $maxH = 210;
201
                $maxW = 297;
202
            }
203
        } // orientação
204
        // largura imprimivel em mm
205
        $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...
206
        // comprimento imprimivel em mm
207
        $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...
208
        // estabelece contagem de paginas
209
        $this->pdf->aliasNbPages();
210
        // fixa as margens
211
        $this->pdf->setMargins($margEsq, $margSup, $margDir);
212
        $this->pdf->setDrawColor(0, 0, 0);
213
        $this->pdf->setFillColor(255, 255, 255);
214
        // inicia o documento
215
        $this->pdf->open();
216
        // adiciona a primeira página
217
        $this->pdf->addPage($this->orientacao, $this->papel);
218
        $this->pdf->setLineWidth(0.1);
219
        $this->pdf->setTextColor(0, 0, 0);
220
        // montagem da página
221
        $pag = 1;
222
        $x = $xInic;
223
        $y = $yInic;
224
        // coloca o cabeçalho
225
        $y = $this->header($x, $y, $pag);
226
        // coloca os dados da CCe
227
        $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\NFe\Dacce::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...
228
        // coloca os dados da CCe
229
        $y = $this->footer($x, $y + $this->hPrint - 20);
0 ignored issues
show
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...
Bug introduced by
Are you sure the assignment to $y is correct as $this->footer($x, $y + $this->hPrint - 20) (which targets NFePHP\DA\NFe\Dacce::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...
230
    }
231
232
    /**
233
     * Monta o cabeçalho
234
     * @param  number $x
235
     * @param  number $y
236
     * @param  number $pag
237
     * @return number
238
     */
239
    private function header($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...
240
    {
241
        $oldX = $x;
242
        $oldY = $y;
243
        $maxW = $this->wPrint;
244
        // ####################################################################################
245
        // coluna esquerda identificação do emitente
246
        $w = round($maxW * 0.41, 0); // 80;
247
        if ($this->orientacao == 'P') {
248
            $aFont = array(
249
                'font'  => $this->fontePadrao,
250
                'size'  => 6,
251
                'style' => 'I'
252
            );
253
        } else {
254
            $aFont = array(
255
                'font'  => $this->fontePadrao,
256
                'size'  => 8,
257
                'style' => 'B'
258
            );
259
        }
260
        $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...
261
        $h = 32;
262
        $oldY += $h;
263
        $this->pdf->textBox($x, $y, $w, $h);
264
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
265
        $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
266
        if (!empty($this->logomarca)) {
267
            $logoInfo = getimagesize($this->logomarca);
268
            $type = strtolower(explode('/', $logoInfo['mime'])[1]);
269
            if ($type == 'png') {
270
                $this->logomarca = $this->imagePNGtoJPG($this->logomarca);
271
                $type == 'jpg';
272
            }
273
            // largura da imagem em mm
274
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
275
            // altura da imagem em mm
276
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
277
            if ($this->logoAlign == 'L') {
278
                $nImgW = round($w / 3, 0);
279
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
280
                $xImg = $x + 1;
281
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
282
                // estabelecer posições do texto
283
                $x1 = round($xImg + $nImgW + 1, 0);
284
                $y1 = round($h / 3 + $y, 0);
285
                $tw = round(2 * $w / 3, 0);
286
            }
287
            if ($this->logoAlign == 'C') {
288
                $nImgH = round($h / 3, 0);
289
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
290
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
291
                $yImg = $y + 3;
292
                $x1 = $x;
293
                $y1 = round($yImg + $nImgH + 1, 0);
294
                $tw = $w;
295
            }
296
            if ($this->logoAlign == 'R') {
297
                $nImgW = round($w / 3, 0);
298
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
299
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
300
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
301
                $x1 = $x;
302
                $y1 = round($h / 3 + $y, 0);
303
                $tw = round(2 * $w / 3, 0);
304
            }
305
            $type = (substr($this->logomarca, 0, 7) === 'data://') ? 'jpg' : null;
306
            $this->pdf->Image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH, $type);
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...
307
        } else {
308
            $x1 = $x;
309
            $y1 = round($h / 3 + $y, 0);
310
            $tw = $w;
311
        }
312
        // Nome emitente
313
        $aFont = ['font' => $this->fontePadrao, 'size' => 12, 'style' => 'B'];
314
        $texto = $this->aEnd['razao'];
315
        $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...
316
        // endereço
317
        $y1 = $y1 + 6;
318
        $aFont = ['font' => $this->fontePadrao, 'size' => 8, 'style' => ''];
319
        $lgr = $this->aEnd['logradouro'];
320
        $nro = $this->aEnd['numero'];
321
        $cpl = $this->aEnd['complemento'];
322
        $bairro = $this->aEnd['bairro'];
323
        $CEP = $this->aEnd['CEP'];
324
        $CEP = $this->formatField($CEP, "#####-###");
325
        $mun = $this->aEnd['municipio'];
326
        $UF = $this->aEnd['UF'];
327
        $fone = $this->aEnd['telefone'];
328
        $email = $this->aEnd['email'];
329
        $foneLen = strlen($fone);
330
        if ($foneLen > 0) {
331
            $fone2 = substr($fone, 0, $foneLen - 4);
332
            $fone1 = substr($fone, 0, $foneLen - 8);
333
            $fone = '(' . $fone1 . ') ' . substr($fone2, - 4) . '-' . substr($fone, - 4);
334
        } else {
335
            $fone = '';
336
        }
337
        if ($email != '') {
338
            $email = 'Email: ' . $email;
339
        }
340
        $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - " . $CEP . "\n"
341
            . $mun . " - " . $UF . " " . $fone . "\n" . $email;
342
        $this->pdf->textBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
343
        // ##################################################
344
        $w2 = round($maxW - $w, 0);
345
        $x += $w;
346
        $this->pdf->textBox($x, $y, $w2, $h);
347
        $y1 = $y + $h;
348
        $aFont = ['font' => $this->fontePadrao, 'size' => 16, 'style' => 'B'];
349
        $this->pdf->textBox($x, $y + 2, $w2, 8, 'Representação Gráfica de CC-e', $aFont, 'T', 'C', 0, '');
350
        $aFont = array(
351
            'font'  => $this->fontePadrao,
352
            'size'  => 12,
353
            'style' => 'I'
354
        );
355
        $this->pdf->textBox($x, $y + 7, $w2, 8, '(Carta de Correção Eletrônica)', $aFont, 'T', 'C', 0, '');
356
        $texto = 'ID do Evento: ' . $this->id;
357
        $aFont = ['font' => $this->fontePadrao, 'size' => 10, 'style' => ''];
358
        $this->pdf->textBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
359
        $tsHora = $this->convertTime($this->dhEvento);
360
        $texto = 'Criado em : ' . date('d/m/Y   H:i:s', $tsHora);
361
        $this->pdf->textBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
362
        $tsHora = $this->convertTime($this->dhRegEvento);
363
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
364
        $this->pdf->textBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
365
        // ####################################################
366
        $x = $oldX;
367
        $this->pdf->textBox($x, $y1, $maxW, 40);
368
        $sY = $y1 + 40;
369
        $texto = 'De acordo com as determinações legais vigentes, vimos por meio desta comunicar-lhe' .
370
            ' que a Nota Fiscal, abaixo referenciada, contêm irregularidades que estão destacadas e' .
371
            ' suas respectivas correções, solicitamos que sejam aplicadas essas correções ao executar' .
372
            ' seus lançamentos fiscais.';
373
        $aFont = array(
374
            'font'  => $this->fontePadrao,
375
            'size'  => 10,
376
            'style' => ''
377
        );
378
        $this->pdf->textBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
379
        // ############################################
380
        $x = $oldX;
381
        $y = $y1;
382
        $texto = '';
383
        if ($this->CNPJDest != '') {
384
            $texto = 'CNPJ do Destinatário: ' . $this->formatField($this->CNPJDest, "##.###.###/####-##");
385
        }
386
        if ($this->CPFDest != '') {
387
            $texto = 'CPF do Destinatário: ' . $this->formatField($this->CPFDest, "###.###.###-##");
388
        }
389
        $aFont = array(
390
            'font'  => $this->fontePadrao,
391
            'size'  => 12,
392
            'style' => 'B'
393
        );
394
        $this->pdf->textBox($x + 2, $y + 13, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
395
        $numNF = substr($this->chNFe, 25, 9);
396
        $serie = substr($this->chNFe, 22, 3);
397
        $numNF = $this->formatField($numNF, "###.###.###");
398
        $texto = "Nota Fiscal: " . $numNF . '  -   Série: ' . $serie;
399
        $this->pdf->textBox($x + 2, $y + 19, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
400
        $bW = 87;
401
        $bH = 15;
402
        $x = 55;
403
        $y = $y1 + 13;
404
        $w = $maxW;
405
        $this->pdf->setFillColor(0, 0, 0);
406
        $this->pdf->code128($x + (($w - $bW) / 2), $y + 2, $this->chNFe, $bW, $bH);
407
        $this->pdf->setFillColor(255, 255, 255);
408
        $y1 = $y + 2 + $bH;
409
        $aFont = array(
410
            'font'  => $this->fontePadrao,
411
            'size'  => 10,
412
            'style' => ''
413
        );
414
        $texto = $this->formatField($this->chNFe, $this->formatoChave);
415
        $this->pdf->textBox($x, $y1, $w - 2, $h, $texto, $aFont, 'T', 'C', 0, '');
416
        $x = $oldX;
417
        $this->pdf->textBox($x, $sY, $maxW, 15);
418
        $texto = $this->xCondUso;
419
        $aFont = array(
420
            'font'  => $this->fontePadrao,
421
            'size'  => 8,
422
            'style' => 'I'
423
        );
424
        $this->pdf->textBox($x + 2, $sY + 2, $maxW - 2, 15, $texto, $aFont, 'T', 'L', 0, '', false);
425
        return $sY + 2;
426
    }
427
428
    /**
429
     * Monta o corpo da pagina
430
     * @param number $x
431
     * @param number $y
432
     */
433
    private function body($x, $y)
434
    {
435
        $maxW = $this->wPrint;
436
        $texto = 'CORREÇÕES A SEREM CONSIDERADAS';
437
        $aFont = array(
438
            'font'  => $this->fontePadrao,
439
            'size'  => 10,
440
            'style' => 'B'
441
        );
442
        $this->pdf->textBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false);
443
        $y += 5;
444
        $this->pdf->textBox($x, $y, $maxW, 190);
445
        $texto = str_replace(";", PHP_EOL, $this->xCorrecao);
446
        $aFont = array(
447
            'font'  => $this->fontePadrao,
448
            'size'  => 12,
449
            'style' => 'B'
450
        );
451
        $this->pdf->textBox($x + 2, $y + 2, $maxW - 2, 150, $texto, $aFont, 'T', 'L', 0, '', false);
452
        if ($this->tpAmb != 1) {
453
            $x = 10;
454
            if ($this->orientacao == 'P') {
455
                $y = round($this->hPrint * 2 / 3, 0);
456
            } else {
457
                $y = round($this->hPrint / 2, 0);
458
            }
459
            $h = 5;
460
            $w = $maxW - (2 * $x);
461
            $this->pdf->SetTextColor(90, 90, 90);
462
            $texto = "SEM VALOR FISCAL";
463
            $aFont = array(
464
                'font'  => $this->fontePadrao,
465
                'size'  => 48,
466
                'style' => 'B'
467
            );
468
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
469
            $aFont = array(
470
                'font'  => $this->fontePadrao,
471
                'size'  => 30,
472
                'style' => 'B'
473
            );
474
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
475
            $this->pdf->textBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
476
            $this->pdf->SetTextColor(0, 0, 0);
477
        }
478
    }
479
480
    /**
481
     * Monta o rodapé
482
     * @param number $x
483
     * @param number $y
484
     */
485
    protected function footer($x, $y)
486
    {
487
        $w = $this->wPrint;
488
        $texto = "Este documento é uma representação gráfica da CC-e e foi impresso apenas para sua"
489
            . " informação e não possue validade fiscal.\n A CC-e deve ser recebida e mantida em"
490
            . " arquivo eletrônico XML e pode ser consultada através dos Portais das SEFAZ.";
491
        $aFont = array(
492
            'font'  => $this->fontePadrao,
493
            'size'  => 10,
494
            'style' => 'I'
495
        );
496
        $this->pdf->textBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
497
        $y = $this->hPrint - 4;
498
        $texto = "Impresso em  " . date('d/m/Y   H:i:s');
499
        $w = $this->wPrint - 4;
500
        $aFont = array(
501
            'font'  => $this->fontePadrao,
502
            'size'  => 6,
503
            'style' => 'I'
504
        );
505
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
506
        $texto = "Dacce ver. " . $this->version . "  Powered by NFePHP (GNU/GPLv3 GNU/LGPLv3) © www.nfephp.org";
507
        $aFont = array(
508
            'font'  => $this->fontePadrao,
509
            'size'  => 6,
510
            'style' => 'I'
511
        );
512
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org');
513
    }
514
515
    /**
516
     * Gera a saida
517
     * @param  string $nome
518
     * @param  string $destino
519
     * @param  string $printer
520
     * @return mixed
521
     */
522
    public function printDocument($nome = '', $destino = 'I', $printer = '')
523
    {
524
        return $this->printDACCE($nome, $destino, $printer);
525
    }
526
527
    /**
528
     * Gera a saida
529
     * @param  string $nome
530
     * @param  string $destino
531
     * @param  string $printer
532
     * @return mixed
533
     */
534
    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...
535
    {
536
        if ($this->pdf == null) {
537
            $this->buildDACCE();
538
        }
539
        return $this->pdf->Output($nome, $destino);
540
    }
541
542
    private function imagePNGtoJPG($original)
543
    {
544
        $image = imagecreatefrompng($original);
545
        ob_start();
546
        imagejpeg($image, null, 100);
547
        imagedestroy($image);
548
        $stringdata = ob_get_contents(); // read from buffer
549
        ob_end_clean();
550
        return 'data://text/plain;base64,' . base64_encode($stringdata);
551
    }
552
}
553