Test Setup Failed
Push — master ( 588a03...acb707 )
by Roberto
06:48 queued 10s
created

Common::pSimpleGetValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 5
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\Legacy;
4
5
class Common
6
{
7
8
    /**
9
     * Extrai o valor do node DOM
10
     * @param  object $theObj Instancia de DOMDocument ou DOMElement
11
     * @param  string $keyName identificador da TAG do xml
12
     * @param  string $extraTextBefore prefixo do retorno
13
     * @param  string extraTextAfter sufixo do retorno
14
     * @param  number itemNum numero do item a ser retornado
15
     * @return string
16
     */
17
    protected function getTagValue($theObj, $keyName, $extraTextBefore = '', $extraTextAfter = '', $itemNum = 0)
18
    {
19
        if (empty($theObj)) {
20
            return '';
21
        }
22
        $vct = $theObj->getElementsByTagName($keyName)->item($itemNum);
23
        if (isset($vct)) {
24
            $value = trim($vct->nodeValue);
25
            if (strpos($value, '&') !== false) {
26
                //existe um & na string, então deve ser uma entidade
27
                $value = html_entity_decode($value);
28
            }
29
            return $extraTextBefore . $value . $extraTextAfter;
30
        }
31
        return '';
32
    }
33
34
    /**
35
     * Recupera e reformata a data do padrão da NFe para dd/mm/aaaa
36
     * @author Marcos Diez
37
     * @param  DOM    $theObj
38
     * @param  string $keyName   identificador da TAG do xml
39
     * @param  string $extraText prefixo do retorno
40
     * @return string
41
     */
42
    protected function pSimpleGetDate($theObj, $keyName, $extraText = '')
43
    {
44
        if (!isset($theObj) || !is_object($theObj)) {
45
            return '';
46
        }
47
        $vct = $theObj->getElementsByTagName($keyName)->item(0);
48
        if (isset($vct)) {
49
            $theDate = explode("-", $vct->nodeValue);
50
            return $extraText . $theDate[2] . "/" . $theDate[1] . "/" . $theDate[0];
51
        }
52
        return '';
53
    }
54
55
    /**
56
     * camcula digito de controle modulo 11
57
     * @param  string $numero
58
     * @return integer modulo11 do numero passado
59
     */
60
    protected function pModulo11($numero = '')
61
    {
62
        if ($numero == '') {
63
            return '';
64
        }
65
        $numero = (string) $numero;
66
        $tamanho = strlen($numero);
67
        $soma = 0;
68
        $mult = 2;
69
        for ($i = $tamanho - 1; $i >= 0; $i--) {
70
            $digito = (int) $numero[$i];
71
            $r = $digito * $mult;
72
            $soma += $r;
73
            $mult++;
74
            if ($mult == 10) {
75
                $mult = 2;
76
            }
77
        }
78
        $resto = ($soma * 10) % 11;
79
        return ($resto == 10 || $resto == 0) ? 1 : $resto;
80
    }
81
82 1
    /**
83
     * Converte datas no formato YMD (ex. 2009-11-02) para o formato brasileiro 02/11/2009)
84 1
     * @param  string $data Parâmetro extraido da NFe
85
     * @return string Formatada para apresentação da data no padrão brasileiro
86
     */
87 1
    protected function pYmd2dmy($data = '')
88 1
    {
89 1
        if ($data == '') {
90
            return '';
91 1
        }
92 1
        $needle = "/";
93
        if (strstr($data, "-")) {
94
            $needle = "-";
95
        }
96
        $dt = explode($needle, $data);
97
        return "$dt[2]/$dt[1]/$dt[0]";
98
    }
99
100
    /**
101 1
     * pConvertTime
102
     * Converte a informação de data e tempo contida na NFe
103 1
     * @param  string $DH Informação de data e tempo extraida da NFe
104 1
     * @return timestamp UNIX Para uso com a funçao date do php
105
     */
106 1
    protected function pConvertTime($DH = '')
107 1
    {
108 1
        if ($DH == '') {
109 1
            return '';
110 1
        }
111 1
        $DH = str_replace('+', '-', $DH);
112 1
        $aDH = explode('T', $DH);
113
        $adDH = explode('-', $aDH[0]);
114
        if (count($aDH) > 1) {
115
            $inter = explode('-', $aDH[1]);
116 1
            $atDH = explode(':', $inter[0]);
117
            $timestampDH = mktime($atDH[0], $atDH[1], $atDH[2], $adDH[1], $adDH[2], $adDH[0]);
118
        } else {
119
            $timestampDH = mktime($month = $adDH[1], $day = $adDH[2], $year = $adDH[0]);
120
        }
121
        return $timestampDH;
122
    }
123
124
    /**
125
     * Função de formatação de strings onde o cerquilha # é um coringa
126 1
     * que será substituido por digitos contidos em campo.
127
     * @param  string $campo   String a ser formatada
128 1
     * @param  string $mascara Regra de formatção da string (ex. ##.###.###/####-##)
129
     * @return string Retorna o campo formatado
130
     */
131
    protected function pFormat($campo = '', $mascara = '')
132 1
    {
133
        if ($campo == '' || $mascara == '') {
134 1
            return $campo;
135 1
        }
136 1
        //remove qualquer formatação que ainda exista
137 1
        $sLimpo = preg_replace("(/[' '-./ t]/)", '', $campo);
138
        // pega o tamanho da string e da mascara
139 1
        $tCampo = strlen($sLimpo);
140
        $tMask = strlen($mascara);
141
        if ($tCampo > $tMask) {
142 1
            $tMaior = $tCampo;
143 1
        } else {
144 1
            $tMaior = $tMask;
145 1
        }
146 1
        //contar o numero de cerquilhas da mascara
147 1
        $aMask = str_split($mascara);
148
        $z = 0;
149
        $flag = false;
150 1
        foreach ($aMask as $letra) {
151
            if ($letra == '#') {
152 1
                $z++;
153
            }
154
        }
155 1
        if ($z > $tCampo) {
156 1
            //o campo é menor que esperado
157
            $flag = true;
158 1
        }
159
        //cria uma variável grande o suficiente para conter os dados
160 1
        $sRetorno = '';
161
        $sRetorno = str_pad($sRetorno, $tCampo + $tMask, " ", STR_PAD_LEFT);
162 1
        //pega o tamanho da string de retorno
163 1
        $tRetorno = strlen($sRetorno);
164 1
        //se houve entrada de dados
165 1
        if ($sLimpo != '' && $mascara != '') {
166 1
            //inicia com a posição do ultimo digito da mascara
167
            $x = $tMask;
168
            $y = $tCampo;
169
            $cI = 0;
170 1
            for ($i = $tMaior - 1; $i >= 0; $i--) {
171 1
                if ($cI < $z) {
172
                    // e o digito da mascara é # trocar pelo digito do campo
173
                    // se o inicio da string da mascara for atingido antes de terminar
174
                    // o campo considerar #
175
                    if ($x > 0) {
176
                        $digMask = $mascara[--$x];
177 1
                    } else {
178 1
                        $digMask = '#';
179 1
                    }
180 1
                    //se o fim do campo for atingido antes do fim da mascara
181 1
                    //verificar se é ( se não for não use
182
                    if ($digMask == '#') {
183
                        $cI++;
184
                        if ($y > 0) {
185 1
                            $sRetorno[--$tRetorno] = $sLimpo[--$y];
186 1
                        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
187
                            //$sRetorno[--$tRetorno] = '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
                        }
189
                    } else {
190
                        if ($y > 0) {
191
                            $sRetorno[--$tRetorno] = $mascara[$x];
192 1
                        } else {
193
                            if ($mascara[$x] == '(') {
194
                                $sRetorno[--$tRetorno] = $mascara[$x];
195
                            }
196 1
                        }
197 1
                        $i++;
198
                    }
199
                }
200
            }
201 1
            if (!$flag) {
202
                if ($mascara[0] != '#') {
203
                    $sRetorno = '(' . trim($sRetorno);
204
                }
205
            }
206
            return trim($sRetorno);
207
        } else {
208
            return '';
209
        }
210
    }
211
212
    /**
213
     * pGetNumLines
214
     * Obtem o numero de linhas usadas pelo texto usando a fonte especifidada
215
     * @param  string $text
216
     * @param  number $width
217
     * @param  array  $aFont
218
     * @return number numero de linhas
219
     */
220
    protected function pGetNumLines($text, $width, $aFont = array('font' => 'Times', 'size' => 8, 'style' => ''))
221
    {
222
        $text = trim($text);
223
        $this->pdf->SetFont($aFont['font'], $aFont['style'], $aFont['size']);
0 ignored issues
show
Bug introduced by
The property pdf 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...
224
        $n = $this->pdf->WordWrap($text, $width - 0.2);
225
        return $n;
226
    }
227
228
    /**
229
     * pTextBox
230
     * Cria uma caixa de texto com ou sem bordas. Esta função perimite o alinhamento horizontal
231
     * ou vertical do texto dentro da caixa.
232
     * Atenção : Esta função é dependente de outras classes de FPDF
233
     * Ex. $this->pTextBox(2,20,34,8,'Texto',array('fonte'=>$this->fontePadrao,
234
     * 'size'=>10,'style='B'),'C','L',FALSE,'http://www.nfephp.org')
235
     *
236
     * @param  number  $x       Posição horizontal da caixa, canto esquerdo superior
237
     * @param  number  $y       Posição vertical da caixa, canto esquerdo superior
238
     * @param  number  $w       Largura da caixa
239
     * @param  number  $h       Altura da caixa
240
     * @param  string  $text    Conteúdo da caixa
241
     * @param  array   $aFont   Matriz com as informações para formatação do texto com fonte, tamanho e estilo
242
     * @param  string  $vAlign  Alinhamento vertical do texto, T-topo C-centro B-base
243
     * @param  string  $hAlign  Alinhamento horizontal do texto, L-esquerda, C-centro, R-direita
244
     * @param  boolean $border  TRUE ou 1 desenha a borda, FALSE ou 0 Sem borda
245
     * @param  string  $link    Insere um hiperlink
246
     * @param  boolean $force   Se for true força a caixa com uma unica linha e para isso atera o tamanho do
247
     * fonte até caber no espaço, se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
248
     * e para isso atera o tamanho do fonte até caber no espaço,
249 1
     * se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
250
     * @param  number  $hmax
251
     * @param  number  $vOffSet incremento forçado na na posição Y
252
     * @return number $height Qual a altura necessária para desenhar esta textBox
253
     */
254
    protected function pTextBox(
255
        $x,
256
        $y,
257
        $w,
258
        $h,
259
        $text = '',
260
        $aFont = array('font' => 'Times', 'size' => 8, 'style' => ''),
261
        $vAlign = 'T',
262
        $hAlign = 'L',
263
        $border = 1,
264 1
        $link = '',
0 ignored issues
show
Unused Code introduced by
The parameter $link 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...
265 1
        $force = true,
266 1
        $hmax = 0,
267 1
        $vOffSet = 0
268
    ) {
269
        $oldY = $y;
270 1
        $temObs = false;
0 ignored issues
show
Unused Code introduced by
$temObs 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...
271
        $resetou = false;
272
        if ($w < 0) {
273 1
            return $y;
274
        }
275 1
        if (is_object($text)) {
276
            $text = '';
277 1
        }
278
        if (is_string($text)) {
279
            //remover espaços desnecessários
280
            $text = trim($text);
281
            //converter o charset para o fpdf
282 1
            $text = utf8_decode($text);
283 1
            //decodifica os caracteres html no xml
284
            $text = html_entity_decode($text);
285
        } else {
286 1
            $text = (string) $text;
287
        }
288 1
        //desenhar a borda da caixa
289 1
        if ($border) {
290
            $this->pdf->RoundedRect($x, $y, $w, $h, 0.8, '1234', 'D');
291 1
        }
292
        //estabelecer o fonte
293 1
        $this->pdf->SetFont($aFont['font'], $aFont['style'], $aFont['size']);
294
        //calcular o incremento
295
        $incY = $this->pdf->fontSize; //tamanho da fonte na unidade definida
296 1
        if (!$force) {
297
            //verificar se o texto cabe no espaço
298 1
            $n = $this->pdf->WordWrap($text, $w);
299
        } else {
300 1
            $n = 1;
301
        }
302 1
        //calcular a altura do conjunto de texto
303
        $altText = $incY * $n;
304 1
        //separar o texto em linhas
305
        $lines = explode("\n", $text);
306 1
        //verificar o alinhamento vertical
307
        if ($vAlign == 'T') {
308 1
            //alinhado ao topo
309
            $y1 = $y + $incY;
310
        }
311
        if ($vAlign == 'C') {
312
            //alinhado ao centro
313 1
            $y1 = $y + $incY + (($h - $altText) / 2);
314
        }
315 1
        if ($vAlign == 'B') {
316 1
            //alinhado a base
317 1
            $y1 = ($y + $h) - 0.5;
318 1
        }
319 1
        //para cada linha
320
        foreach ($lines as $line) {
321
            //verificar o comprimento da frase
322
            $texto = trim($line);
323
            $comp = $this->pdf->getStringWidth($texto);
324
            if ($force) {
325
                $newSize = $aFont['size'];
326 1
                while ($comp > $w) {
327 1
                    //estabelecer novo fonte
328
                    $this->pdf->SetFont($aFont['font'], $aFont['style'], --$newSize);
329 1
                    $comp = $this->pdf->getStringWidth($texto);
330 1
                }
331
            }
332 1
            //ajustar ao alinhamento horizontal
333
            if ($hAlign == 'L') {
334
                $x1 = $x + 0.5;
335
            }
336 1
            if ($hAlign == 'C') {
337
                $x1 = $x + (($w - $comp) / 2);
338
            }
339
            if ($hAlign == 'R') {
340
                $x1 = $x + $w - ($comp + 0.5);
341
            }
342
            //escrever o texto
343
            if ($vOffSet > 0) {
344
                if ($y1 > ($oldY + $vOffSet)) {
345 1
                    if (!$resetou) {
346
                        $y1 = $oldY;
347
                        $resetou = true;
348 1
                    }
349 1
                    $this->pdf->Text($x1, $y1, $texto);
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...
350
                }
351 1
            } else {
352
                $this->pdf->Text($x1, $y1, $texto);
353
            }
354 1
            //incrementar para escrever o proximo
355
            $y1 += $incY;
356
            if (($hmax > 0) && ($y1 > ($y + ($hmax - 1)))) {
357
                $temObs = true;
0 ignored issues
show
Unused Code introduced by
$temObs 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...
358
                break;
359
            }
360
        }
361
        return ($y1 - $y) - $incY;
362
    }
363
364
    /**
365
     * Cria uma caixa de texto com ou sem bordas. Esta função permite o alinhamento horizontal
366
     * ou vertical do texto dentro da caixa, rotacionando-o em 90 graus, essa função precisa que
367
     * a classe PDF contenha a função Rotate($angle,$x,$y);
368
     * Atenção : Esta função é dependente de outras classes de FPDF
369
     * Ex. $this->__textBox90(2,20,34,8,'Texto',array('fonte'=>$this->fontePadrao,
370
     * 'size'=>10,'style='B'),'C','L',FALSE,'http://www.nfephp.org')
371
     * @param  number $x Posição horizontal da caixa, canto esquerdo superior
372
     * @param  number $y Posição vertical da caixa, canto esquerdo superior
373
     * @param  number $w Largura da caixa
374
     * @param  number $h Altura da caixa
375
     * @param  string $text Conteúdo da caixa
376
     * @param  array $aFont Matriz com as informações para formatação do texto com fonte, tamanho e estilo
377
     * @param  string $vAlign Alinhamento vertical do texto, T-topo C-centro B-base
378
     * @param  string $hAlign Alinhamento horizontal do texto, L-esquerda, C-centro, R-direita
379
     * @param  boolean $border TRUE ou 1 desenha a borda, FALSE ou 0 Sem borda
380
     * @param  string $link Insere um hiperlink
381
     * @param  boolean $force Se for true força a caixa com uma unica linha e para isso atera o tamanho do
382
     *  fonte até caber no espaço, se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
383
     *  linha e para isso atera o tamanho do fonte até caber no espaço,
384
     *  se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
385
     * @param  number  $hmax
386
     * @param  number  $vOffSet incremento forçado na na posição Y
387
     * @return number $height Qual a altura necessária para desenhar esta textBox
388
     */
389
    protected function pTextBox90(
390
        $x,
391
        $y,
392
        $w,
393
        $h,
394
        $text = '',
395
        $aFont = array('font' => 'Times', 'size' => 8, 'style' => ''),
396
        $vAlign = 'T',
397
        $hAlign = 'L',
398
        $border = 1,
399
        $link = '',
0 ignored issues
show
Unused Code introduced by
The parameter $link 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...
400
        $force = true,
401
        $hmax = 0,
402
        $vOffSet = 0
403
    ) {
404
        $this->pdf->Rotate(90, $x, $y);
405
        $oldY = $y;
406
        $temObs = false;
0 ignored issues
show
Unused Code introduced by
$temObs 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...
407
        $resetou = false;
408
        if ($w < 0) {
409
            return $y;
410
        }
411
        if (is_object($text)) {
412
            $text = '';
413
        }
414
        if (is_string($text)) {
415
            //remover espaços desnecessários
416
            $text = trim($text);
417
            //converter o charset para o fpdf
418
            $text = utf8_decode($text);
419
            //decodifica os caracteres html no xml
420
            $text = html_entity_decode($text);
421
        } else {
422
            $text = (string) $text;
423
        }
424
        //desenhar a borda da caixa
425
        if ($border) {
426
            $this->pdf->RoundedRect($x, $y, $w, $h, 0.8, '1234', 'D');
427
        }
428
        //estabelecer o fonte
429
        $this->pdf->SetFont($aFont['font'], $aFont['style'], $aFont['size']);
430
        //calcular o incremento
431
        $incY = $this->pdf->fontSize; //tamanho da fonte na unidade definida
432
        if (!$force) {
433
            //verificar se o texto cabe no espaço
434
            $n = $this->pdf->WordWrap($text, $w);
435
        } else {
436
            $n = 1;
437
        }
438
        //calcular a altura do conjunto de texto
439
        $altText = $incY * $n;
440
        //separar o texto em linhas
441
        $lines = explode("\n", $text);
442
        //verificar o alinhamento vertical
443
        if ($vAlign == 'T') {
444
            //alinhado ao topo
445
            $y1 = $y + $incY;
446
        }
447
        if ($vAlign == 'C') {
448
            //alinhado ao centro
449
            $y1 = $y + $incY + (($h - $altText) / 2);
450
        }
451
        if ($vAlign == 'B') {
452
            //alinhado a base
453
            $y1 = ($y + $h) - 0.5;
454
        }
455
        //para cada linha
456
        foreach ($lines as $line) {
457
            //verificar o comprimento da frase
458
            $texto = trim($line);
459
            $comp = $this->pdf->GetStringWidth($texto);
460
            if ($force) {
461
                $newSize = $aFont['size'];
462
                while ($comp > $w) {
463
                    //estabelecer novo fonte
464
                    $this->pdf->SetFont($aFont['font'], $aFont['style'], --$newSize);
465
                    $comp = $this->pdf->GetStringWidth($texto);
466
                }
467
            }
468
            //ajustar ao alinhamento horizontal
469
            if ($hAlign == 'L') {
470
                $x1 = $x + 0.5;
471
            }
472
            if ($hAlign == 'C') {
473
                $x1 = $x + (($w - $comp) / 2);
474
            }
475
            if ($hAlign == 'R') {
476
                $x1 = $x + $w - ($comp + 0.5);
477
            }
478
            //escrever o texto
479
            if ($vOffSet > 0) {
480
                if ($y1 > ($oldY + $vOffSet)) {
481
                    if (!$resetou) {
482
                        $y1 = $oldY;
483
                        $resetou = true;
484
                    }
485
                    $this->pdf->Text($x1, $y1, $texto);
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...
486
                }
487
            } else {
488
                $this->pdf->Text($x1, $y1, $texto);
489
            }
490
            //incrementar para escrever o proximo
491
            $y1 += $incY;
492
            if (($hmax > 0) && ($y1 > ($y + ($hmax - 1)))) {
493
                $temObs = true;
0 ignored issues
show
Unused Code introduced by
$temObs 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...
494
                break;
495
            }
496
        }
497
        //Zerando rotação
498
        $this->pdf->Rotate(0, $x, $y);
499
        return ($y1 - $y) - $incY;
500
    }
501
      
502
    protected function tipoPag($tPag)
503
    {
504
        switch ($tPag) {
505
            case '01':
506
                $tPagNome = 'Dinheiro';
507
                break;
508
            case '02':
509
                $tPagNome = 'Cheque';
510
                break;
511
            case '03':
512
                $tPagNome = 'Cartão de Crédito';
513
                break;
514
            case '04':
515
                $tPagNome = 'Cartão de Débito';
516
                break;
517
            case '05':
518
                $tPagNome = 'Crédito Loja';
519
                break;
520
            case '10':
521
                $tPagNome = 'Vale Alimentação';
522
                break;
523
            case '11':
524
                $tPagNome = 'Vale Refeição';
525
                break;
526
            case '12':
527
                $tPagNome = 'Vale Presente';
528
                break;
529
            case '13':
530
                $tPagNome = 'Vale Combustível';
531
                break;
532
            case '14':
533
                $tPagNome = 'Duplicata Mercantil';
534
                break;
535
            case '15':
536
                $tPagNome = 'Boleto Bancário';
537
                break;
538
            case '90':
539
                $tPagNome = 'Sem Pagamento';
540
                break;
541
            case '99':
542
                $tPagNome = 'Outros';
543
                break;
544
            default:
545
                $tPagNome = '';
546
                // Adicionado default para impressão de notas da 3.10
547
        }
548
        return $tPagNome;
549
    }
550
}
551