Passed
Pull Request — master (#114)
by Roberto
06:37
created

Common::pTextBox90()   F

Complexity

Conditions 20
Paths 16513

Size

Total Lines 110
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
cc 20
eloc 71
nc 16513
nop 13
dl 0
loc 110
ccs 0
cts 55
cp 0
crap 420
rs 2
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\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 pSimpleGetValue($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
            return $extraTextBefore . trim($vct->nodeValue) . $extraTextAfter;
25
        }
26
        return '';
27
    }
28
29
    /**
30
     * Recupera e reformata a data do padrão da NFe para dd/mm/aaaa
31
     * @author Marcos Diez
32
     * @param  DOM    $theObj
33
     * @param  string $keyName   identificador da TAG do xml
34
     * @param  string $extraText prefixo do retorno
35
     * @return string
36
     */
37
    protected function pSimpleGetDate($theObj, $keyName, $extraText = '')
38
    {
39
        if (!isset($theObj) || !is_object($theObj)) {
40
            return '';
41
        }
42
        $vct = $theObj->getElementsByTagName($keyName)->item(0);
43
        if (isset($vct)) {
44
            $theDate = explode("-", $vct->nodeValue);
45
            return $extraText . $theDate[2] . "/" . $theDate[1] . "/" . $theDate[0];
46
        }
47
        return '';
48
    }
49
50
    /**
51
     * camcula digito de controle modulo 11
52
     * @param  string $numero
53
     * @return integer modulo11 do numero passado
54
     */
55
    protected function pModulo11($numero = '')
56
    {
57
        if ($numero == '') {
58
            return '';
59
        }
60
        $numero = (string) $numero;
61
        $tamanho = strlen($numero);
62
        $soma = 0;
63
        $mult = 2;
64
        for ($i = $tamanho - 1; $i >= 0; $i--) {
65
            $digito = (int) $numero[$i];
66
            $r = $digito * $mult;
67
            $soma += $r;
68
            $mult++;
69
            if ($mult == 10) {
70
                $mult = 2;
71
            }
72
        }
73
        $resto = ($soma * 10) % 11;
74
        return ($resto == 10 || $resto == 0) ? 1 : $resto;
75
    }
76
77
    /**
78
     * Converte datas no formato YMD (ex. 2009-11-02) para o formato brasileiro 02/11/2009)
79
     * @param  string $data Parâmetro extraido da NFe
80
     * @return string Formatada para apresentação da data no padrão brasileiro
81
     */
82 1
    protected function pYmd2dmy($data = '')
83
    {
84 1
        if ($data == '') {
85
            return '';
86
        }
87 1
        $needle = "/";
88 1
        if (strstr($data, "-")) {
89 1
            $needle = "-";
90
        }
91 1
        $dt = explode($needle, $data);
92 1
        return "$dt[2]/$dt[1]/$dt[0]";
93
    }
94
95
    /**
96
     * pConvertTime
97
     * Converte a informação de data e tempo contida na NFe
98
     * @param  string $DH Informação de data e tempo extraida da NFe
99
     * @return timestamp UNIX Para uso com a funçao date do php
100
     */
101 1
    protected function pConvertTime($DH = '')
102
    {
103 1
        if ($DH == '') {
104 1
            return '';
105
        }
106 1
        $DH = str_replace('+', '-', $DH);
107 1
        $aDH = explode('T', $DH);
108 1
        $adDH = explode('-', $aDH[0]);
109 1
        if (count($aDH) > 1) {
110 1
            $inter = explode('-', $aDH[1]);
111 1
            $atDH = explode(':', $inter[0]);
112 1
            $timestampDH = mktime($atDH[0], $atDH[1], $atDH[2], $adDH[1], $adDH[2], $adDH[0]);
113
        } else {
114
            $timestampDH = mktime($month = $adDH[1], $day = $adDH[2], $year = $adDH[0]);
115
        }
116 1
        return $timestampDH;
117
    }
118
119
    /**
120
     * Função de formatação de strings onde o cerquilha # é um coringa
121
     * que será substituido por digitos contidos em campo.
122
     * @param  string $campo   String a ser formatada
123
     * @param  string $mascara Regra de formatção da string (ex. ##.###.###/####-##)
124
     * @return string Retorna o campo formatado
125
     */
126 1
    protected function pFormat($campo = '', $mascara = '')
127
    {
128 1
        if ($campo == '' || $mascara == '') {
129
            return $campo;
130
        }
131
        //remove qualquer formatação que ainda exista
132 1
        $sLimpo = preg_replace("(/[' '-./ t]/)", '', $campo);
133
        // pega o tamanho da string e da mascara
134 1
        $tCampo = strlen($sLimpo);
135 1
        $tMask = strlen($mascara);
136 1
        if ($tCampo > $tMask) {
137 1
            $tMaior = $tCampo;
138
        } else {
139 1
            $tMaior = $tMask;
140
        }
141
        //contar o numero de cerquilhas da mascara
142 1
        $aMask = str_split($mascara);
143 1
        $z = 0;
144 1
        $flag = false;
145 1
        foreach ($aMask as $letra) {
146 1
            if ($letra == '#') {
147 1
                $z++;
148
            }
149
        }
150 1
        if ($z > $tCampo) {
151
            //o campo é menor que esperado
152 1
            $flag = true;
153
        }
154
        //cria uma variável grande o suficiente para conter os dados
155 1
        $sRetorno = '';
156 1
        $sRetorno = str_pad($sRetorno, $tCampo + $tMask, " ", STR_PAD_LEFT);
157
        //pega o tamanho da string de retorno
158 1
        $tRetorno = strlen($sRetorno);
159
        //se houve entrada de dados
160 1
        if ($sLimpo != '' && $mascara != '') {
161
            //inicia com a posição do ultimo digito da mascara
162 1
            $x = $tMask;
163 1
            $y = $tCampo;
164 1
            $cI = 0;
165 1
            for ($i = $tMaior - 1; $i >= 0; $i--) {
166 1
                if ($cI < $z) {
167
                    // e o digito da mascara é # trocar pelo digito do campo
168
                    // se o inicio da string da mascara for atingido antes de terminar
169
                    // o campo considerar #
170 1
                    if ($x > 0) {
171 1
                        $digMask = $mascara[--$x];
172
                    } else {
173
                        $digMask = '#';
174
                    }
175
                    //se o fim do campo for atingido antes do fim da mascara
176
                    //verificar se é ( se não for não use
177 1
                    if ($digMask == '#') {
178 1
                        $cI++;
179 1
                        if ($y > 0) {
180 1
                            $sRetorno[--$tRetorno] = $sLimpo[--$y];
181 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...
182
                            //$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...
183
                        }
184
                    } else {
185 1
                        if ($y > 0) {
186 1
                            $sRetorno[--$tRetorno] = $mascara[$x];
187
                        } else {
188
                            if ($mascara[$x] == '(') {
189
                                $sRetorno[--$tRetorno] = $mascara[$x];
190
                            }
191
                        }
192 1
                        $i++;
193
                    }
194
                }
195
            }
196 1
            if (!$flag) {
197 1
                if ($mascara[0] != '#') {
198
                    $sRetorno = '(' . trim($sRetorno);
199
                }
200
            }
201 1
            return trim($sRetorno);
202
        } else {
203
            return '';
204
        }
205
    }
206
207
    /**
208
     * pGetNumLines
209
     * Obtem o numero de linhas usadas pelo texto usando a fonte especifidada
210
     * @param  string $text
211
     * @param  number $width
212
     * @param  array  $aFont
213
     * @return number numero de linhas
214
     */
215
    protected function pGetNumLines($text, $width, $aFont = array('font' => 'Times', 'size' => 8, 'style' => ''))
216
    {
217
        $text = trim($text);
218
        $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...
219
        $n = $this->pdf->WordWrap($text, $width - 0.2);
220
        return $n;
221
    }
222
223
    /**
224
     * pTextBox
225
     * Cria uma caixa de texto com ou sem bordas. Esta função perimite o alinhamento horizontal
226
     * ou vertical do texto dentro da caixa.
227
     * Atenção : Esta função é dependente de outras classes de FPDF
228
     * Ex. $this->pTextBox(2,20,34,8,'Texto',array('fonte'=>$this->fontePadrao,
229
     * 'size'=>10,'style='B'),'C','L',FALSE,'http://www.nfephp.org')
230
     *
231
     * @param  number  $x       Posição horizontal da caixa, canto esquerdo superior
232
     * @param  number  $y       Posição vertical da caixa, canto esquerdo superior
233
     * @param  number  $w       Largura da caixa
234
     * @param  number  $h       Altura da caixa
235
     * @param  string  $text    Conteúdo da caixa
236
     * @param  array   $aFont   Matriz com as informações para formatação do texto com fonte, tamanho e estilo
237
     * @param  string  $vAlign  Alinhamento vertical do texto, T-topo C-centro B-base
238
     * @param  string  $hAlign  Alinhamento horizontal do texto, L-esquerda, C-centro, R-direita
239
     * @param  boolean $border  TRUE ou 1 desenha a borda, FALSE ou 0 Sem borda
240
     * @param  string  $link    Insere um hiperlink
241
     * @param  boolean $force   Se for true força a caixa com uma unica linha e para isso atera o tamanho do
242
     * fonte até caber no espaço, se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
243
     * e para isso atera o tamanho do fonte até caber no espaço,
244
     * se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
245
     * @param  number  $hmax
246
     * @param  number  $vOffSet incremento forçado na na posição Y
247
     * @return number $height Qual a altura necessária para desenhar esta textBox
248
     */
249 1
    protected function pTextBox(
250
        $x,
251
        $y,
252
        $w,
253
        $h,
254
        $text = '',
255
        $aFont = array('font' => 'Times', 'size' => 8, 'style' => ''),
256
        $vAlign = 'T',
257
        $hAlign = 'L',
258
        $border = 1,
259
        $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...
260
        $force = true,
261
        $hmax = 0,
262
        $vOffSet = 0
263
    ) {
264 1
        $oldY = $y;
265 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...
266 1
        $resetou = false;
267 1
        if ($w < 0) {
268
            return $y;
269
        }
270 1
        if (is_object($text)) {
271
            $text = '';
272
        }
273 1
        if (is_string($text)) {
274
            //remover espaços desnecessários
275 1
            $text = trim($text);
276
            //converter o charset para o fpdf
277 1
            $text = utf8_decode($text);
278
        } else {
279
            $text = (string) $text;
280
        }
281
        //desenhar a borda da caixa
282 1
        if ($border) {
283 1
            $this->pdf->RoundedRect($x, $y, $w, $h, 0.8, '1234', 'D');
284
        }
285
        //estabelecer o fonte
286 1
        $this->pdf->SetFont($aFont['font'], $aFont['style'], $aFont['size']);
287
        //calcular o incremento
288 1
        $incY = $this->pdf->fontSize; //tamanho da fonte na unidade definida
289 1
        if (!$force) {
290
            //verificar se o texto cabe no espaço
291 1
            $n = $this->pdf->WordWrap($text, $w);
292
        } else {
293 1
            $n = 1;
294
        }
295
        //calcular a altura do conjunto de texto
296 1
        $altText = $incY * $n;
297
        //separar o texto em linhas
298 1
        $lines = explode("\n", $text);
299
        //verificar o alinhamento vertical
300 1
        if ($vAlign == 'T') {
301
            //alinhado ao topo
302 1
            $y1 = $y + $incY;
303
        }
304 1
        if ($vAlign == 'C') {
305
            //alinhado ao centro
306 1
            $y1 = $y + $incY + (($h - $altText) / 2);
307
        }
308 1
        if ($vAlign == 'B') {
309
            //alinhado a base
310
            $y1 = ($y + $h) - 0.5;
311
        }
312
        //para cada linha
313 1
        foreach ($lines as $line) {
314
            //verificar o comprimento da frase
315 1
            $texto = trim($line);
316 1
            $comp = $this->pdf->getStringWidth($texto);
317 1
            if ($force) {
318 1
                $newSize = $aFont['size'];
319 1
                while ($comp > $w) {
320
                    //estabelecer novo fonte
321
                    $this->pdf->SetFont($aFont['font'], $aFont['style'], --$newSize);
322
                    $comp = $this->pdf->getStringWidth($texto);
323
                }
324
            }
325
            //ajustar ao alinhamento horizontal
326 1
            if ($hAlign == 'L') {
327 1
                $x1 = $x + 0.5;
328
            }
329 1
            if ($hAlign == 'C') {
330 1
                $x1 = $x + (($w - $comp) / 2);
331
            }
332 1
            if ($hAlign == 'R') {
333
                $x1 = $x + $w - ($comp + 0.5);
334
            }
335
            //escrever o texto
336 1
            if ($vOffSet > 0) {
337
                if ($y1 > ($oldY + $vOffSet)) {
338
                    if (!$resetou) {
339
                        $y1 = $oldY;
340
                        $resetou = true;
341
                    }
342
                    $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...
343
                }
344
            } else {
345 1
                $this->pdf->Text($x1, $y1, $texto);
346
            }
347
            //incrementar para escrever o proximo
348 1
            $y1 += $incY;
349 1
            if (($hmax > 0) && ($y1 > ($y + ($hmax - 1)))) {
350
                $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...
351 1
                break;
352
            }
353
        }
354 1
        return ($y1 - $y) - $incY;
355
    }
356
357
    /**
358
     * Cria uma caixa de texto com ou sem bordas. Esta função permite o alinhamento horizontal
359
     * ou vertical do texto dentro da caixa, rotacionando-o em 90 graus, essa função precisa que
360
     * a classe PDF contenha a função Rotate($angle,$x,$y);
361
     * Atenção : Esta função é dependente de outras classes de FPDF
362
     * Ex. $this->__textBox90(2,20,34,8,'Texto',array('fonte'=>$this->fontePadrao,
363
     * 'size'=>10,'style='B'),'C','L',FALSE,'http://www.nfephp.org')
364
     * @param  number $x Posição horizontal da caixa, canto esquerdo superior
365
     * @param  number $y Posição vertical da caixa, canto esquerdo superior
366
     * @param  number $w Largura da caixa
367
     * @param  number $h Altura da caixa
368
     * @param  string $text Conteúdo da caixa
369
     * @param  array $aFont Matriz com as informações para formatação do texto com fonte, tamanho e estilo
370
     * @param  string $vAlign Alinhamento vertical do texto, T-topo C-centro B-base
371
     * @param  string $hAlign Alinhamento horizontal do texto, L-esquerda, C-centro, R-direita
372
     * @param  boolean $border TRUE ou 1 desenha a borda, FALSE ou 0 Sem borda
373
     * @param  string $link Insere um hiperlink
374
     * @param  boolean $force Se for true força a caixa com uma unica linha e para isso atera o tamanho do
375
     *  fonte até caber no espaço, se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
376
     *  linha e para isso atera o tamanho do fonte até caber no espaço,
377
     *  se falso mantem o tamanho do fonte e usa quantas linhas forem necessárias
378
     * @param  number  $hmax
379
     * @param  number  $vOffSet incremento forçado na na posição Y
380
     * @return number $height Qual a altura necessária para desenhar esta textBox
381
     */
382
    protected function pTextBox90(
383
        $x,
384
        $y,
385
        $w,
386
        $h,
387
        $text = '',
388
        $aFont = array('font' => 'Times', 'size' => 8, 'style' => ''),
389
        $vAlign = 'T',
390
        $hAlign = 'L',
391
        $border = 1,
392
        $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...
393
        $force = true,
394
        $hmax = 0,
395
        $vOffSet = 0
396
    ) {
397
        $this->pdf->Rotate(90, $x, $y);
398
        $oldY = $y;
399
        $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...
400
        $resetou = false;
401
        if ($w < 0) {
402
            return $y;
403
        }
404
        if (is_object($text)) {
405
            $text = '';
406
        }
407
        if (is_string($text)) {
408
            //remover espaços desnecessários
409
            $text = trim($text);
410
            //converter o charset para o fpdf
411
            $text = utf8_decode($text);
412
        } else {
413
            $text = (string) $text;
414
        }
415
        //desenhar a borda da caixa
416
        if ($border) {
417
            $this->pdf->RoundedRect($x, $y, $w, $h, 0.8, '1234', 'D');
418
        }
419
        //estabelecer o fonte
420
        $this->pdf->SetFont($aFont['font'], $aFont['style'], $aFont['size']);
421
        //calcular o incremento
422
        $incY = $this->pdf->fontSize; //tamanho da fonte na unidade definida
423
        if (!$force) {
424
            //verificar se o texto cabe no espaço
425
            $n = $this->pdf->WordWrap($text, $w);
426
        } else {
427
            $n = 1;
428
        }
429
        //calcular a altura do conjunto de texto
430
        $altText = $incY * $n;
431
        //separar o texto em linhas
432
        $lines = explode("\n", $text);
433
        //verificar o alinhamento vertical
434
        if ($vAlign == 'T') {
435
            //alinhado ao topo
436
            $y1 = $y + $incY;
437
        }
438
        if ($vAlign == 'C') {
439
            //alinhado ao centro
440
            $y1 = $y + $incY + (($h - $altText) / 2);
441
        }
442
        if ($vAlign == 'B') {
443
            //alinhado a base
444
            $y1 = ($y + $h) - 0.5;
445
        }
446
        //para cada linha
447
        foreach ($lines as $line) {
448
            //verificar o comprimento da frase
449
            $texto = trim($line);
450
            $comp = $this->pdf->GetStringWidth($texto);
451
            if ($force) {
452
                $newSize = $aFont['size'];
453
                while ($comp > $w) {
454
                    //estabelecer novo fonte
455
                    $this->pdf->SetFont($aFont['font'], $aFont['style'], --$newSize);
456
                    $comp = $this->pdf->GetStringWidth($texto);
457
                }
458
            }
459
            //ajustar ao alinhamento horizontal
460
            if ($hAlign == 'L') {
461
                $x1 = $x + 0.5;
462
            }
463
            if ($hAlign == 'C') {
464
                $x1 = $x + (($w - $comp) / 2);
465
            }
466
            if ($hAlign == 'R') {
467
                $x1 = $x + $w - ($comp + 0.5);
468
            }
469
            //escrever o texto
470
            if ($vOffSet > 0) {
471
                if ($y1 > ($oldY + $vOffSet)) {
472
                    if (!$resetou) {
473
                        $y1 = $oldY;
474
                        $resetou = true;
475
                    }
476
                    $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...
477
                }
478
            } else {
479
                $this->pdf->Text($x1, $y1, $texto);
480
            }
481
            //incrementar para escrever o proximo
482
            $y1 += $incY;
483
            if (($hmax > 0) && ($y1 > ($y + ($hmax - 1)))) {
484
                $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...
485
                break;
486
            }
487
        }
488
        //Zerando rotação
489
        $this->pdf->Rotate(0, $x, $y);
490
        return ($y1 - $y) - $incY;
491
    }
492
}
493