Completed
Pull Request — master (#259)
by Roberto
02:22
created

Dacce::__construct()   F

Complexity

Conditions 13
Paths 313

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
nc 313
nop 11
dl 0
loc 86
ccs 0
cts 82
cp 0
crap 182
rs 3.8021
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 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
    public $chCTe;
25
    protected $logoAlign = 'C';
26
    protected $yDados = 0;
27
    protected $debugMode = 0;
28
    protected $aEnd = array();
29
    protected $pdf;
30
    protected $xml;
31
    protected $logomarca = '';
32
    protected $errMsg = '';
33
    protected $errStatus = false;
34
    protected $orientacao = 'P';
35
    protected $papel = 'A4';
36
    protected $destino = 'I';
37
    protected $pdfDir = '';
38
    protected $fontePadrao = 'Times';
39
    protected $version = '0.1.1';
40
    protected $wPrint;
41
    protected $hPrint;
42
    protected $wCanhoto;
43
    protected $formatoChave = "#### #### #### #### #### #### #### #### #### #### ####";
44
    protected $id;
45
    protected $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 $siteDesenvolvedor;
55
    protected $nomeDesenvolvedor;
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
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
        $nomeDesenvolvedor = 'Dacce ver. 0.1.1 Powered by NFePHP (GNU/GPLv3 GNU/LGPLv3) © www.nfephp.org',
87
        $siteDesenvolvedor = 'http://www.nfephp.org'
88
    ) {
89
        if (empty($docXML)) {
90
            throw new Exception("Um xml de evento deve ser passado");
91
        }
92
        if (is_numeric($mododebug)) {
93
            $this->debugMode = (int) $mododebug;
94
        }
95
        if ($this->debugMode === 1) {
96
            // ativar modo debug
97
            error_reporting(E_ALL);
98
            ini_set('display_errors', 'On');
99
        } elseif ($this->debugMode === 0) {
100
            // desativar modo debug
101
            error_reporting(0);
102
            ini_set('display_errors', 'Off');
103
        }
104
        if (is_array($aEnd)) {
105
            $this->aEnd = $aEnd;
106
        }
107
        $this->orientacao = $sOrientacao;
108
        $this->papel = $sPapel;
109
        $this->pdf = '';
110
        $this->xml = $docXML;
111
        $this->logomarca = $sPathLogo;
112
        $this->destino = $sDestino;
113
        $this->pdfDir = $sDirPDF;
114
        $this->siteDesenvolvedor = $siteDesenvolvedor;
115
        $this->nomeDesenvolvedor = $nomeDesenvolvedor;
116
        // verifica se foi passa a fonte a ser usada
117
        if (empty($fontePDF)) {
118
            $this->fontePadrao = 'Times';
119
        } else {
120
            $this->fontePadrao = $fontePDF;
121
        }
122
        // se for passado o xml
123
        if (!empty($this->xml)) {
124
            if (is_file($this->xml)) {
125
                $this->xml = file_get_contents($this->xml);
126
            }
127
            $this->dom = new Dom();
128
            $this->dom->loadXML($this->xml);
129
            if (empty($this->dom->getElementsByTagName("eventoCTe")->item(0))) {
130
                throw new Exception("Este xml não é um evento do CTe");
131
            }
132
            $this->procEventoCTe = $this->dom->getElementsByTagName("procEventoCTe")->item(0);
133
            $this->eventoCTe = $this->procEventoCTe->getElementsByTagName("eventoCTe")->item(0);
134
            $this->retEventoCTe = $this->procEventoCTe->getElementsByTagName("retEventoCTe")->item(0);
135
            $this->infEvento = $this->eventoCTe->getElementsByTagName("infEvento")->item(0);
136
            $this->retInfEvento = $this->retEventoCTe->getElementsByTagName("infEvento")->item(0);
137
            $tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
138
            if ($tpEvento != '110110') {
139
                $this->errMsg = 'Um evento de CC-e deve ser passado.';
140
                $this->errStatus = true;
141
                throw new Exception($this->errMsg);
142
            }
143
            $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
144
            $this->chCTe = $this->infEvento->getElementsByTagName("chCTe")->item(0)->nodeValue;
145
            $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
146
            $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
147
            $this->infCorrecao = $this->infEvento->getElementsByTagName("infCorrecao");
148
            $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0)->nodeValue;
149
            $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
150
            $this->cStat = $this->retInfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
151
            $this->xMotivo = $this->retInfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
152
            $this->CNPJDest = !empty($this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue) ?
0 ignored issues
show
Bug introduced by
The property CNPJDest does not exist. Did you maybe forget to declare it?

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

class MyClass { }

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

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

class MyClass {
    public $foo;
}

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

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

class MyClass { }

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

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

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
156
                $this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue
157
                : '';
158
            $this->dhRegEvento = $this->retInfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
159
            $this->nProt = $this->retInfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
160
        }
161
    }
162
163
    /**
164
     * monta
165
     *
166
     * @param string $orientacao
167
     * @param string $papel
168
     * @param string $logoAlign
169
     */
170
    public function monta($orientacao = 'P', $papel = 'A4', $logoAlign = 'C')
171
    {
172
        $this->orientacao = $orientacao;
173
        $this->papel = $papel;
174
        $this->logoAlign = $logoAlign;
175
        $this->buildDACCE();
176
    }
177
178
    /**
179
     * buildDACCE
180
     */
181
    private function buildDACCE()
182
    {
183
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
184
        if ($this->orientacao == 'P') {
185
            // margens do PDF
186
            $margSup = 2;
187
            $margEsq = 2;
188
            $margDir = 2;
189
            // posição inicial do relatorio
190
            $xInic = 1;
191
            $yInic = 1;
192
            if ($this->papel == 'A4') { // A4 210x297mm
193
                $maxW = 210;
194
                $maxH = 297;
195
            }
196
        } else {
197
            // margens do PDF
198
            $margSup = 3;
199
            $margEsq = 3;
200
            $margDir = 3;
201
            // posição inicial do relatorio
202
            $xInic = 5;
203
            $yInic = 5;
204
            if ($this->papel == 'A4') { // A4 210x297mm
205
                $maxH = 210;
206
                $maxW = 297;
207
            }
208
        } // orientação
209
        // largura imprimivel em mm
210
        $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...
211
        // comprimento imprimivel em mm
212
        $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...
213
        // estabelece contagem de paginas
214
        $this->pdf->aliasNbPages();
215
        // fixa as margens
216
        $this->pdf->setMargins($margEsq, $margSup, $margDir);
217
        $this->pdf->setDrawColor(0, 0, 0);
218
        $this->pdf->setFillColor(255, 255, 255);
219
        // inicia o documento
220
        $this->pdf->open();
221
        // adiciona a primeira página
222
        $this->pdf->addPage($this->orientacao, $this->papel);
223
        $this->pdf->setLineWidth(0.1);
224
        $this->pdf->setTextColor(0, 0, 0);
225
        // montagem da página
226
        $pag = 1;
227
        $x = $xInic;
228
        $y = $yInic;
229
        // coloca o cabeçalho
230
        $y = $this->header($x, $y, $pag);
231
        // coloca os dados da CCe
232
        $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\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...
233
        // coloca o rodapé
234
        $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\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...
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...
235
        //retorna o ID na CTe
236
    }
237
238
    /**
239
     * header
240
     *
241
     * @param  number $x
242
     * @param  number $y
243
     * @param  number $pag
244
     * @return number
245
     */
246
    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...
247
    {
248
        $oldX = $x;
249
        $oldY = $y;
250
        $maxW = $this->wPrint;
251
        // ####################################################################################
252
        // coluna esquerda identificação do emitente
253
        $w = round($maxW * 0.41, 0); // 80;
254
        if ($this->orientacao == 'P') {
255
            $aFont = array(
256
                'font'  => $this->fontePadrao,
257
                'size'  => 6,
258
                'style' => 'I'
259
            );
260
        } else {
261
            $aFont = array(
262
                'font'  => $this->fontePadrao,
263
                'size'  => 8,
264
                'style' => 'B'
265
            );
266
        }
267
        $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...
268
        $h = 32;
269
        $oldY += $h;
270
        $this->pdf->textBox($x, $y, $w, $h);
271
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
272
        $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
273
        if (is_file($this->logomarca)) {
274
            $logoInfo = getimagesize($this->logomarca);
275
            // largura da imagem em mm
276
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
277
            // altura da imagem em mm
278
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
279
            if ($this->logoAlign == 'L') {
280
                $nImgW = round($w / 3, 0);
281
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
282
                $xImg = $x + 1;
283
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
284
                // estabelecer posições do texto
285
                $x1 = round($xImg + $nImgW + 1, 0);
286
                $y1 = round($h / 3 + $y, 0);
287
                $tw = round(2 * $w / 3, 0);
288
            }
289
            if ($this->logoAlign == 'C') {
290
                $nImgH = round($h / 3, 0);
291
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
292
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
293
                $yImg = $y + 3;
294
                $x1 = $x;
295
                $y1 = round($yImg + $nImgH + 1, 0);
296
                $tw = $w;
297
            }
298
            if ($this->logoAlign == 'R') {
299
                $nImgW = round($w / 3, 0);
300
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
301
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
302
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
303
                $x1 = $x;
304
                $y1 = round($h / 3 + $y, 0);
305
                $tw = round(2 * $w / 3, 0);
306
            }
307
            $this->pdf->Image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH);
0 ignored issues
show
Bug introduced by
The variable $xImg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $yImg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $nImgW does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $nImgH does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
308
        } else {
309
            $x1 = $x;
310
            $y1 = round($h / 3 + $y, 0);
311
            $tw = $w;
312
        }
313
        // Nome emitente
314
        $aFont = ['font' => $this->fontePadrao, 'size' => 12, 'style' => 'B'];
315
        $texto = $this->aEnd['razao'];
316
        $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...
317
        // endereço
318
        $y1 = $y1 + 6;
319
        $aFont = ['font' => $this->fontePadrao, 'size' => 8, 'style' => ''];
320
        $lgr = $this->aEnd['logradouro'];
321
        $nro = $this->aEnd['numero'];
322
        $cpl = $this->aEnd['complemento'];
323
        $bairro = $this->aEnd['bairro'];
324
        $CEP = $this->aEnd['CEP'];
325
        // $CEP = $this->formatField($CEP, "#####-###");
326
        $mun = $this->aEnd['municipio'];
327
        $UF = $this->aEnd['UF'];
328
        $fone = $this->aEnd['telefone'];
329
        $email = $this->aEnd['email'];
330
        if ($email != '') {
331
            $email = 'Email: ' . $email;
332
        }
333
        $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - " . $CEP . "\n"
334
            . $mun . " - " . $UF . " - " . $fone . "\n" . $email;
335
        $this->pdf->textBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
336
        // ##################################################
337
        $w2 = round($maxW - $w, 0);
338
        $x += $w;
339
        $this->pdf->textBox($x, $y, $w2, $h);
340
        $y1 = $y + $h;
341
        $aFont = ['font' => $this->fontePadrao, 'size' => 16, 'style' => 'B'];
342
        $this->pdf->textBox($x, $y + 2, $w2, 8, 'Representação Gráfica de CC-e', $aFont, 'T', 'C', 0, '');
343
        $aFont = array(
344
            'font'  => $this->fontePadrao,
345
            'size'  => 12,
346
            'style' => 'I'
347
        );
348
        $this->pdf->textBox($x, $y + 7, $w2, 8, '(Carta de Correção Eletrônica)', $aFont, 'T', 'C', 0, '');
349
        $texto = 'ID do Evento: ' . $this->id;
350
        $aFont = ['font' => $this->fontePadrao, 'size' => 10, 'style' => ''];
351
        $this->pdf->textBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
352
        $tsHora = $this->convertTime($this->dhEvento);
353
        $texto = 'Criado em : ' . date('d/m/Y H:i:s', $tsHora);
354
        $this->pdf->textBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
355
        $tsHora = $this->convertTime($this->dhRegEvento);
356
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
357
        $this->pdf->textBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
358
        // ####################################################
359
        $x = $oldX;
360
        $this->pdf->textBox($x, $y1, $maxW, 40);
361
        $sY = $y1 + 40;
362
        $texto = 'De acordo com as determinações legais vigentes, vimos por meio desta comunicar-lhe' .
363
            ' que o Conhecimento, abaixo referenciada, contêm irregularidades que estão destacadas e' .
364
            ' suas respectivas correções, solicitamos que sejam aplicadas essas correções ao executar' .
365
            ' seus lançamentos fiscais.';
366
        $aFont = array(
367
            'font'  => $this->fontePadrao,
368
            'size'  => 10,
369
            'style' => ''
370
        );
371
        $this->pdf->textBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
372
        // ############################################
373
        $x = $oldX;
374
        $y = $y1;
375
        if ($this->CNPJDest != '') {
376
            $texto = 'CNPJ do Destinatário: ' . $this->formatField($this->CNPJDest, "##.###.###/####-##");
0 ignored issues
show
Unused Code introduced by
$texto is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
478
        } //fim da soma das areas de itens usadas
479
        //$texto = str_replace(";", PHP_EOL, $this->xCorrecao);
480
        $aFont = array(
0 ignored issues
show
Unused Code introduced by
$aFont is not used, you could remove the assignment.

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

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

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

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

Loading history...
481
            'font'  => $this->fontePadrao,
482
            'size'  => 12,
483
            'style' => 'B'
484
        );
485
        //$this->pdf->textBox($x + 2, $y + 2, $maxW - 2, 150, $texto, $aFont, 'T', 'L', 0, '', false);
486
        $maxW = $this->wPrint;
487
        if ($this->tpAmb != 1) {
488
            $x = 10;
489
            if ($this->orientacao == 'P') {
490
                $y = round($this->hPrint * 2 / 3, 0);
491
            } else {
492
                $y = round($this->hPrint * 2 / 3, 0);
493
            }
494
            $h = 5;
495
            $w = $maxW - (2 * $x);
496
            $this->pdf->setTextColor(90, 90, 90);
497
            $texto = "SEM VALOR FISCAL";
498
            $aFont = array(
499
                'font'  => $this->fontePadrao,
500
                'size'  => 48,
501
                'style' => 'B'
502
            );
503
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
504
            $aFont = array(
505
                'font'  => $this->fontePadrao,
506
                'size'  => 30,
507
                'style' => 'B'
508
            );
509
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
510
            $this->pdf->textBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
511
            $this->pdf->setTextColor(0, 0, 0);
512
        }
513
    }
514
515
    /**
516
     * footer
517
     *
518
     * @param number $x
519
     * @param number $y
520
     */
521
    protected function footer($x, $y)
522
    {
523
        $w = $this->wPrint;
524
        $texto = "Este documento é uma representação gráfica da CC-e e foi impresso apenas para sua"
525
            . " informação e não possue validade fiscal.\n A CC-e deve ser recebida e mantida em"
526
            . " arquivo eletrônico XML e pode ser consultada através dos Portais das SEFAZ.";
527
        $aFont = array(
528
            'font'  => $this->fontePadrao,
529
            'size'  => 10,
530
            'style' => 'I'
531
        );
532
        $this->pdf->textBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
533
        $y = $this->hPrint - 4;
534
        $texto = "Impresso em  " . date('d/m/Y   H:i:s');
535
        $w = $this->wPrint - 4;
536
        $aFont = array(
537
            'font'  => $this->fontePadrao,
538
            'size'  => 6,
539
            'style' => 'I'
540
        );
541
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
542
543
        $texto = "Dacce ver. " . $this->version . " - Desenvolvido por "
544
            . $this->nomeDesenvolvedor . " - " . $this->siteDesenvolvedor;
545
        $aFont = array(
546
            'font'  => $this->fontePadrao,
547
            'size'  => 6,
548
            'style' => 'I'
549
        );
550
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org');
551
    }
552
553
    /**
554
     * printDocument
555
     *
556
     * @param  string $nome
557
     * @param  string $destino
558
     * @param  string $printer
559
     * @return mixed
560
     */
561
    public function printDocument($nome = '', $destino = 'I', $printer = '')
562
    {
563
        return $this->printDACCE($nome, $destino, $printer);
564
    }
565
566
    /**
567
     * printDACCE
568
     *
569
     * @param  string $nome
570
     * @param  string $destino
571
     * @param  string $printer
572
     * @return mixed
573
     */
574
    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...
575
    {
576
        if ($this->pdf == null) {
577
            $this->buildDACCE();
578
        }
579
        return $this->pdf->output($nome, $destino);
580
    }
581
582
    /**
583
     * Dados brutos do PDF
584
     * @return string
585
     */
586
    public function render()
587
    {
588
        if ($this->pdf == null) {
589
            $this->buildDACCE();
590
        }
591
        return $this->pdf->getPdf();
592
    }
593
}
594