Completed
Push — master ( 82c323...989f08 )
by Roberto
05:05 queued 02:26
created

Dacce::buildDACCE()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 56
ccs 0
cts 41
cp 0
crap 20
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace NFePHP\DA\CTe;
4
5
/**
6
 * Esta classe gera a carta de correção em PDF
7
 * NOTA: Esse documento NÃO É NORMALIZADO, nem requerido pela SEFAZ
8
 *
9
 * @category  Library
10
 * @package   nfephp-org/sped-da
11
 * @name      Dacce.php
12
 * @copyright 2009-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
25
    public $chCTe;
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 $infCorrecao;
49
    protected $xCondUso;
50
    protected $dhEvento;
51
    protected $cStat;
52
    protected $xMotivo;
53
    protected $dhRegEvento;
54
    protected $nProt;
55
    protected $siteDesenvolvedor;
56
    protected $nomeDesenvolvedor;
57
    private $dom;
58
    private $procEventoCTe;
59
    private $eventoCTe;
60
    private $infEvento;
61
    private $retEventoCTe;
62
    private $retInfEvento;
63
64
    /**
65
     * __construct
66
     *
67
     * @param string $docXML      Arquivo XML (diretório ou string)
68
     * @param string $sOrientacao (Opcional) Orientação da impressão P-retrato L-Paisagem
69
     * @param string $sPapel      Tamanho do papel (Ex. A4)
70
     * @param string $sPathLogo   Caminho para o arquivo do logo
71
     * @param string $sDestino    Destino do PDF I-browser D-download S-string F-salva
72
     * @param array  $aEnd        array com o endereço do emitente
73
     * @param string $sDirPDF     Caminho para o diretorio de armazenamento dos arquivos PDF
74
     * @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...
75
     * @param number $mododebug   0-Não 1-Sim e 2-nada (2 default)
76
     */
77
    public function __construct(
78
        $docXML = '',
79
        $sOrientacao = '',
80
        $sPapel = '',
81
        $sPathLogo = '',
82
        $sDestino = 'I',
83
        $aEnd = '',
84
        $sDirPDF = '',
85
        $fontePDF = '',
86
        $mododebug = 0,
87
        $nomeDesenvolvedor = 'Dacce ver. 0.1.1 Powered by NFePHP (GNU/GPLv3 GNU/LGPLv3) © www.nfephp.org',
88
        $siteDesenvolvedor = 'http://www.nfephp.org'
89
    ) {
90
        if (is_numeric($mododebug)) {
91
            $this->debugMode = (int) $mododebug;
92
        }
93
        if ($this->debugMode === 1) {
94
            // ativar modo debug
95
            error_reporting(E_ALL);
96
            ini_set('display_errors', 'On');
97
        } elseif ($this->debugMode === 0) {
98
            // desativar modo debug
99
            error_reporting(0);
100
            ini_set('display_errors', 'Off');
101
        }
102
        if (is_array($aEnd)) {
103
            $this->aEnd = $aEnd;
104
        }
105
        $this->orientacao = $sOrientacao;
106
        $this->papel = $sPapel;
107
        $this->pdf = '';
108
        $this->xml = $docXML;
109
        $this->logomarca = $sPathLogo;
110
        $this->destino = $sDestino;
111
        $this->pdfDir = $sDirPDF;
112
        $this->siteDesenvolvedor = $siteDesenvolvedor;
113
        $this->nomeDesenvolvedor = $nomeDesenvolvedor;
114
        // verifica se foi passa a fonte a ser usada
115
        if (empty($fontePDF)) {
116
            $this->fontePadrao = 'Times';
117
        } else {
118
            $this->fontePadrao = $fontePDF;
119
        }
120
        // se for passado o xml
121
        if (!empty($this->xml)) {
122
            if (is_file($this->xml)) {
123
                $this->xml = file_get_contents($this->xml);
124
            }
125
            $this->dom = new Dom();
126
            $this->dom->loadXML($this->xml);
127
            $this->procEventoCTe = $this->dom->getElementsByTagName("procEventoCTe")->item(0);
128
            $this->eventoCTe = $this->procEventoCTe->getElementsByTagName("eventoCTe")->item(0);
129
            $this->retEventoCTe = $this->procEventoCTe->getElementsByTagName("retEventoCTe")->item(0);
130
            $this->infEvento = $this->eventoCTe->getElementsByTagName("infEvento")->item(0);
131
            $this->retInfEvento = $this->retEventoCTe->getElementsByTagName("infEvento")->item(0);
132
            $tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
133
            if ($tpEvento != '110110') {
134
                $this->errMsg = 'Um evento de CC-e deve ser passado.';
135
                $this->errStatus = true;
136
                throw new Exception($this->errMsg);
137
            }
138
            $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
139
            $this->chCTe = $this->infEvento->getElementsByTagName("chCTe")->item(0)->nodeValue;
140
            $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
141
            $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
142
            $this->infCorrecao = $this->infEvento->getElementsByTagName("infCorrecao");
143
            $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0)->nodeValue;
144
            $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
145
            $this->cStat = $this->retInfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
146
            $this->xMotivo = $this->retInfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
147
            $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...
148
                $this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue
149
                : '';
150
            $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...
151
                $this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue
152
                : '';
153
            $this->dhRegEvento = $this->retInfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
154
            $this->nProt = $this->retInfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
155
        }
156
    }
157
158
    /**
159
     * monta
160
     *
161
     * @param string $orientacao
162
     * @param string $papel
163
     * @param string $logoAlign
164
     */
165
    public function monta($orientacao = 'P', $papel = 'A4', $logoAlign = 'C')
166
    {
167
        $this->orientacao = $orientacao;
168
        $this->papel = $papel;
169
        $this->logoAlign = $logoAlign;
170
        $this->buildDACCE();
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 ($this->papel == 'A4') { // A4 210x297mm
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\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...
228
        // coloca o rodapé
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\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...
230
        //retorna o ID na CTe
231
    }
232
233
    /**
234
     * header
235
     *
236
     * @param  number $x
237
     * @param  number $y
238
     * @param  number $pag
239
     * @return number
240
     */
241
    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...
242
    {
243
        $oldX = $x;
244
        $oldY = $y;
245
        $maxW = $this->wPrint;
246
        // ####################################################################################
247
        // coluna esquerda identificação do emitente
248
        $w = round($maxW * 0.41, 0); // 80;
249
        if ($this->orientacao == 'P') {
250
            $aFont = array(
251
                'font'  => $this->fontePadrao,
252
                'size'  => 6,
253
                'style' => 'I'
254
            );
255
        } else {
256
            $aFont = array(
257
                'font'  => $this->fontePadrao,
258
                'size'  => 8,
259
                'style' => 'B'
260
            );
261
        }
262
        $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...
263
        $h = 32;
264
        $oldY += $h;
265
        $this->pdf->textBox($x, $y, $w, $h);
266
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
267
        $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
268
        if (is_file($this->logomarca)) {
269
            $logoInfo = getimagesize($this->logomarca);
270
            // largura da imagem em mm
271
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
272
            // altura da imagem em mm
273
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
274
            if ($this->logoAlign == 'L') {
275
                $nImgW = round($w / 3, 0);
276
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
277
                $xImg = $x + 1;
278
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
279
                // estabelecer posições do texto
280
                $x1 = round($xImg + $nImgW + 1, 0);
281
                $y1 = round($h / 3 + $y, 0);
282
                $tw = round(2 * $w / 3, 0);
283
            }
284
            if ($this->logoAlign == 'C') {
285
                $nImgH = round($h / 3, 0);
286
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
287
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
288
                $yImg = $y + 3;
289
                $x1 = $x;
290
                $y1 = round($yImg + $nImgH + 1, 0);
291
                $tw = $w;
292
            }
293
            if ($this->logoAlign == 'R') {
294
                $nImgW = round($w / 3, 0);
295
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
296
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
297
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
298
                $x1 = $x;
299
                $y1 = round($h / 3 + $y, 0);
300
                $tw = round(2 * $w / 3, 0);
301
            }
302
            $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...
303
        } else {
304
            $x1 = $x;
305
            $y1 = round($h / 3 + $y, 0);
306
            $tw = $w;
307
        }
308
        // Nome emitente
309
        $aFont = ['font' => $this->fontePadrao, 'size' => 12, 'style' => 'B'];
310
        $texto = $this->aEnd['razao'];
311
        $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...
312
        // endereço
313
        $y1 = $y1 + 6;
314
        $aFont = ['font' => $this->fontePadrao, 'size' => 8, 'style' => ''];
315
        $lgr = $this->aEnd['logradouro'];
316
        $nro = $this->aEnd['numero'];
317
        $cpl = $this->aEnd['complemento'];
318
        $bairro = $this->aEnd['bairro'];
319
        $CEP = $this->aEnd['CEP'];
320
        // $CEP = $this->formatField($CEP, "#####-###");
321
        $mun = $this->aEnd['municipio'];
322
        $UF = $this->aEnd['UF'];
323
        $fone = $this->aEnd['telefone'];
324
        $email = $this->aEnd['email'];
325
        if ($email != '') {
326
            $email = 'Email: ' . $email;
327
        }
328
        $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - " . $CEP . "\n"
329
            . $mun . " - " . $UF . " - " . $fone . "\n" . $email;
330
        $this->pdf->textBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
331
        // ##################################################
332
        $w2 = round($maxW - $w, 0);
333
        $x += $w;
334
        $this->pdf->textBox($x, $y, $w2, $h);
335
        $y1 = $y + $h;
336
        $aFont = ['font' => $this->fontePadrao, 'size' => 16, 'style' => 'B'];
337
        $this->pdf->textBox($x, $y + 2, $w2, 8, 'Representação Gráfica de CC-e', $aFont, 'T', 'C', 0, '');
338
        $aFont = array(
339
            'font'  => $this->fontePadrao,
340
            'size'  => 12,
341
            'style' => 'I'
342
        );
343
        $this->pdf->textBox($x, $y + 7, $w2, 8, '(Carta de Correção Eletrônica)', $aFont, 'T', 'C', 0, '');
344
        $texto = 'ID do Evento: ' . $this->id;
345
        $aFont = ['font' => $this->fontePadrao, 'size' => 10, 'style' => ''];
346
        $this->pdf->textBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
347
        $tsHora = $this->convertTime($this->dhEvento);
348
        $texto = 'Criado em : ' . date('d/m/Y   H:i:s', $tsHora);
349
        $this->pdf->textBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
350
        $tsHora = $this->convertTime($this->dhRegEvento);
351
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
352
        $this->pdf->textBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
353
        // ####################################################
354
        $x = $oldX;
355
        $this->pdf->textBox($x, $y1, $maxW, 40);
356
        $sY = $y1 + 40;
357
        $texto = 'De acordo com as determinações legais vigentes, vimos por meio desta comunicar-lhe' .
358
            ' que o Conhecimento, abaixo referenciada, contêm irregularidades que estão destacadas e' .
359
            ' suas respectivas correções, solicitamos que sejam aplicadas essas correções ao executar' .
360
            ' seus lançamentos fiscais.';
361
        $aFont = array(
362
            'font'  => $this->fontePadrao,
363
            'size'  => 10,
364
            'style' => ''
365
        );
366
        $this->pdf->textBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
367
        // ############################################
368
        $x = $oldX;
369
        $y = $y1;
370
        if ($this->CNPJDest != '') {
371
            $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...
372
        }
373
        if ($this->CPFDest != '') {
374
            $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...
375
        }
376
        $aFont = array(
377
            'font'  => $this->fontePadrao,
378
            'size'  => 12,
379
            'style' => 'B'
380
        );
381
382
        $numNF = substr($this->chCTe, 25, 9);
383
        $serie = substr($this->chCTe, 22, 3);
384
        $numNF = $this->formatField($numNF, "###.###.###");
385
        $texto = "Conhecimento: " . $numNF . '  -   Série: ' . $serie;
386
        $this->pdf->textBox($x + 2, $y + 19, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
387
        $bW = 87;
388
        $bH = 15;
389
        $x = 55;
390
        $y = $y1 + 13;
391
        $w = $maxW;
392
        $this->pdf->setFillColor(0, 0, 0);
393
        $this->pdf->code128($x + (($w - $bW) / 2), $y + 2, $this->chCTe, $bW, $bH);
394
        $this->pdf->setFillColor(255, 255, 255);
395
        $y1 = $y + 2 + $bH;
396
        $aFont = array(
397
            'font'  => $this->fontePadrao,
398
            'size'  => 10,
399
            'style' => ''
400
        );
401
        $texto = $this->formatField($this->chCTe, $this->formatoChave);
402
        $this->pdf->textBox($x, $y1, $w - 2, $h, $texto, $aFont, 'T', 'C', 0, '');
403
        $x = $oldX;
404
        $this->pdf->textBox($x, $sY, $maxW, 15);
405
        $texto = $this->xCondUso;
406
        $aFont = array(
407
            'font'  => $this->fontePadrao,
408
            'size'  => 8,
409
            'style' => 'I'
410
        );
411
        $this->pdf->textBox($x + 2, $sY + 2, $maxW - 2, 15, $texto, $aFont, 'T', 'L', 0, '', false);
412
        return $sY + 2;
413
    }
414
415
    /**
416
     * body
417
     *
418
     * @param number $x
419
     * @param number $y
420
     */
421
    private function body($x, $y)
422
    {
423
        if ($this->orientacao == 'P') {
424
            $maxH = 190;
425
        } else {
426
            $maxH = 95;
427
        }
428
        $maxW = $this->wPrint;
429
        $texto = 'CORREÇÕES A SEREM CONSIDERADAS';
430
        $aFont = array(
431
            'font'  => $this->fontePadrao,
432
            'size'  => 10,
433
            'style' => 'B'
434
        );
435
        $this->pdf->textBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false);
436
        $y += 5;
437
        $this->pdf->textBox($x, $y, $maxW, $maxH);
438
439
440
        $aFont = array(
441
            'font'  => $this->fontePadrao,
442
            'size'  => 9,
443
            'style' => 'B'
444
        );
445
        $this->pdf->textBox($x, $y, $maxW = ($maxW / 5), 5, "Grupo", $aFont, 'T', 'C', 0, '', false);
446
        $this->pdf->textBox($x = $maxW, $y, $maxW, 5, "Campo", $aFont, 'T', 'C', 0, '', false);
447
        $this->pdf->textBox($x = ($maxW * 2), $y, $maxW, 5, "Número", $aFont, 'T', 'C', 0, '', false);
448
        $this->pdf->textBox($x = ($maxW * 3), $y, ($this->wPrint - $x), 5, "Valor", $aFont, 'T', 'C', 0, '', false);
449
450
        $aFont = array(
451
            'font'  => $this->fontePadrao,
452
            'size'  => 9,
453
            'style' => ''
454
        );
455
        //Correções
456
        $i = 0;
457
        $numlinhas = 1;
458
        while ($i < $this->infCorrecao->length) {
459
            $x = 0;
460
            $y = $numlinhas == 1 ? ($y + 5) : ($y + (5 * $numlinhas));
461
            $maxW = $this->wPrint;
462
            $grupo = $this->infCorrecao->item($i)->getElementsByTagName('grupoAlterado')->item(0)->nodeValue;
463
            $campo = $this->infCorrecao->item($i)->getElementsByTagName('campoAlterado')->item(0)->nodeValue;
464
            $numero = $this->infCorrecao->item($i)->getElementsByTagName('nroItemAlterado')->item(0)->nodeValue;
465
            $valor = $this->infCorrecao->item($i)->getElementsByTagName('valorAlterado')->item(0)->nodeValue;
466
467
            $i++;
468
            $this->pdf->textBox($x, $y, $maxW = ($maxW / 5), 5, $grupo, $aFont, 'T', 'C', 0, '', false);
469
            $this->pdf->textBox($x = $maxW, $y, $maxW, 5, $campo, $aFont, 'T', 'C', 0, '', false);
470
            $this->pdf->textBox($x = ($maxW * 2), $y, $maxW, 5, $numero, $aFont, 'T', 'C', 0, '', false);
471
            $this->pdf->textBox($x = ($maxW * 3), $y, ($this->wPrint - $x), 5, $valor, $aFont, 'T', 'C', 0, '', false);
472
            $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...
473
        } //fim da soma das areas de itens usadas
474
        //$texto = str_replace(";", PHP_EOL, $this->xCorrecao);
475
        $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...
476
            'font'  => $this->fontePadrao,
477
            'size'  => 12,
478
            'style' => 'B'
479
        );
480
        //$this->pdf->textBox($x + 2, $y + 2, $maxW - 2, 150, $texto, $aFont, 'T', 'L', 0, '', false);
481
        $maxW = $this->wPrint;
482
        if ($this->tpAmb != 1) {
483
            $x = 10;
484
            if ($this->orientacao == 'P') {
485
                $y = round($this->hPrint * 2 / 3, 0);
486
            } else {
487
                $y = round($this->hPrint * 2 / 3, 0);
488
            }
489
            $h = 5;
490
            $w = $maxW - (2 * $x);
491
            $this->pdf->setTextColor(90, 90, 90);
492
            $texto = "SEM VALOR FISCAL";
493
            $aFont = array(
494
                'font'  => $this->fontePadrao,
495
                'size'  => 48,
496
                'style' => 'B'
497
            );
498
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
499
            $aFont = array(
500
                'font'  => $this->fontePadrao,
501
                'size'  => 30,
502
                'style' => 'B'
503
            );
504
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
505
            $this->pdf->textBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
506
            $this->pdf->setTextColor(0, 0, 0);
507
        }
508
    }
509
510
    /**
511
     * footer
512
     *
513
     * @param number $x
514
     * @param number $y
515
     */
516
    protected function footer($x, $y)
517
    {
518
        $w = $this->wPrint;
519
        $texto = "Este documento é uma representação gráfica da CC-e e foi impresso apenas para sua"
520
            . " informação e não possue validade fiscal.\n A CC-e deve ser recebida e mantida em"
521
            . " arquivo eletrônico XML e pode ser consultada através dos Portais das SEFAZ.";
522
        $aFont = array(
523
            'font'  => $this->fontePadrao,
524
            'size'  => 10,
525
            'style' => 'I'
526
        );
527
        $this->pdf->textBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
528
        $y = $this->hPrint - 4;
529
        $texto = "Impresso em  " . date('d/m/Y   H:i:s');
530
        $w = $this->wPrint - 4;
531
        $aFont = array(
532
            'font'  => $this->fontePadrao,
533
            'size'  => 6,
534
            'style' => 'I'
535
        );
536
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
537
538
        $texto = "Dacce ver. " . $this->version . " - Desenvolvido por "
539
            . $this->nomeDesenvolvedor . " - " . $this->siteDesenvolvedor;
540
        $aFont = array(
541
            'font'  => $this->fontePadrao,
542
            'size'  => 6,
543
            'style' => 'I'
544
        );
545
        $this->pdf->textBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org');
546
    }
547
548
    /**
549
     * printDocument
550
     *
551
     * @param  string $nome
552
     * @param  string $destino
553
     * @param  string $printer
554
     * @return mixed
555
     */
556
    public function printDocument($nome = '', $destino = 'I', $printer = '')
557
    {
558
        return $this->printDACCE($nome, $destino, $printer);
559
    }
560
561
    /**
562
     * printDACCE
563
     *
564
     * @param  string $nome
565
     * @param  string $destino
566
     * @param  string $printer
567
     * @return mixed
568
     */
569
    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...
570
    {
571
        if ($this->pdf == null) {
572
            $this->buildDACCE();
573
        }
574
        return $this->pdf->output($nome, $destino);
575
    }
576
577
    /**
578
     * Dados brutos do PDF
579
     * @return string
580
     */
581
    public function render()
582
    {
583
        if ($this->pdf == null) {
584
            $this->buildDACCE();
585
        }
586
        return $this->pdf->getPdf();
587
    }
588
}
589